two-dimensional arrays and function help

If my explanation isn't clear enough read #9
http://books.google.com/books?id=LUJbzQR7KnoC&pg=PA322&lpg=PA322&dq=0.3*grade1%09%2B%090.2%09*grade2%09%2B%090.2%09*%09grade3%09%2B%090.3+*%09grade4+c%2B%2B&source=bl&ots=1JL47t2dCP&sig=I_hioqOBa_T6DqkXznAGfziROFE&hl=en&sa=X&ei=wBVmT8PHFcazsgL408C2Dw&ved=0CC8Q6AEwAQ#v=onepage&q&f=false
Hello I am attempting to pass a two-dimensional array 60 rows by 7 columns through a function, the purpose of this program is to calculate student grades. In the first column of every row it should be numbered 1-60 example shown below
1
2
3
4
5
6
ect... all the way to 60..

the next 4 rows allow for a user input and the last 2 output totals

I have no idea what I am doing wrong and I don't really know where to start please help me....

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
#include <iostream> 
#include <iomanip>

using namespace std;

int grade(int win[][7], int num);

int main(){
const int ROWS = 60;
const int COLS = 7;
int table[ROWS][COLS];
int total = grade(table[ROWS][7], 7);


}

int grade(int win[][7], int num){
	int sum = 0;
	for(int i = 0; i<num;i++){
	sum+=win[i][7];

	return sum;
	}

}

Last edited on
You are not calling the function properly:

12
13
int total = grade(table,ROWS);

In C++ all arrays start at zero. You can pretend it is something different to the user, if you want, but in your program just know that your array ranges from table[0][0] to table[59][6].

Be careful with your indenting and where you stick return statements. (Line 22 should not be inside the loop.)

Also, your variable names stink. Name them for what they are.

You can use global type information to help out here:

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

static const int COLS=7;
static const int ROWS=60;
typedef int all_students_grades_t[ROWS][COLS];

int average_grade(all_students_grades_t grades,int student)
{
  float sum=0;
  for (int i=0; i<COLS; i++)
  {
    sum+=grades[student][i];
  }
  return (sum/COLS)+0.5;
}

int main()
{
  all_students_grades_t all_grades=
  {
    { 90, 97, 85, 12, 99, 79, 82 },  // this student paid attention
    { 19,  4, 23, 88, 47,  0, 26 },  // this student did not
    { 0 }  // there are no more students
  };

  cout<<"Good student's grade is "<<average_grade(all_grades,0)<<endl;
  cout<<"Lazy student's grade is "<<average_grade(all_grades,1)<<endl;

  return 0;
}

I recommend that you only pass the subarray that you are interested in to the average_grade() function. (This requires you to split the global type information up):

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

static const int COLS=7;
typedef int one_students_grades_t[COLS];

static const int ROWS=60;
typedef one_students_grades_t  all_students_grades_t[ROWS];

int average_grade(one_students_grades_t grades)
{
  float sum=0;
  for (int i=0; i<COLS; i++)
  {
    sum+=grades[i];
  }
  return (sum/COLS)+0.5;
}

int main()
{
  all_students_grades_t all_grades=
  {
    { 90, 97, 85, 12, 99, 79, 82 },  // this student paid attention
    { 19,  4, 23, 88, 47,  0, 26 },  // this student did not
    { 0 }  // there are no more students
  };

  cout<<"Good student's grade is "<<average_grade(all_grades[0])<<endl;
  cout<<"Lazy student's grade is "<<average_grade(all_grades[1])<<endl;

  return 0;
}

You can also use templates to help out, but I don't think your professor would like to see this from you yet:

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

template <size_t Cols>
int average_grade(int(&student_grades)[Cols])
{
  float sum=0;
  for (size_t i=0; i<Cols; i++)
  {
    sum+=student_grades[i];
  }
  return (sum/Cols)+0.5;  // be careful where you put this line. You had it inside the loop...
}

int main()
{
  int all_grades[60][7]=
  {
    { 90, 97, 85, 12, 99, 79, 82 },  // this student paid attention
    { 19,  4, 23, 88, 47,  0, 26 },  // this student did not
    { 0 }  // there are no more students
  };

  cout<<"Good student's grade is "<<average_grade(all_grades[0])<<endl;
  cout<<"Lazy student's grade is "<<average_grade(all_grades[1])<<endl;

  return 0;
}

Hope this helps.
Topic archived. No new replies allowed.