# question using an array

Page 1 of 1

## 11 Replies - 513 Views - Last Post: 07 July 2013 - 03:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=324407&amp;s=d84b13d84ce3fac84ba7a7faf8466739&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 coqui921

• New D.I.C Head

Reputation: -1
• Posts: 5
• Joined: 07-July 13

# question using an array

Posted 07 July 2013 - 11:35 AM

i have a code done to calculate the show the grades for 4 students and 3 test. but know i want to calculate the average for each student, the avg. of the test scores for each test, and the avg of the student avg. I also ant it to give a letter grade after calculating the avg for the students. im not sure how to do that. i will include the code so you can see and help....
```#include <iostream>
using namespace std;

void printArray(int [][3], int, int);

void main(){
//declare a 2-d aray
//each row is for a student and each column for a grade
int course [4][3];
int columns = sizeof(course[0])/sizeof(int);
int rows = (sizeof(course)/sizeof(int))/(sizeof(cou…

//enter the grades per student(by rows in a column)
for(int i = 0; i < columns; i++){
for(int j = 0; j < rows; j++){
cout << "\nEnter the grade for test number " << i + 1 << " for student number " << j + 1 << ":";
}
}

printArray(course, rows, columns);
//int prueba [][3] = {{1,2,3},{4,5,6},{7,8,9}};
//printArray(prueba,3,3);

system("pause");
}

void printArray( int anArray[][3], int rows, int cols){
cout << "Student\tTest 1\tTest 2\tTest 3\n";
for(int i = 0; i < rows; i++){
cout << i + 1 << "\t";
for(int j = 0; j < cols; j++){
cout << anArray[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}

```

the range for the letter avg are:
A = 90 - 100
B = 80 - 89
C = 70 - 79
D = 60 - 69
F = 0 - 59

here is an example of what the program shows now:

Student Test1 Test2 Test3
1 100 98 97
2 100 97 98
3 100 98 99
4 100 97 99

here is what i want it to do:
```Student 		Test1	 Test2	 Test3	 Student Avg	 Letter Grade
1 			100	 98	 97	  98 			A
2 			100 	 97	 98 	  98 			A
3 			100 	 98	 99 	  99 			A
4 			100 	 97	 99	  98			A
Test Avg.  		100	 97	 98	  98

```

This post has been edited by macosxnerd101: 07 July 2013 - 11:36 AM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: question using an array

### #2 jimblumberg

Reputation: 4591
• Posts: 14,499
• Joined: 25-December 09

## Re: question using an array

Posted 07 July 2013 - 11:53 AM

A couple of things to start, first did you use some kind of Word Processor with this code? It has several "special" codes that the compiler doesn't like. Second main() must be defined to return an int and you should return an int from this function:
```int main()
{

return 0;
}
```

Next please explain the purpose of these lines:

```int columns = sizeof(course[0])/sizeof(int);
int rows = (sizeof(course)/sizeof(int))/(sizeof(cou…
```

And those "special characters" are contained in the second line above.

What exactly are you having problems with, the format or adding the letter grade.

Lastly, but very important none the less, find an indentation style you like and use it consistently. This will make your program much easier to read.

Jim

This post has been edited by jimblumberg: 07 July 2013 - 11:54 AM

### #3 coqui921

• New D.I.C Head

Reputation: -1
• Posts: 5
• Joined: 07-July 13

## Re: question using an array

Posted 07 July 2013 - 01:09 PM

here is the code again:
```    #include <iostream>
using namespace std;

void printArray(int [][3], int, int);

void main(){
//declare a 2-d aray
//each row is for a student and each column for a grade
int course [4][3];
int columns = sizeof(course[0])/sizeof(int);
int rows = (sizeof(course)/sizeof(int))/(sizeof(course[0])/sizeof(int));

//enter the grades per student(by rows in a column)
for(int i = 0; i < columns; i++){
for(int j = 0; j < rows; j++){
cout << "\nEnter the grade for test number " << i + 1 << " for student number " << j + 1 << ":";
}
}

printArray(course, rows, columns);
//int prueba [][3] = {{1,2,3},{4,5,6},{7,8,9}};
//printArray(prueba,3,3);

system("pause");
}

void printArray( int anArray[][3], int rows, int cols){
cout << "Student\tTest 1\tTest 2\tTest 3\n";
for(int i = 0; i < rows; i++){
cout << i + 1 << "\t";
for(int j = 0; j < cols; j++){
cout << anArray[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}

```

the range for the letter avg are:
A = 90 - 100
B = 80 - 89
C = 70 - 79
D = 60 - 69
F = 0 - 59

