11 Replies - 348 Views - Last Post: 07 July 2013 - 03:33 PM Rate Topic: -----

#1 coqui921  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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…
int grade;

//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 << ":";
cin >> grade;
course[j][i] = grade;
}
}	

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  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 coqui921  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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));
    int grade;

    //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 << ":";
    cin >> grade;
    course[j][i] = grade;
    }
    }	

    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?

Was This Post Helpful? -1
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • 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, :code:/>
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: question using an array

Posted 07 July 2013 - 01:31 PM

View Postcoqui921, 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
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 coqui921  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • 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
Was This Post Helpful? 0
  • +
  • -

#9 coqui921  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 coqui921  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1