C++/C++ arrays/vector
Expert: vijayan - 11/19/2010
QuestionHello, i have a program here that is almost finished. But i
am in need to implement an array or vector for processing.
- "3. The program must utilize one array or vector for
processing – not just output. I would suggest reading the
assignment scores into an array."
that is the last requirement of the program that i need to
put in but i have no idea how to.
#include <iostream>
#include <stdlib.h>
#include <fstream>
//Student Name-Aris Lara
using namespace std;
int f_assignmentavg (int a, int b, int c, int d)
{
int aavg;
aavg=(a+b+c+d)/4;
return (aavg);
}
int f_testassigmentavg (int a, int b, int c)
{
int tstavg;
tstavg = (a+b+c) /3;
return (tstavg);
}
int f_checkpassfail (int att, int testavg)
{ bool pass = true;
if (att < 30)
{pass = false; cout << "failed [attendance]" ; }
else
if (testavg <60)
{pass = false ; cout << "failed [grades] " ;}
return (pass);
}
int main(int argc, char *argv[])
{
string lastname, name, phone, pref;
int test1, test2, count = 0;
int a1, a2, a3 , a4;
int attendance;
int assignment_avg, test_assignment_avg;
ifstream myfile ("grades.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
//while (count < 4)
{ //count +=1;
{
// while( myfile >> line ) {
myfile >> name;
cout << "First Name: " << name << endl; //}
myfile >> lastname;
cout << "Last Name: " << lastname << endl;
myfile >> test1;
cout << "Test 1: " << test1 << endl;
myfile >> test2 ;
cout << "Test 2: " << test2 << endl;
myfile >> a1 ;
cout << "Assigment 1: " << a1 <<endl;
myfile >> a2 ;
cout << "Assigment 2: " << a2 <<endl;
myfile >> a3 ;
cout << "Assigment 3: " << a3 <<endl;
myfile >> a4 ;
cout << "Assigment 4: " << a4 <<endl;
myfile >> attendance ;
cout << "Attendance: " << attendance << endl;
// replace this code with function
//assignment_avg = (a1 + a2 + a3 + a4 ) / 4 ;
//cout << "Assignment Average : " << assignment_avg <<
endl ;
assignment_avg = f_assignmentavg(a1,a2,a3,a4);
//replace this code with function
//test_assignment_avg = ( assignment_avg + test1 +
test2 ) / 3 ;
test_assignment_avg = f_testassigmentavg
(assignment_avg, test1 , test2 );
cout << "Average : " << test_assignment_avg << endl;
//home work output
cout << endl << endl << "--- Assignment output ----"
<< endl;
cout << name << " " << lastname << endl;
if (f_checkpassfail (attendance, test_assignment_avg) )
cout << "[pass good grades & Attendance] ";
if (test_assignment_avg > 90) cout << "A" << endl;
else
if (test_assignment_avg >80) cout << "B" << endl;
else
if (test_assignment_avg > 70 ) cout << "C"
<<endl ;
else
if (test_assignment_avg >60) cout << "D" <<endl
;
else
if (test_assignment_avg <50) cout << "F" <<endl
;
cout << endl;
cout << "---------------------------------------------
---------------------- " << endl;
}
}
myfile.close();
}
else cout << "Unable to open file";
system("PAUSE");
return 0;
}
Answer> " I would suggest reading the assignment scores into an array"
Since the number of assignments (four) is a constant known at compile time, an array would do.
a. Define an array for reading the assignment scores.
replace
int a1, a2, a3 , a4;
with
enum { NUM_ASSIGNMENTS = 4 } ;
int a[NUM_ASSIGNMENTS] ;
b. Read the scores into the array
Instead of
myfile >> a1 ;
cout << "Assigment 1: " << a1 <<endl;
// a2, a3, a4 etc
Write a loop to read the assignment values into the array:
for( int i=0 ; i<NUM_ASSIGNMENTS ; ++i )
{
myfile >> a[i] ;
cout << "Assigment " << i << ": " << a[i] <<endl;
}
c. Use a[0], a[1], a[2], a[3] instead of a1, a2, a3, a4 in the rest of the program.