ggavinmoss

Diamond Member
Apr 20, 2001
4,798
1
0
Two options are to save the info in a data structure of some sort, or iterate through the file 5 or more times -- which is lame.

-geoff
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Here's my version of the program:

--------------------------------------------------------------------------------------------------

@info = &file_array("C:/scores2.dat");
foreach $i(@info){
chomp($i);
my $average = 0;
my @student = split(/ /, $i);
for(my $j=1;$j<=$#student;$j++){
$average += $student[$j];
}
$average /= $#student;
my $grade ='';
if($average>=90){$grade = 'A'}
elsif($average>=80){$grade = 'B'}
elsif($average>=70){$grade = 'C'}
elsif($average>=60){$grade = 'D'}
else{$grade = 'F'}
print "Average for student #$student[0] is $average\% and grade is: $grade\n";
}

sub file_array{
open(FILE, "$_[0]");
my @array = <FILE>;
close (FILE);
return @array;
}

--------------------------------------------------------------------------------------------------

It's not written in C++, but that's the entire program, in Perl :)

edited: didn't see that you needed the letter grades too, until now... so I added them :)

 

satori

Senior member
Nov 2, 1999
471
0
0
notfred: Argh... I >hate< looking at Perl. One of my roommate's is a PERL-fiend and whenever I try to decipher the stuff he writes, my head starts hurting like a mofo. :)

I still can't believe how much you guys make, though. :)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<< whoa that just confused me some more :)
not on arrays yet. yeah the prof. wants in in c++ :)
>>



you have to admit, it took a lot less typing, though :)
 

holden j caufield

Diamond Member
Dec 30, 1999
6,324
10
81
this is what I got am I on the right track. :)

ex1Avg=0;
sumEx1=0;
count=1;
myFile>>ID>>exam1>>exam2>>exam3>>exam4>>exam5;
while(myFile)
{

sumEx1=sumEx1+exam1;
ex1Avg=sumEx1/count;
count++;
myFile>>ID>>exam1>>exam2>>exam3>>exam4>>exam5;



}

cout<<setprecision(4)<<setw(9)<<"Average"<<setw(7)<<showpoint<<ex1Avg;

 

amishman

Member
Aug 1, 2001
66
0
0
Hi,
how about you declare another set of variables along with the original score variables you declare right after void main?
I copied your code but just added 4 blocks of code. The first block declares six new variables, 5 to act as sums of all the column scores and a counter to count the number of times you have been through the loop (ie the number of student records).

The 2nd block of code sets all the sum variables to the first record scores and the counter to one.

The 3rd block - each time you go through the while loop you read in a record, add the sums up and add one to the counter.


the 4th block - Finallly I displayed the column scores.

It seems like it should work. Hell I hope it does after 5 semesters of C++.




int main ()
{
int ID; //Variable Declarations
int exam1;
int exam2;
int exam3;
int exam4;
int exam5;
float avg;
char grade;
float ex1Avg;
float ex2Avg;
float ex3Avg;
float ex4Avg;
float ex5Avg;


//MY NEW INTEGER DECLARATIONS
int exam1Total;
int exam2Total;
int exam3Total;
int exam4Total;
int exam5Total;
int myCounter;
///////////////////////////////

ifstream myFile;



cout<<setw(9)<<"ID"<<setw(7)<<"Ex 1"<<setw(7)<<"Ex 2"<<setw(7)<<"Ex 3";
cout<<setw(7)<<"Ex 4"<<setw(7)<<"Ex 5"<<setw(7)<<"Avg"<<setw(8)<<"Grade\n";



myFile.open("c:\\scores2.dat");
if(!myFile)
{
cout<<"File Not Found!\n";

return 1;
}



exam1=0;
exam2=0;
exam3=0;
exam4=0;
exam5=0;
avg=0;
grade=0;
exam1=0;

myFile>>ID>>exam1>>exam2>>exam3>>exam4>>exam5;


//////// IADDED THIS TOO
exam1Total = exam1;
exam2Total = exam2;
exam3Total = exam3;
exam4Total = exam4;
exam5Total = exam5;
myCounter = 1;
////////////////////////////////




while(myFile)
{
avg = (exam1+exam2+exam3+exam4+exam5)/5.00;

//////AGAIN I ADDED THIS CODE
exam1Total += exam1;
exam2Total += exam2;
exam3Total += exam3;
exam4Total += exam4;
exam5Total += exam5;
++myCounter;
///////////////////////




if(avg<=100.0&&avg>=90.00)
{
grade = 'A';
}

else if(avg<90.00&&avg>=80.00)
{
grade = 'B';
}

else if(avg<80.00&&avg>=70)
{
grade = 'C';
}

else if(avg<70&&avg>=60)
{
grade = 'D';
}
else
{
grade = 'F';
}

cout<<setprecision(4)<<showpoint<<setw(9)<<ID<<setw(7)<<exam1;
cout<<setw(7)<<exam2<<setw(7)<<exam3<<setw(7)<<exam4<<setw(7)<<exam5;
cout<<setw(7)<<avg<<setw(7)<<grade<<endl;

myFile>>ID>>exam1>>exam2>>exam3>>exam4>>exam5;

}



////MY FINAL ADDITION TO YOUR CODE
cout << "Exam 1 average is: " << exam1Total / myCounter << endl;
cout << "Exam 2 average is: " << exam2Total / myCounter << endl;
cout << "Exam 3 average is: " << exam3Total / myCounter << endl;
cout << "Exam 4 average is: " << exam4Total / myCounter << endl;
cout << "Exam 5 average is: " << exam5Total / myCounter << endl;
/////////////////////////////////

cout<<endl;

return 0;

}
 

holden j caufield

Diamond Member
Dec 30, 1999
6,324
10
81
thanks for the help. it seems like a valid idea but I get quite a few errors when I build it. I'll work on it thanks again. :)