Array

I've started to write the program that follows but have already started getting error messages so I stopped rather than continue.

The instructions are as follows:

Write one program that does the following:

1. 1. Ask the user for ten (10) grades, and store the data in an array. Compute the average of all the grades. Print the original ten grades and the average.

a. Declare an integer array with the name of “grades” of size 10 in the main function.

b. Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.

i. The function will receive an integer array and the size of the array.

ii. In the function prototype and the function header, call the array “gradeArray” and call the size of the array "size."

iii. Create a For Loop that cycles through each cell of the array.

iv. At each cell prompt the User for a grade.

v. Store the grade entered by the User into the array cell.

c. From main, send this function the array called “grades” and the size of the array.

d. Create a function called “computeAverage” that computes the average of an array of integers and returns the average as a float.

i. The function will receive an integer array and the size of the array.

ii. In the function prototype and the function header, call the array “numbers” and call the size of the array "size."

iii. Create a For Loop that cycles through each cell of the array and computes a running total of the values stored in the array.

iv. Use the size of the array to compute the average from the running total.

v. Return the average through the function name.

e. From main, send this function the array called “grades” and the size of the array.

f. From main, print out the average returned by computeAverage.

g. Create a third function called “printInts” that will print out the contents of an integer array. This function will be void.

i. The function will receive an integer array and the size of the array.

ii. In the function prototype and the function header, call the array “intArray” and call the size of the array "size."

iii. Create a For Loop that prints out the contents of the array.



2. 2. Generate 20 random numbers within a range of 1 to 50 and store them in an integer array named “randomArray.” Then print out the contents of the array.

a. Create a function named “getRandom” that will generate a random number between any range.

i. The function will receive two call-by-value arguments – the beginning value of the range and the ending value of the range.

ii. Generate a random number within the specified range.

iii. Send the random number back through the function name.

b. In the main function, create a For Loop that cycles through the array called randomArray.

i. With each cycle, call your getRandom function from main and store the random number it returns into your array.

c. In main, call the function printInts to print out the contents of the array called randomArray. Make sure you use the SAME FUNCTION you created in step 1. You are just sending it a different array.

d. Next, in the main function, call the function computeAverage to compute the average of the random numbers you stored in the array called randomArray. Make sure you use the SAME FUNCTION you created in step 1. You are just sending it a different array.

e. In main, print out the average returned by computeAverage.

This is the little bit of code I've written so far but I'm already getting error messages:
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
const int SIZE = 10;//Size of the array
int grades[SIZE];//Reserve 10 memory locations to store quiz grades
int getGrades;//grade entered by user
int gradeArray(SIZE);//function prototype
int grade;
float numbers(SIZE);//function prototype

int main() 

{

for ( getGrades = 0;  getGrades < SIZE; getGrades++)
{
          cout <<  "Please Enter Grade # " << getGrades + 1 << " grade:  ";    
          cin >> grade;
          grade(getGrades) = grade;
} //end for getGrades

//Print grades in the array

for ( getGrades = 0;  getGrades < SIZE; getGrades++)
{
          cout <<  "Grade#:  " << grade(getGrades) << endl;    
} //end for getGrades
}


The error message I'm getting at this point is:
In function 'int main()':
23 26 (Error) 'grade' cannot be used as a function
30 50 [Error] 'grade' cannot be used as a function

I don't want to go any further
Last edited on
Maybe you meant grades[getGrades] instead of grade(getGrades) ?
do yourself a favor and try to use better variable names. 5 versions of the word grade is going to confuse you and make a mess. Some of this is the teacher's fault.

your instructions are to make the array inside main.

int main()
{
int grades[10]; //inside main.
}

grade(getGrades) = grade; //this line is complete nonsense. No offense. what do YOU think it is supposed to do? What it says is this:
invoke a function grade (none exists) with the parameter getGrades (global int, ok) and assign the result of the function (this is not legal, only variables can get a result assigned in to them) the value grade.

you appear to be trying so lets do the second one to get you started.
b. Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getGrades(int *gradesarray)
{
    cout << "enter the grades\n";
    for(int x = 0; x < 10; x++) 
    cin >> gradesarray[x];
}


and in main you can call that:

getGrades(grades);
and test it to make sure it worked:
for(int x = 0; x < 10; x++)
 cout << grades[x] <<" "; cout << endl;

Last edited on
Thanks, jonnin for your help but I'm still confused. Did you mean for the code to look like this?
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
#include <cstdlib> 
#include <iostream> 

using namespace std; 

int getGrades;//grade entered by user
int grade;


int main() 

{
int grades[10];//size of array
getGrades(grades);
}
}//end main
void getGrades(int *gradesarray)
{
    cout << "enter the grades\n";
    for(int x = 0; x < 10; x++) 
    cin >> gradesarray[x];
}
{

getGrades(grades);
for(int x = 0; x < 10; x++)
{
 cout << grades[x] <<" "; cout << endl;
}


Also some of the code from my first post was given as examples from the instructor and edited by me to fit this particular assignment.
More like this.

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

#include <cstdlib> 
#include <iostream> 

using namespace std; 

void getGrades(int *gradesarray); //header, so main can see it 

int main() 
{
int grade;  //removed int getGrades, which was not workable because function has same name.  Moved all variables inside main as globals are bad practice. 
int grades[10];//size of array 
getGrades(grades);

	for(int x = 0; x < 10; x++)
	{
	 cout << grades[x] <<" "; cout << endl;
	}

}

void getGrades(int *gradesarray)
{
    cout << "enter the grades\n";
    for(int x = 0; x < 10; x++) 
    cin >> gradesarray[x];
}



now see if you can make the above starting point do this stuff:

ii. In the function prototype and the function header, call the array “gradeArray” and call the size of the array "size."

iii. Create a For Loop that cycles through each cell of the array.

iv. At each cell prompt the User for a grade.

v. Store the grade entered by the User into the array cell. (already did this)
Last edited on
It might help if you copy/paste all the instructions into your code as comments.
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
#include <stdio.h>

/*
1. 1. Ask the user for ten (10) grades, and store the data in an array. 
Compute the average of all the grades. 
Print the original ten grades and the average.
*/

// b. Create a function called “getGrades” that prompts the User for the grades 
//    and puts them in an integer array.
// i. The function will receive an integer array and the size of the array.
// ii. In the function prototype and the function header, 
//     call the array “gradeArray” and call the size of the array "size."
void getGrades(int gradeArray[], int size) {
    // iii. Create a For Loop that cycles through each cell of the array.
    for ( ; ; ) {
        // iv. At each cell prompt the User for a grade.
        // v. Store the grade entered by the User into the array cell.
    }
}

int main ( ) {
    // a. Declare an integer array with the name of “grades” of size 10 in the main function.
    int grades[10];
    // c. From main, send this function the array called “grades” and the size of the array.
    getGrades(grades,10);
}


This minimal skeleton of a program will at least compile.
Now finish off points iii,iv,v ONE line at a time, and make sure it still compiles after each edit.

This is the programming equivalent of "paint by numbers".
There is very little to think about, you just have to read what is written down and follow it.

Topic archived. No new replies allowed.