### Using arrays for math calculations

Hi Everyone, I have to use an input.txt file to read a student's name three program scores and three test scores. From there I have to output their name, program average, test average, course average (program/test combined), and letter grade.

I've done several variations of this problem but recently we learned arrays. It seems I am a little lost regarding perform calculations with arrays rather then specific variables.

I have my equations listed in main because sometimes when I'm trying something new I test it out in main, then I create the function.

My input file looks like the below:

Donald Duck 90 98 29 59 99 100
The first three scores are program scores, the next three are test scores. I setup my arrays for program/test scores as 21 as there is a total of seven people. (3 exam scores * 7 people = 21).

I must use one dimensional arrays in this problem.

Also previously when combinding a string I would use as follow:

`string fullname=firstname+" "+lastname;`

However, that doesnt seem to be supported in arrays? Is that correct?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798`` ``````#include #include #include #include using namespace std; //GLOBAL VARIABLES ifstream fin; ofstream fout; //FUNCTION PROTOTYPES void headerfn(); void inputfn(string firstname [7], string lastname [7], float progscore[21], float testscore[21]); //char gradefn(float courseavg [7]); void outputfn(string firstname [7], string lastname [7], float progavg [7], float testavg [7], float courseavg[7], char lettergrade [7]); int main (){ //START OF MAIN system("color f0"); fin.open ("input5.txt"); //OPENING INPUT FILE fout.open("output5.txt");//OPENING OUTPUT FILE if(!fin){ cout<<"INPUT FILE ERROR!\n"; system("pause"); return 1; }//END OF INPUT FILE CHECK if(!fout){ cout<<"OUTPUT FILE ERROR\n"; system("pause"); return 1; }//END OF OUTPUT FILE CHECK //VARIABLES string firstname [7], lastname [7]; float progscore [7], testscore [7], progavg [7], testavg [7], courseavg [7]; char lettergrade [7]; //MAIN FUNCTION CALLS headerfn(); inputfn(firstname, lastname, progscore, testscore); //gradefn(courseavg); outputfn(firstname, lastname, progavg, testavg, courseavg, lettergrade); cout<>firstname[row]>>lastname[row]>>progscore[row]>>testscore[row]; }//END OF INPUTFN //****************************************************************************** /*char gradefn(float courseavg){ char lettergrade; if(courseavg>=90)(lettergrade='A'); else if(courseavg>=80)(lettergrade='B'); else if(courseavg>=70)(lettergrade='C'); else lettergrade='F'; }//END OF GRADEFN */ //****************************************************************************** void outputfn(string firstname [7], string lastname [7], float progavg [7], float testavg [7], float courseavg[7], char lettergrade [7]){ for(int row=0; row<7; row++) cout<
Last edited on
I'm always wary of a design which assumes that the number of lines in the input file is known in advance. My preferred approach would be to allocate an array which is more than large enough (or better, use a vector) and then count how many rows were actually read from the file.

As for the data, the student scores can be handled using a 2-dimensional array, so instead of 21 elements, there are 7 sets of 3 elements.

Here's one approach, I've only covered getting the input here. The number of rows read is returned to the calling function, and should be passed as a parameter to the other functions where needed.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include #include #include #include using namespace std; const int asize = 20; //GLOBAL VARIABLES ifstream fin; //FUNCTION PROTOTYPES int inputfn(string firstname[], string lastname[], float progscore[][3], float testscore[][3]); int main () { fin.open ("d:\\temp\\input5.txt"); if (!fin) { cout<<"INPUT FILE ERROR!\n"; system("pause"); return 1; } // VARIABLES string firstname [asize], lastname [asize]; float progscore [asize][3], testscore [asize][3], progavg [asize], testavg [asize], courseavg [asize]; char lettergrade [asize]; int numStudents = inputfn(firstname, lastname, progscore, testscore); system("pause"); return 0; } //****************************************************************************** int inputfn(string firstname[], string lastname[], float progscore[][3], float testscore[][3]) { int row = 0; string line; while (getline(fin, line)) { istringstream ssin(line); ssin >> firstname[row] >> lastname[row]; ssin >> progscore[row][0] >> progscore[row][1] >> progscore[row][2]; ssin >> testscore[row][0] >> testscore[row][1] >> testscore[row][2]; row++; } return row; }``````

