I'm trying to find the class average but its displaying random numbers in a row. What's my mistake?

#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");
}
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.
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");
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.
Code tags (the button with <> on the right) helps keeping the code nice. For example, you have
void classavg (double stavg[rsize], double clavg[rsize])
{
for (r=0; r<rsize; r++)
{
sum += stavg[rsize];
clavg [rsize] = sum / rsize;
}
}
1
2
3
4
5
6
7
8
void classavg (double stavg[rsize], double clavg[rsize])
{
    for (r=0; r<rsize; r++)
    {
        sum += stavg[rsize];
        clavg [rsize] = sum / rsize;
    }
}
Do I have to put the code tags before each line or after? or just once before or after?
Put [code] before the code, and [/code] after.
Thank you. Okay so my issue starts from those lines of code. When I run the program, the class average shows me random numbers and letters mixed which is junk. Do you have any ideas to fix this? I've been working on just this part since 6 pm and getting nothing!

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
void classavg (double stavg[rsize], double clavg[rsize])
{
    double sum = 0;
	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");
}
Moreover, it shows me "Run-Time Check Failure #2 - Stack around the variable 'clav' was corrupted" after debugging my code.
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
Topic archived. No new replies allowed.