Need Help

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
#include <iostream>
#include <fstream>
using namespace std;
int i;
float x;
struct student
{
	char name[50];
	int regno;
	int phy;
	int math;
	int cs;
	int isl;
	int pak;
	int cavg;
	char cgrade;
	float avg;
	char grade;
};
void cavg (student *ptr);
int main ()
{
cout << "\t\t****CLASS REPORT****\n";
	student me[2];
	for (i=0;i<2;i++)
	{

		cout << "Student " << i+1 << ": \n";
		cout << "Name: ";
		cin >> me[i].name;
		cout << "Roll No: ";
		cin >> me[i].regno;
		cout << "Enter Subject Marks\n";
		cout << "Physics: ";
		cin >> me[i].phy;
		cout << "Math: ";
		cin >> me[i].math;
		cout << "Computer Science: ";
		cin >> me[i].cs;
		cout << "Islamiyat: ";
		cin >> me[i].isl;
		cout << "Pak Studies: ";
		cin >> me[i].pak;
		me[i].avg = (me[i].cs + me[i].isl + me[i].math + me[i].pak + me[i].phy)/500;
		cout << "Student Average is: " << me[i].avg <<endl;
		cout << me[i].avg;
	}
	student cavg (me[2]);
	return 0;
}
void cavg (student *ptr)
{
	int x=0,res;
	student me[2];
	for (i=0;i<2;i++)
	{
		x=me[i].avg+x;
	}
	res=x/2;
	cout << "Average of class: "<<res;
}

NEED HELP
Can anyone tell me why my program is not showing avg and cavg?
Hi, it is a precision problem. All the grades are stored in int variables and the division between two ints is again an int. This means that, for example, 1/2 = 0 because the value is truncated.

So the solution to your problem is a simple cast:

 
me[i].avg = (me[i].cs + me[i].isl + me[i].math + me[i].pak + me[i].phy)/(float)500;


Let me know if you solve...
EDIT - I would make some other changes to the cavg function and re-write the code this way:

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
#include <iostream>
#include <fstream>
using namespace std;
int i;
float x;
struct student {
    char name[50];
    int regno;
    int phy;
    int math;
    int cs;
    int isl;
    int pak;
    int cavg;
    char cgrade;
    float avg;
    char grade;
};
void cavg (student *ptr);
int main ()
{
    cout << "\t\t****CLASS REPORT****\n";
    student me[2];
    for (i=0; i<2; i++) {

        cout << "Student " << i+1 << ": \n";
        cout << "Name: ";
        cin >> me[i].name;
        cout << "Roll No: ";
        cin >> me[i].regno;
        cout << "Enter Subject Marks\n";
        cout << "Physics: ";
        cin >> me[i].phy;
        cout << "Math: ";
        cin >> me[i].math;
        cout << "Computer Science: ";
        cin >> me[i].cs;
        cout << "Islamiyat: ";
        cin >> me[i].isl;
        cout << "Pak Studies: ";
        cin >> me[i].pak;
        me[i].avg = (me[i].cs + me[i].isl + me[i].math + me[i].pak + me[i].phy)/(float)500;
        cout << "Student Average is: " << me[i].avg <<endl;
        cout << me[i].avg;
    }
    cavg (me);
    return 0;
}
void cavg (student *ptr)
{
    float x=0;
    float res;
    for (i=0; i<2; i++) {
        x=ptr[i].avg+x;
    }
    res=x/2;
    cout << "Average of class: "<<res;
}


Again, let me know if you solve your problems. Bye.
Thank you very much #minomic.
I have with it.
Topic archived. No new replies allowed.