However, rather than having multiple individual arrays, a struct could be used to contain all of the data for an individual student, and then just a single array will be required. Example:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include #include #include #include using namespace std; const int asize = 20; struct Student { string firstname; string lastname; float progscore[3]; float testscore[3]; float progavg; float testavg; float courseavg; char lettergrade; }; //GLOBAL VARIABLES ifstream fin; //FUNCTION PROTOTYPES int inputfn(Student students[]); int main () { fin.open ("d:\\temp\\input5.txt"); if (!fin) { cout<<"INPUT FILE ERROR!\n"; system("pause"); return 1; } // VARIABLES Student students[asize]; int numStudents = inputfn(students); system("pause"); return 0; } //****************************************************************************** int inputfn(Student students[]) { int row = 0; string line; while (getline(fin, line)) { istringstream ssin(line); ssin >> students[row].firstname >> students[row].lastname; ssin >> students[row].progscore[0] >> students[row].progscore[1] >> students[row].progscore[2]; ssin >> students[row].testscore[0] >> students[row].testscore[1] >> students[row].testscore[2]; row++; } return row; }``````
Last edited on
I really appreciate your help however, I am only suppose to use a one dimensional array in this problem.
Well, the output requirements of the project don't seem to include the individual test scores, if I'm reading/understanding correctly.

So you don't actually need to store them. The values still need to be read in from the file, but rather than storing everything in the array, instead read the three scores into temporary variables, calculate the average, and store that result in the array. (Do that for both program and test scores).

That actually makes the array requirement simpler, however it means that some of the calculation needs to be done at the time of reading the input.

 `string fullname=firstname+" "+lastname;` However, that doesnt seem to be supported in arrays? Is that correct?

No, there is no difference, other than the need to specify the appropriate subscript to use the correct element of the array.
Last edited on
Hi Thank you so much! I managed to output each of the names and scores correctly. However, back to the equations it seems I may have them setup incorrect?

