Mar 5, 2015 at 2:22am UTC
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int rsize = 10;
const int csize = 5;
int row, col, r, c;
double sum;
ifstream In1, In2;
//========================================================================
void getdata (string stname[rsize], double stgrade[rsize][csize])
{
for (row=0; row<rsize; row++)
{
getline (In2, stname[row]);
for (col=0; col<csize; col++)
In1 >> stgrade[row][col];
}
}
//========================================================================
void total (double stgrade[rsize][csize], double stsum[rsize])
{
for (r=0; r<rsize; r++)
{
for (c=0; c<csize; c++)
stsum[r] += stgrade[r][c];
}
}
//========================================================================
void average (double stsum[rsize], double stavg[rsize])
{
for (r=0; r<rsize; r++)
stavg[r] = stsum[r] / csize;
}
//========================================================================
void stletter (double stavg[rsize], char stltr[rsize])
{
for (r=0; r<rsize; r++)
{
if (stavg[r] >= 90)
stltr[r] = 'A';
else if (stavg[r] >= 80)
stltr[r] = 'B';
else if (stavg[r] >= 70)
stltr[r] = 'C';
else if (stavg[r] >= 60)
stltr[r] = 'D';
else
stltr[r] = 'F';
}
}
//========================================================================
void classavg (double stavg[rsize], double clavg[rsize])
{
for (r=0; r<rsize; r++)
{
sum += stavg[r];
clavg[r] = sum / rsize;
}
}
//========================================================================
void printdata (string stname[rsize], double stgrade[rsize][csize], double stsum [rsize], double stavg[rsize], char stltr[rsize], double clavg[rsize])
{
cout << "NAMES GRADES TOTAL AVERAGE LETTER" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
for (row=0; row<rsize; row++)
{
cout << stname[row] << "\t";
for (col=0; col<csize; col++)
cout << stgrade[row][col] << " ";
cout << "\t";
cout << stsum[row]<< "\t";
cout << stavg[row] << "\t";
cout << " ";
cout << stltr[row] << "\t";
cout << clavg[row];
cout << endl;
cout << endl;
}
}
//========================================================================
void main()
{
In1.open("grades.txt");
In2.open("names.txt");
string stn[rsize];
double stg[rsize][csize]={0};
double sts[rsize]={0};
double stav[rsize]={0};
char stlet[rsize]={};
double clav[rsize]={0};
getdata(stn, stg);
total (stg, sts);
average (sts, stav);
stletter (stav, stlet);
classavg (stav, clav);
printdata(stn, stg, sts, stav, stlet, clav);
In1.close();
In2.close();
system("pause");
}
Mar 5, 2015 at 4:22am UTC
A few comments before your answer:
1. Use code tags ([code ][ /code]), it will make your code easier to read.
2. Avoid using global variables. Put them inside the corresponding functions. For example In1 and In2 should only appear in getdata
[code]int row, col, r, c;
double sum;
ifstream In1, In2;[/code]
Your answer: initialize sum with 0.
Mar 5, 2015 at 4:35am UTC
What do you mean by code tags [/code]? And I adjusted my code but the answer for the class average is giving me junk although i initialized double sum=0 and clavg[rsize]={0}:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int rsize = 10;
const int csize = 5;
double sum = 0;
int row, col, r, c;
ifstream In1, In2;
//========================================================================
void getdata (string stname[rsize], double stgrade[rsize][csize])
{
for (row=0; row<rsize; row++)
{
getline (In2, stname[row]);
for (col=0; col<csize; col++)
In1 >> stgrade[row][col];
}
}
//========================================================================
void total (double stgrade[rsize][csize], double stsum[rsize])
{
for (r=0; r<rsize; r++)
{
for (c=0; c<csize; c++)
stsum[r] += stgrade[r][c];
}
}
//========================================================================
void average (double stsum[rsize], double stavg[rsize])
{
for (r=0; r<rsize; r++)
stavg[r] = stsum[r] / csize;
}
//========================================================================
void stletter (double stavg[rsize], char stltr[rsize])
{
for (r=0; r<rsize; r++)
{
if (stavg[r] >= 90)
stltr[r] = 'A';
else if (stavg[r] >= 80)
stltr[r] = 'B';
else if (stavg[r] >= 70)
stltr[r] = 'C';
else if (stavg[r] >= 60)
stltr[r] = 'D';
else
stltr[r] = 'F';
}
}
//========================================================================
void classavg (double stavg[rsize], double clavg[rsize])
{
for (r=0; r<rsize; r++)
{
sum += stavg[rsize];
clavg [rsize] = sum / rsize;
}
}
//========================================================================
void printdata (string stname[rsize], double stgrade[rsize][csize], double stsum [rsize], double stavg[rsize], char stltr[rsize], double clavg[rsize])
{
cout << "NAMES GRADES TOTAL AVERAGE LETTER" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
for (row=0; row<rsize; row++)
{
cout << stname[row] << "\t ";
for (col=0; col<csize; col++)
cout << stgrade[row][col] << " ";
cout << "\t";
cout << stsum[row]<< "\t";
cout << stavg[row] << "\t";
cout << " ";
cout << stltr[row] << "\t";
cout << endl;
cout << endl;
}
cout << "--------------------------------------------------------------------------------" << endl;
cout << "The class average is: " << clavg[rsize] << endl << endl;
}
//========================================================================
void main()
{
In1.open("grades.txt");
In2.open("names.txt");
string stn[rsize];
double stg[rsize][csize]={0};
double sts[rsize]={0};
double stav[rsize]={0};
char stlet[rsize]={};
double clav[rsize]={0};
getdata(stn, stg);
total (stg, sts);
average (sts, stav);
stletter (stav, stlet);
classavg (stav, clav);
printdata(stn, stg, sts, stav, stlet, clav);
In1.close();
In2.close();
system("pause");
Mar 5, 2015 at 4:37am UTC
Every part is actually working perfectly so the Ifstream are also good, the only problem I'm facing is finding the class average, its appearing as junk.
Mar 5, 2015 at 4:52am UTC
Do I have to put the code tags before each line or after? or just once before or after?
Mar 5, 2015 at 5:00am UTC
Put [co de] before the code, and [/co de] after.
Mar 5, 2015 at 5:09am UTC
Moreover, it shows me "Run-Time Check Failure #2 - Stack around the variable 'clav' was corrupted" after debugging my code.
Mar 5, 2015 at 5:21am UTC
I JUST FIGURED OUT MY MISTAKE!!!!!!!!!! :D:D:D:D
It was:
1 2 3 4 5 6 7 8 9
void classavg (double stavg[rsize], double clavg[rsize])
{
double sum = 0;
for (r=0; r<rsize; r++)
{
sum += stavg[rsize];
clavg [rsize] = sum / rsize;
}
}
My mistake was that I put
sum += stavg[rsize] ; instead of
sum += stavg[r] ; since the loop says
r and not
rsize
So now my code is:
1 2 3 4 5 6 7 8 9
void classavg (double stavg[rsize], double clavg[rsize])
{
double sum = 0;
for (r=0; r<rsize; r++)
{
sum += stavg[r];
}
clavg[rsize] = sum / rsize;
}
IT WORKING!!
But i still have to figure out the stack flow problem. Lol Please help?
Last edited on Mar 5, 2015 at 5:27am UTC