"Not declared in scope" error

I am fairly new at programming, and I really cannot figure this one out. When compiled, I get 'Expected primary expression'errors for the array in main, and 'Not declared in scope' for every perimeter in both of my void functions. I've been trying to solve this for nearly three hours now, to no avail. Help is much 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
   #include <iostream>
 using namespace std;
 
 const int COLS = 5;//grade
 const int ROWS = 3;//studnet
 
 void computeMaximum(float grades[][COLS], int rows, float maximum[]);

 void showArray(float array[], int size);
 
 int main()
 {
	 int rows;
	 float grades[ROWS][COLS];
	 float maximum[ROWS];
	 float array[5];
	 
	 //enters 5 quiz grades for each student
	 for (int row=0; row<ROWS; row++)
	 {
		 for (int col=0; col<COLS-1;col++)
		 {
			 cout<<"Enter QUIZ #"<<col+1<<"for STUDENT #" << row+1 << ":";
			 cin>>grades[row][col];
		 }
	 }
	 
	computeMaximum(grades, rows, maximum); //write a function call here to compute maximum grade
	 for (int row=0; row<ROWS; row++)
	 {
		 for (int col=0; col<COLS; col++)
		 {
			cout << grades[row][col]<<" ";
		 } 
		 cout << endl;
	 }
	showArray(array[], 4);
	cout<< "Max grades: " <<showArray(array[], 4) <<endl;//call showarry  here to display max grade array
	return 0;
 }
 
 
 void computeMaximum(float grades[][COLS], int rows, float maximum[])
{
	maximum = array[0];
	int i=0;
	for(int i=0; i<rows; i++)
	{
		for(int j=0; j<5; j++)
		{
			array[i]<array[][j];
			array[j]=array[i];
		}
	}
}

void showArray(float array[], int size)
{
	computeMaximum(float grades[][COLS], int rows,float maximum[]);
	cout << "max Grades;"<< computeMaximum(grades, rows, maximum)<<endl;
}
 
Hello starlightexplorer,

You have many errors in the code.

Line 37 showArray(array[], 4);. Unless your intent is to send a single element to the function, only the variable is needed.The []s are not.

Not a problem, but double" is preferred over "float".

In line 38 the "cout" statement contains a function. First the function returns nothing that can be printed. Second only the variable name is needed not the []s.

In the "computeMaximum" function the first line maximum = array[0];, but where is "array" defined. I think you meant to pass this as a parameter and maybe forgot to?

In the line array[i] < array[][j];. You access the lhs as a 1D array, which it is, and the rhs as a 2D array, which it is not> Also you would need to give the dimension a value so it knows which element of the 2D array to access, but since it is a 1D array you do not need it here. I have not test that part yet, but I think what you want is array[i] < array[i + 1];. It is also going to take some more time to figure out what you are trying to do.

In the "showArray" function the first function call is wrong All you need is what you did in the "cout" statement, but even that is wrong because the functionn nothing that can be printed and the "computeMaximum" function does not print anything. Actually it does not really do anything.

The last problem with the "showArray" function is that "grades", "rows" and "maximum" are not defined in the function or even passed to the function.

For your line 59 you again you only need to send the variable names to the function.

Andy
Andy,

Thank you so much. This helped a lot. My professor gave us a partial program to fill in, and I believe correct also, but we are supposed to collect the user input grades, use a void function (computeMaximum) to find the maximum grades out of what was collected, then use another void function (showArray) to display the max grades. I am also hung up on how to deal with the arrays. There is an array that stores the user's input, and the array used in showArray.

Starlight
Hello starlightexplorer,

Since this is for an assignment post the actual instructions. It does help those who read this.

And since you were given something to work with as a start post the unaltered version. Again so everyone will know what you started with.

At the moment I do not understand what the arrays "maximum" and "array" are used for. And try not to call it array. This could cause a problem in the future. It would help if you explain what "maximum" and "array" are used for.

It helps to work in small parts or steps. First I would get the input working to have something to work with before you call the other functions.

This may be more than what you want or can use for now, but worth looking at:
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
#include <iostream>
#include <string>

using namespace std;

const int ROWS = 3;//studnet  // <--- You should think of the order as Rows then Columns, but really makes no difference.
const int COLS = 5;//grade

//void computeMaximum(double grades[][COLS], int rows, double maximum[]);
//
//void showArray(double array[], int size);

int main()
{
	//int rows;
	double grades[ROWS][COLS]{};
	const std::string names[ROWS]  // <--- By leaving the size blank it is easy to change, but it better to use "ROWS" for the size.
	{
		"Saphira",
		"Navarre",
		"Isabeau"
	};
	//double maximum[ROWS];
	//double array[COLS];  // <--- Unless this size is to be different. Then you will need a new variable name.

	//enters 5 quiz grades for each student
	for (int row = 0; row < ROWS; row++)
	{
		cout << "\n For student #" << row + 1 << ' ' << names[row] << '\n';
		
		for (int col = 0; col < COLS; col++)  // <--- Do not need the "- 1". The "<" will take care of that.
		{
			std::cout << "  Enter quiz #" << col + 1 << ": ";
			cin >> grades[row][col];
		}
	}

Give it a try and see what you think. Taking out the extras in the for loops is OK, but the format is still worth looking at.

Andy
Topic archived. No new replies allowed.