You need to initialize variables before using them. What value does average have when you declare it?
Quzah.
You need to initialize variables before using them. What value does average have when you declare it?
Quzah.
Hope is the first step on the road to disappointment.
ok this is my newest code.. still.. the average is still not the correct answer.. plz help meCode:#include<stdio.h> void readinput(int i); void writeoutput(int i); typedef struct { char matrix[30]; char name[30]; int age; char sex[6]; } record; record students[10]; main(void) { int i, n, maleaverage, femaleaverage,malesum = 0, femalesum = 0, mcount = 0, fcount = 0; printf("how many student"); scanf("%d", &n); for (i = 0; i < n; ++i) { readinput(i); } for (i = 0; i < n; ++i) { if (strcmp(students[i].sex, "male") == 0) malesum = malesum + students[i].age; ++mcount; if (strcmp(students[i].sex, "female") == 0) femalesum += students[i].age; ++fcount; } maleaverage = malesum/mcount; femaleaverage = femalesum/fcount; for (i = 0; i < n; ++i){ writeoutput(i); } printf("\n Average male age %d",maleaverage); printf("\n Average female age %d", femaleaverage); } void readinput(int i) { printf("\nStudents no. %d\n", i+1); printf(" Name "); scanf(" %s", students[i].name); printf(" Matrix "); scanf("%s", students[i].matrix); printf(" sex "); scanf("%s", &students[i].sex); printf(" Age "); scanf("%d", &students[i].age); return; } void writeoutput(int i) { printf("\nName %s", students[i].name); printf("\n Matrix %s", students[i].matrix); printf("\n Sex %s", students[i].sex); printf("\n Age %d", students[i].age); return; }
If you indented your code better, the problem would become more obvious:
Code:#include<stdio.h> void readinput(int i); void writeoutput(int i); typedef struct { char matrix[30]; char name[30]; int age; char sex[6]; } record; record students[10]; main(void) { int i, n, maleaverage, femaleaverage,malesum = 0, femalesum = 0, mcount = 0, fcount = 0; printf("how many student"); scanf("%d", &n); for (i = 0; i < n; ++i) { readinput(i); } for (i = 0; i < n; ++i) { if (strcmp(students[i].sex, "male") == 0) malesum = malesum + students[i].age; ++mcount; if (strcmp(students[i].sex, "female") == 0) femalesum += students[i].age; ++fcount; } maleaverage = malesum/mcount; femaleaverage = femalesum/fcount; for (i = 0; i < n; ++i){ writeoutput(i); } printf("\n Average male age %d",maleaverage); printf("\n Average female age %d", femaleaverage); } void readinput(int i) { printf("\nStudents no. %d\n", i+1); printf(" Name "); scanf(" %s", students[i].name); printf(" Matrix "); scanf("%s", students[i].matrix); printf(" sex "); scanf("%s", &students[i].sex); printf(" Age "); scanf("%d", &students[i].age); return; } void writeoutput(int i) { printf("\nName %s", students[i].name); printf("\n Matrix %s", students[i].matrix); printf("\n Sex %s", students[i].sex); printf("\n Age %d", students[i].age); return; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
plz state out my problem can?i cant find it thats y i post it up here..
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I will narrow it down to:
Think of how you are counting the number of students from the respective sex.Code:for (i = 0; i < n; ++i) { if (strcmp(students[i].sex, "male") == 0) malesum = malesum + students[i].age; ++mcount; if (strcmp(students[i].sex, "female") == 0) femalesum += students[i].age; ++fcount; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
okey thx alot i found out my problem d.. thx bracket wasted my time=[
And if you had used proper indentation, you would have seen it immediately. There are plenty of tools available to help with indentation - most modern editors for example have a tool that will "auto-indent" your code - it's usually adjustable to suit your personal style as well.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
-other tread-
Last edited by archriku; 04-10-2009 at 04:43 AM.