here is an example of what the program shows now:
```    Student	 Test1	 Test2	 Test3
1 		   100 	   98 	  97
2 		   100 	   97     98
3 		   100     98 	  99
4 		   100 	   97 	  99

```

here is what i want it to do:
```    Student 		Test1	 Test2	 Test3	 Student Avg	 Letter Grade
1 			100		 98		 97			98 			A
2 			100 		 97		 98		 	98 			A
3 			100 		 98		 99		 	99 			A
4 			100 		 97		 99		 	98			A
Test Avg.  		100		 97		 98			98

```

I don't need a return value on main() because i have it as a void and that does not return a value.
here are the 2 lines you had a doubt on:

int columns = sizeof(course[0])/sizeof(int);
int rows = (sizeof(course)/sizeof(int))/(sizeof(course[0])/sizeof(int));

This post has been edited by Skydiver: 07 July 2013 - 01:27 PM
Reason for edit:: Put code in code tags. What did you not understand from the edit of your first post?

### #4 AKMafia001

• </code.in.dream>

Reputation: 234
• Posts: 724
• Joined: 11-June 11

## Re: question using an array

Posted 07 July 2013 - 01:28 PM

Quote

I don't need a return value on main() because i have it as a void and that does not return a value.

Whether you like it or not, your main() should must return an int...

Quote

here is what i want it to do:

Student Test1 Test2 Test3 Student Avg Letter Grade
1 100 98 97 98 A
2 100 97 98 98 A
3 100 98 99 99 A
4 100 97 99 98 A
Test Avg. 100 97 98 98

Have you tried calculating this manually using a calculator? What formulas did you use?

And Please always wrap your code in the code tags like, />

### #5 Skydiver

• Code herder

Reputation: 4021
• Posts: 12,816
• Joined: 05-May 12

## Re: question using an array

Posted 07 July 2013 - 01:31 PM

coqui921, on 07 July 2013 - 04:09 PM, said:

I don't need a return value on main() because i have it as a void and that does not return a value.

The C and C++ standards requires that main() return an int. See: http://www.parashift...eturns-int.html and http://faq.cprogramm...3&id=1043284376

### #6 jimblumberg

Reputation: 4591
• Posts: 14,499
• Joined: 25-December 09

## Re: question using an array

Posted 07 July 2013 - 02:13 PM

You still haven't answered the question about those two lines. What is the purpose of those lines?

Plus you didn't answer the question about what exactly you're trying to accomplish.

Do you have a problem formating the output as you desire? Or do you have a problem computing the letter grades.

Jim

### #7 coqui921

• New D.I.C Head

Reputation: -1
• Posts: 5
• Joined: 07-July 13

## Re: question using an array

Posted 07 July 2013 - 02:26 PM

i have a problem finding the averages of the test scores and the letter grades

This post has been edited by GunnerInc: 07 July 2013 - 07:13 PM
Reason for edit:: Quote be gone

### #8 jimblumberg

Reputation: 4591
• Posts: 14,499
• Joined: 25-December 09

## Re: question using an array

Posted 07 July 2013 - 03:02 PM

What about the other question?

What is the purpose of these lines?
```int columns = sizeof(course[0])/sizeof(int);
int rows = (sizeof(course)/sizeof(int))/(sizeof(cou…

```

You also need to properly indent your code so I can easily read it. As presented your code takes too much to read.

Jim

### #9 coqui921

• New D.I.C Head

Reputation: -1
• Posts: 5
• Joined: 07-July 13

## Re: question using an array

Posted 07 July 2013 - 03:16 PM

those 2 lines state the size of the columns and rows in the 2D array

This post has been edited by GunnerInc: 07 July 2013 - 07:13 PM
Reason for edit:: Quote be gone

### #10 jimblumberg

Reputation: 4591
• Posts: 14,499
• Joined: 25-December 09

## Re: question using an array

Posted 07 July 2013 - 03:22 PM

Then why do you even need them? You already know the size of your array since you used the constants. Why not just create a couple of const variables instead.

Something like:
```
const int columns = 4;
const int rows = 3;
int course [columns][rows];

```

Jim

### #11 coqui921

• New D.I.C Head

Reputation: -1
• Posts: 5
• Joined: 07-July 13

## Re: question using an array

Posted 07 July 2013 - 03:25 PM

okay i will give that a try but how do i calculate the average of the test scores?

This post has been edited by GunnerInc: 07 July 2013 - 07:13 PM
Reason for edit:: Quote be gone

### #12 jimblumberg

Reputation: 4591
• Posts: 14,499
• Joined: 25-December 09

## Re: question using an array

Posted 07 July 2013 - 03:33 PM

Quote

okay i will give that a try but how do i calculate the average of the test scores?

Post your correctly formatted current code, then I'll try to actually read the program.

Jim