1. ## calculate average

Code:
```#include<stdio.h>

void writeoutput(int i);

typedef struct {
char matrix;
char name;
int  age;
char sex;
} record;

record students;

main(void)
{
int i, n, j, average;
printf("how many student");
scanf("%d", &n);

for (i = 0; i < n; ++i){
}

for (i = 0; i < n; ++i)
{
for ( j= i+ 1; j < n; ++j) {
average = (students[i].age+ students[j].age)/2;
}
writeoutput(i);
printf("\n Average age %d",average);

}
}

{
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;
}```
how do i calculate the average for male and female separately? i only know how 2 do overall. izzit need 2 use strcmp? 2. Just check the sex of the student before adding their grade in. You would either need to count the different sexes or keep a running count as you add the scores in. 3. Try to use some consistent indentation when posting your code sample.

What happens if/when the user enters more than 10 for "how many students?"?
Code:
```record students;

...

int i, n, j, average;
printf("how many student");
scanf("%d", &n);

for (i = 0; i < n; ++i){
}```
... a very simple check can help you out here and deal with such potential problems. 4. Originally Posted by hk_mp5kpdw Try to use some consistent indentation when posting your code sample.

What happens if/when the user enters more than 10 for "how many students?"?
Code:
```record students;

...

int i, n, j, average;
printf("how many student");
scanf("%d", &n);

for (i = 0; i < n; ++i){
}```
... a very simple check can help you out here and deal with such potential problems.
okok i will just increase the size of the students.. so how do i relate the check the sex and count the mark.. what fuction do i use? 5. Originally Posted by archriku okok i will just increase the size of the students.. so how do i relate the check the sex and count the mark.. what fuction do i use?
Just use your for loop, but compare the sex of the student with the desired sex for the average

Something like:

Code:
```int average, count = 0;
for (i = 0; i < n; ++i)
{
if (sex = 'M')
{
average = average + students[i].age);
count++;
}
}
average = average/count;``` 6. Originally Posted by archriku okok i will just increase the size of the students..
Not really the point I was trying to make. I was trying to point out that you do no validation of the user's input to verify that it is a valid/meaningful value prior to use in the loop. It does you no good to say "well, I'll just increase the size of the array" because as soon as you do, someone might just enter a bigger value. Get the user's input and validate that the value is between 0 and the size of the array before you use it in the loop and it won't matter what size you make the array since your code will now be proofed against bad user input. 7. Code:
```#include<stdio.h>

void writeoutput(int i);

typedef struct {
char matrix;
char name;
int  age;
char sex;
} record;

record students;

main(void)
{
int i, n, count, average;
printf("how many student");
scanf("%d", &n);

for (i = 0; i < n; ++i){
}

for (i = 0; i < n; ++i)
{

if (strcmp(students[i].sex, "male") == 0)
average = average + students[i].age;
++count;

if (strcmp(students[i].sex, "female") == 0)
average = average + students[i].age;
++count;
}

average = average/count;

writeoutput(i);
printf("\n Average age %d",average);

}

{
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;
}```
can i know what i did wrong?i was doing calculation of age for male and female separately.. 8. The real big issue here is that you are using the same variable 'average' to tally up both your distinct averages that you are trying to separate.

Create a maleAverage and femalAverage variable for this. 9. Code:
```#include<stdio.h>

void writeoutput(int i);

typedef struct {
char matrix;
char name;
int  age;
char sex;
} record;

record students;

main(void)
{
int i, n,j, maleaverage, femaleaverage,mcount = 0, fcount = 0;
printf("how many student");
scanf("%d", &n);

for (i = 0; i < n; ++i) {
}

for (i = 0; i < n; ++i)
{
for( j = i + 1; j < n; ++j){

if (strcmp(students[i].sex, "male") == 0)
maleaverage = (students[i].age + students[j].age);
++mcount;

if (strcmp(students[i].sex, "female") == 0)
femaleaverage = (students[i].age + students[j].age);
++fcount;
}
}
maleaverage = maleaverage/mcount;
femaleaverage = femaleaverage/fcount;

for (i = 0; i < n; ++i){
writeoutput(i);
}
printf("\n Average male age %d",maleaverage);
printf("\n Average female age %d", femaleaverage);

}

{
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;
}```
plz help.. i already modified it.. still cant.. plz state my problem plz.. n if can give me some tips will be the best.. 10. when you enter sex - no ampersand needed...

Start simple

take array
Code:
`int a[] = {1,2,3,4,5};`
and write short code 3-5 lines to calculate avg and print it - should be 3

when take another array
Code:
```
int a[] = {1,2};```
and check on it same code - should be 1.5

when you got this very simple code working - compare it to the code in you program and try to understand what is your problem 11. Originally Posted by vart when you enter sex - no ampersand needed...

Start simple

take array
Code:
`int a[] = {1,2,3,4,5};`
and write short code 3-5 lines to calculate avg and print it - should be 3

when take another array
Code:
```
int a[] = {1,2};```
and check on it same code - should be 1.5

when you got this very simple code working - compare it to the code in you program and try to understand what is your problem
when you enter sex - no ampersand needed... <
-- can u explain this more clearer? 12. Originally Posted by archriku when you enter sex - no ampersand needed... <
-- can u explain this more clearer?
sex and name - both are char arrays

when you enter name and sex - syntax should be the same

you have a difference. Could you spot it? 13. #include<stdio.h>

Code:
```main(void)
{
int a, count = 0, average, sum, i,n;

printf("how many numbers");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{

scanf("%d", &a);
sum += a[i];
++count;
}
average = sum/count;

printf("%d", average);
return 0;
}```
ok even this simple count i also dont know what is wrong with it.. can u please state my problem?i really dun understand what is my problem 14. sum is not initialized

average declared as int - so it cannot store something like 2.5 15. Originally Posted by vart sum is not initialized

average declared as int - so it cannot store something like 2.5
Code:
```#include<stdio.h>

main(void)
{
int a, count = 0, sum = 0, i,n;
float average;

printf("how many numbers");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{

scanf("%d", &a);
sum += a[i];
++count;
}
average = sum/count;

printf("%f", average);
return 0;
}```
ok i modified into lidis but still the average is very big Popular pages Recent additions 