arrays

Not sure where to go with this one, it has some description in the code. I have been trying to find the right info to complete the program. I am still working on it now, but I would like to see what someone with a different prospective can do with it. Your feedback will be appreciated.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include<vector>
using namespace std;

//Function Prototypes
int roundDoubleToInt (double);
void sortArray(int[], int);
void display();

const int SIZE = 100;  //used to limit array size

int main()
{
	//Declare data objects
	int numbers[SIZE] = {82, 77, 90, 89, 63, 71, 75, 86, 45,78,-1};
//	sortArray(nums, 20); 
	double total, average;
	//Definition of function display.
	//Display name and lab number.
	display();

	//Input data
	//Loops until user enters -1
	cout <<"Enter test scores." << endl;  //this is a priming read

	do //until user enters a negative value
	{
		cin >> score;
		do  //error checking for values over 100
		{
		
        if (score > 100)	//add code here for error checking
		{
			cout <<"you have enterered too large of a number" << endl;
		}
        } while (score > 100);

		if (score >= 0)	
		{	
            //process valid entry
            //store in array
            //accumulate total
            total = getTotal(numbers, SIZE)
            //keep count of the number of values entered into 
            (i < -1 && SIZE[i] == SIZE[i+1])
            
            
  
		}
		
	} while (count < 100 && score >= 0);


	//Calculate average score
	average = total / (SIZE -1);
	
	//Sort scores highest to lowest with bubble sort.
	list =  {82, 77, 90, 89, 63, 71, 75, 86, 45,78,-1};
	
	//Pass array to sort routine for processing
	

	//Output array in descending order and averages, totals, etc

    system("pause");
    
	return 0;
}


//sort scores descending 
void sortArray(int nums[], int count)
{
	int hold, a, b;						// a and b are subscripts.
	for(a = 0; a < count-1; a++)		//Start first loop with 0.
	{
		for(b = a + 1; b < count; b++)	//Start second loop with 1.
		{
			if(nums[a] < nums[b])		//Compare nums[0] to numbs[1].
			{
				hold = nums[a];			//If first is bigger,
				nums[a] = nums[b];		//swap the two
				nums[b] = hold;
			}
		}
	}
}


//display program information
void display()
{
	cout <<"Array Lab." << endl;
	cout <<"Programmed by you" <<endl;
}


//round a double to closest integer
int roundDoubleToInt(double d)
{
	int result;

	if (d > 0) result = (int)(d + 0.5);    //result = dresult + 0.5
	else result = (int)(d - 0.5);          //result = deresult - 0.5

	return result;
}
heres more info on project

Project - Test Scores
Project will have three functions: main, round, sort_Array

Declare an array to store a maximum of 100 integer test scores.
Use a loop to permit operator entry of the scores into the array.
Scores will be positive, so use a negative number as a sentinel.
Display the following information:
The test scores in descending order - (sort the array using a modification of bubble sort function below)
The total of all test scores.
The average score rounded to the nearest whole number.

(Code a generic function to receive a double and return the nearest integer - for use here and in future)
Test Data:
82, 77, 90, 89, 63, 71, 75, 86, 45, 78, -1
feedback will be appreciated.

The code as posted won't compile, it has several errors.

Using variables that haven't actually been defined, using a function that doesn't exist, at least one missing semi-colon (maybe two)....

And one line of code that is likely supposed to be an if condition instead of stand-alone code.

Have you actually tried to compile this? If not, why not.

If you have tried you should already know something about the errors.
variable score isn't defined
function getTotal isn't defined
L43 is mising a ;
what is L45?
L58 - list needs a type specified

There is an-inbuilt function std::sort() to sort an array
http://www.cplusplus.com/reference/algorithm/sort/

There is an in-built function to round std::lrint()
http://www.cplusplus.com/reference/cmath/lrint/
seeplus wrote:
There is an in-built function to round std::lrint()

There is also std::round and std::lround so that you don't have to worry about what rounding mode has been set.

https://www.cplusplus.com/reference/cmath/round/
https://www.cplusplus.com/reference/cmath/lround/
Last edited on
As a first refactor, consider something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <algorithm>
#include <functional>

void display();

constexpr size_t SIZE {100};

int main() {
	//int numbers[SIZE] {82, 77, 90, 89, 63, 71, 75, 86, 45, 78, -1};
	int numbers[SIZE] {};
	double total {};
	int score {}, cnt {};

	display();

	std::cout << "Enter a maximum of " << SIZE << " test scores - each separated by a space and terminated by - 1\n";

	do {
		do {
			std::cin >> score;
		} while (score > 100 && (std::cout << "You have entered too large a number\n"));

		if (score >= 0) {
			numbers[cnt++] = score;
			total += score;
		}
	} while (score >= 0 && cnt < SIZE);

	if (cnt) {
		std::sort(numbers, numbers + cnt, std::greater());

		std::cout << "The total of the scores is " << total << '\n';
		std::cout << "The average score is " << total / cnt << '\n';
		std::cout << "The scores in descending order are:\n";

		for (size_t i = 0; i < cnt; ++i)
			std::cout << numbers[i] << ' ';

		std::cout << '\n';
	} else
		std::cout << "No valid number entered\n";
}

// Display program information
void display() {
	std::cout << "Array Lab.\n";
	std::cout << "Programmed by you\n\n";
}



Array Lab.
Programmed by you
Enter a maximum of 100 test scores - each separated by a space and terminated by - 1
1 2 3 4 5 -1
The total of the scores is 15
The average score is 3
The scores in descending order are
5 4 3 2 1

Last edited on
this is good. thanks for the help guys.
Topic archived. No new replies allowed.