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?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
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<<left<<fixed<<showpoint<<setprecision(2); //SHOW DECIMAL POINTS
      
      //EQUATIONS
      progavg= (progscore[3])/(3);
      testavg= (testscore[3])/(3);
      courseavg= (progscore[3]+testscore[3])/(6);





    system("pause");    
return 0;
} //END OF MAIN

//******************************************************************************
void headerfn(){
     cout<<"************************************************************\n";
     cout<<"                   PUT A HEADER HERE                       *\n";
     cout<<"************************************************************\n";

     }//END OF HEADERFN                              
//******************************************************************************
void inputfn(string firstname [7], string lastname [7], float progscore[21], float testscore[21]){
     for(int row=0; row<7; row++) fin>>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<<left<<setw(10)<<firstname[row]<<" "<<lastname[row]
											<<setw(10)<<progavg[row]
											<<setw(10)<<testavg[row]
											<<setw(10)<<courseavg[row]
											<<setw(10)<<lettergrade[row]<<endl;
}//END OF OUTPUTFN
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.

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
57
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

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:
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
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

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



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
01	#include <iostream>
02	#include <iomanip>
03	#include <fstream>
04	#include <string>
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"<<endl;
64	cout<<"Examine the input text file (input5.txt) before you continue\n"<<endl;
65	}//END OF HEADERFN                             
66	//******************************************************************************
67	void inputfn(string firstname [7], string lastname [7], int progscore1 [7], int progscore2[7], int progscore3 [7],
68	int testscore1[7], int testscore2 [7], int testscore3 [7]){
69	 
70	for(int row=0; row<7; row++) fin>>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<<left<<fixed<<showpoint<<setprecision(2); //SHOW DECIMAL POINTS
79	      progavg = (progscore1+progscore2+progscore3)/(3);
80	      testavg = (testscore1+testscore2+testscore3)/(3);
81	    courseavg = (progavg+testavg)/(2);
82	 
83	}//END OF AVERAGEFN
84	 
85	 
86	//******************************************************************************
87	void outputfn(string firstname[7], string lastname[7], float progavg[7], float testavg[7], float courseavg[7], char lettergrade[7]){
88	 
89	for(int row=0; row<7; row++) cout<<left<<setw(10)<<firstname[row]
90	                                          <<setw(10)<<lastname[row]
91	                                          <<setw(5)<<progavg[row]
92	                                          <<setw(5)<<testavg[row]
93	                                          <<setw(5)<<courseavg[row]
94	                                          <<setw(5)<<lettergrade[row]<<endl;
95	}//END OF OUTPUTFN
96	//***************************************************************************** 
These lines aren't correct.
79
80
81
    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.
79
80
    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


1
2
3
4
      cout<<left<<fixed<<showpoint<<setprecision(2); //SHOW DECIMAL POINTS
      progavg[row] = (progscore1[row]+progscore2[row]+progscore3[row])/(3);
      testavg[row] = (testscore1[row]+testscore2[row]+testscore3[row])/(3);
    courseavg[row] = (progavg[row]+testavg[row])/(2);


The entire function looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//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<<left<<fixed<<showpoint<<setprecision(2); //SHOW DECIMAL POINTS
      for(int row=0; row<7; row++) progavg[row] = (progscore1[row]+progscore2[row]+progscore3[row])/(3);
      for(int row=0; row<7; row++) testavg[row] = (testscore1[row]+testscore2[row]+testscore3[row])/(3);
      for(int row=0; row<7; row++) courseavg[row] = (progavg[row]+testavg[row])/(2);
    

}//END OF AVERAGEFN


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

1
2
3
4
5
6
7
8
9
10
11
12
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.