help passing array through function!

Please help me pass line 16 through line 39 into my function! I have no idea how to do this I have tried so many unsuccessful ways!
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
#include <iostream> 
#include <iomanip>
#include <cmath>


using namespace std;

int grade(int win[5][7]);
int study = 0;
int main(){
const int ROWS = 5;
const int COLS = 7;
int table[ROWS][COLS]={{1,100,100,100,100,0,0},{2,100,0,100,0,0,0},{3,82,94,73,86,0,0},{4,64,74,84,94,0,0},{5,94,84,74,64,0,0}};
int total = grade(table);

const int hi = 5;
const int wide = 7;
const int left = 4;
const int right = 6;
const int small = 5;
const int big = 7;

int average= 0;
cout << "\tStudent" << "\tGrade1" << "\tGrade2" << " \t   Grade3" << "    Grade4" << "   Average" << " \tStandard Dev" << endl;
cout <<"________________________________________________________________________________";
for(int i = 0;i < hi;i++){
	for(int j=0;j < wide;j++){
		if(left < j < right)
			table[i][5]=(table[i][1]+table[i][2]+table[i][3]+table[i][4])/4;
		if(small < j < big)
		table [i][6]=(.3*table[i][1])+(.2*table[i][2])+(.2*table[i][3])+(.3*table[i][4]);        //0.3*grade1	+	0.2	*grade2	+	0.2	*	grade3	+	0.3 *	grade4
		
		
		cout << setiosflags(ios::fixed) << setw(10)<<table[i][j];
		
	}
	
	cout << endl;
	}



cout << "the av is " << total;





system("pause");
}

int grade(int win[5][7]){
int average= 0;
average+=(win[0][1]+win[0][2]+win[0][3]+win[0][4])/4;
return average;	
}
Last edited on
you want what !?!? @_@
I can't figure out how to put all the coding from like 16 through line 39 into the "int grade()" function on line 52... every time I try I get 1 million errors please help.
Read your compiler warnings:

D:\prog\foo>g++ -Wall -ansi -pedantic a.cpp -s -o a
a.cpp: In function 'int main()':
a.cpp:28:17: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
a.cpp:30:18: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
a.cpp:49:15: error: 'system' was not declared in this scope
a.cpp:23:5: warning: unused variable 'average' [-Wunused-variable]

Fix those errors and your program will run.
Duoas, you didn't answer my question? That has nothing to do with my question.
Are you trying to get something other than this, which is what I get after fixing those errors?:

D:\prog\foo>g++ -Wall -ansi -pedantic a.cpp -s -o a

D:\prog\foo>a
        Student Grade1  Grade2     Grade3    Grade4   Average   Standard Dev
________________________________________________________________________________         1       100       100       100       100       100       100
         2       100         0       100         0        50        50
         3        82        94        73        86        83        83
         4        64        74        84        94        79        79
         5        94        84        74        64        79        79
the av is 100

BTW, be careful with your "average", which you declare as int. Here is a change for you:

1
2
3
4
5
int grade(int win[5][7]){
	float average= 0;
	average+=(win[0][1]+win[0][2]+win[0][3]+win[0][4])/4.0;
	return average + 0.5;
}

Remember to use consistent indentation.

As to your direct question, which I missed: "[how do I put lines 16-39 into grade()?"
Don't. It doesn't belong there.

The grade function should calculate a single student's grade, and return that to the main function. It should take as argument an array of a single student's grades.

The table of student grades should only contain student grades. If number of marks given so far is important, that should also be an argument to the function, and your grade() function will have to use a loop to calculate the average instead of a single expression.

I have also already spent plenty of time answering this question for one of your classmates:
http://cplusplus.com/forum/general/64906/#msg350962

Your variable names are meaningless. Pick names that mean something. "win" and "table" and "hi" and "wide" don't tell you anything about the data they hold or the function they serve.

Your student grades table should look like this:

1
2
3
4
5
6
7
static int NUM_STUDENTS=5;
static int NUM_GRADES=7;
int all_grades[NUM_STUDENTS][NUM_GRADES] = {
	{ 90, 97, 85, 12, 99, 79, 82 },  // this student paid attention
	{ 19,  4, 23, 88, 47,  0, 26 },  // this student did not
	... // etc
};

Your grade average function should look like this:

1
2
3
4
5
6
7
int average_grade( int grades[], int count ) {
	float average=0;
	for (...) {  // you will have to figure this out yourself
		average+=...  // this too
	}
	return (average/count)+0.5;
}

Use the function to get a specific student's average grade, call it with that student's grades:

1
2
3
cout << "The bad kid's average grade is: ";
cout << average_grade( all_grades[1], NUM_GRADES );
cout << ".\n";

You can pick any student the same way:

1
2
3
cout << "The good kid's average grade is: ";
cout << average_grade( all_grades[0], NUM_GRADES );
cout << ".\n";

If you want to print out a pretty table of grade information, you should put that in its own, different function. That is, a function that calculates the average grade should not be doing anything except calculating the average grade; it should not be couting anything, etc.

If you do put it in another function, I recommend that you put the type information about the table in the global space like I suggested to your classmate. Using typedefs is optional.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.