invalid conversion from 'int*' to 'int'

Assignment given in my intro c++ class.

I keep getting the error (invalid conversion from 'int*' to 'int') in my calcAvg function.


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
#include <iostream>
using namespace std;

//for file level scope
		const int columns = 4;

//prototype for 2d array
void showScores(int scores[][columns], int);
double calcAvg(int X[][columns], int);

int main()
{
	//declare rows & columns
	const int rows    = 5;

	//declare 2d array
	int scores[rows][columns];
	
	//populate 2d array
	for(int i=0; i<rows; i++)
	{
		cout<<"Enter "<<columns<<" scores for row "<<i+1<<": ";
		for (int j=0; j<columns; j++)
		{
			cin>>scores[i][j];
		}
	}
	

	//display  scores with function
	showScores(scores, rows);
	
	//display avg
	double avg = calcAvg(scores, rows);
	cout<<"Avg: "<<avg<<endl;
	
	
}//endmain



//defines a function to accept 2d array
void showScores( int scores[][columns], int rows)
{

//display 2d array
for(int i=0; i<rows; i++)
{
	cout<<"Scores for row "<<i+1<<": ";
	for (int j=0; j<columns; j++)
	{
		cout<<scores[i][j]<<" ";
	}
	cout<<endl;
	
}
	
}//endshowScores

//calcAvg
double calcAvg(int X[][columns], int s)
{
	double a;
	
	int sum = 0;
	for(int i=0; i<s; i++)
		sum += X[i];
   //(error: invalid conversion from 'int*' to 'int')

	a = static_cast<double>(sum)/s;
	
	return a;
	
}//endcalcAvg
	
You have a two dimensional array on that line and are only specifying one dimension. That means you get a pointer to the start of the second dimension of the array, where you think you have an int. Hence the compiler complaining about int* -> int.
oh my.. don't know how i missed that. okay now its running and calculating an average. its not the right average, but its a start. thanks! =]
Topic archived. No new replies allowed.