Specifically lines 75-82

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````01 #include 02 #include 03 #include 04 #include 05 using namespace std; 06 07 //GLOBAL VARIABLES 08 ifstream fin; 09 ofstream fout; 10 11 //FUNCTION PROTOTYPES 12 void headerfn(); 13 void inputfn(string firstname [7], string lastname [7], int progscore1 [7], int progscore2[7], int progscore3 [7], 14 int testscore1[7], int testscore2 [7], int testscore3 [7]); 15 void outputfn(string firstname[7], string lastname[7], float progavg[7], float testavg[7], float courseavg[7], char lettergrade[7]); 16 void averagefn(int progscore1 [7], int progscore2 [7], int progscore3 [7], int testscore1 [7], int testscore2 [7], int testscore3 [7], 17 float progavg [7], float testavg [7], float courseavg [7]); 18 19 int main (){ //START OF MAIN 20 system("color f0"); 21 22 23 fin.open ("input5.txt"); //OPENING INPUT FILE 24 fout.open("output5.txt");//OPENING OUTPUT FILE 25 26 if(!fin){ 27 cout<<"INPUT FILE ERROR!\n"; 28 system("pause"); 29 return 1; 30 }//END OF INPUT FILE CHECK 31 32 if(!fout){ 33 cout<<"OUTPUT FILE ERROR\n"; 34 system("pause"); 35 return 1; 36 }//END OF OUTPUT FILE CHECK 37 38 39 //VARIABLES 40 string firstname [7], lastname [7]; 41 int progscore1 [7], progscore2 [7], progscore3 [7], 42 testscore1[7], testscore2[7], testscore3 [7]; 43 float progavg[7], testavg[7], courseavg[7]; 44 char lettergrade[7]; 45 46 47 //MAIN FUNCTION CALLS 48 headerfn(); 49 inputfn(firstname, lastname, progscore1, progscore2, progscore3, testscore1, testscore2, testscore3); 50 outputfn(firstname, lastname, progavg,testavg, courseavg, lettergrade); 51 void averagefn(progscore1, progscore2, progscore3, testscore1, testscore2, testscore3, progavg, testavg, courseavg); 52 53 system("pause"); 54 return 0; 55 } //END OF MAIN 56 57 //****************************************************************************** 58 void headerfn(){ 59 cout<<"************************************************************\n"; 60 cout<<"* HEADER *\n"; 61 cout<<"************************************************************\n"; 62 63 cout<<"Welcome to the Student Grade Caluclator IV\n"<>firstname[row]>>lastname[row]>>progscore1[row]>>progscore2[row]>>progscore3[row]>> 71 testscore1[row]>>testscore2[row]>>testscore3[row]; 72 73 }//END OF INPUTFN 74 //****************************************************************************** 75 void averagefn(int progscore1 [7], int progscore2 [7], int progscore3 [7], int testscore1 [7], int testscore2 [7], int testscore3 [7], 76 float progavg [7], float testavg [7], float courseavg [7]){ 77 78 cout<
These lines aren't correct.
 ``798081`` `````` progavg = (progscore1+progscore2+progscore3)/(3); testavg = (testscore1+testscore2+testscore3)/(3); courseavg = (progavg+testavg)/(2);``````

You need to process the values for each student one at a time.
 ``7980`` `````` progavg[row] = float(progscore1[row] + progscore2[row] + progscore3[row])/3; // etc ``````
Last edited on
Hum...I tried that but I'm getting an error " 'row' undeclared (first use of function). I put them in a for loop

 ``1234`` `````` cout<

The entire function looks like this:

 ``1234567891011121314151617181920`` ``````//Prototype void averagefn(int progscore1 [7], int progscore2 [7], int progscore3 [7], int testscore1 [7], int testscore2 [7], int testscore3 [7], float progavg [7], float testavg [7], float courseavg [7]); //Function Call averagefn(progscore1, progscore2, progscore3, testscore1, testscore2, testscore3, progavg, testavg, courseavg); //Function Definition void averagefn(int progscore1 [7], int progscore2 [7], int progscore3 [7], int testscore1 [7], int testscore2 [7], int testscore3 [7], float progavg [7], float testavg [7], float courseavg [7]){ cout<

However, it isnt outputting anything valuable it just looks like this:

 ``123456789101112`` ``````Welcome to the Student Grade Caluclator IV Examine the input text file (input5.txt) before you continue Snow White 2.8026e-0451.41427e-0381.77965e-043 Jack Frost 3.76484e-0391.41431e-0380 Sleeping Beauty 3.65985e+0331.41509e-0381.00977e-028 Prince Charming 4.2039e-0450 1.41568e-038 Rapunzel Repunzel -1.#QNAN1.41431e-0381.4013e-045 Santa Claus 2.8026e-0451.16416e-0101.4151e-038 Miss Horner 3.57453e+0332.35099e-0381.41431e-038Ü Press any key to continue . . .``````
Last edited on
Take a look at line 89:
`for (int row=0; row<7; row++)`
That's a for loop.
You need a loop like that in function `averagefn()`

Also beware of integer division which truncates the decimal portion.
Note the example I posted previously has float(.....) in order to force a floating point divide.
Last edited on
Haha my bad Chervil, I think I edited my response too soon after I posted it. I did add the for loop but the output it just random characters (see last response)
In main() you are first creating the output, and after that calculating the averages. That's the wrong order.
God this class will be the death of me hahaha. Thank you so much I got it now.
Topic archived. No new replies allowed.