calculate average

This is a discussion on calculate average within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void readinput(int i); void writeoutput(int i); typedef struct { char matrix[30]; char name[30]; int age; char sex[4]; } ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    77

    calculate average

    Code:
    #include<stdio.h>
    
    void readinput(int i);
    void writeoutput(int i);
    
    typedef struct {
    	char matrix[30];
    	char name[30];
    	int  age;
    	char sex[4];
    } record;
    
    record students[10];
    
    
    
    main(void)
    {
    	int i, n, j, average;
    	printf("how many student");
    	scanf("%d", &n);
    		
    	for (i = 0; i < n; ++i){
    readinput(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);
    
    }
    }
    	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;
    	}
    how do i calculate the average for male and female separately? i only know how 2 do overall. izzit need 2 use strcmp?

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    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. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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[10];
    
    ...
    
    int i, n, j, average;
    printf("how many student");
    scanf("%d", &n);
    		
    for (i = 0; i < n; ++i){
        readinput(i);
    }
    ... a very simple check can help you out here and deal with such potential problems.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by hk_mp5kpdw View Post
    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[10];
    
    ...
    
    int i, n, j, average;
    printf("how many student");
    scanf("%d", &n);
    		
    for (i = 0; i < n; ++i){
        readinput(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. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Quote Originally Posted by archriku View Post
    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;
    Last edited by markcole; 04-08-2009 at 11:48 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by archriku View Post
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    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, count, average;
    	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)
    		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);
    
    }
    
    
    
    
    
    	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;
    	}
    can i know what i did wrong?i was doing calculation of age for male and female separately..

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    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. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    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,j, maleaverage, femaleaverage,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)
    {
    	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); 
    
    }
    
    
    
    
    
    	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;
    	}
    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. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    when you enter sex - no ampersand needed...

    about your avg calculation... hmmm

    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    when you enter sex - no ampersand needed...

    about your avg calculation... hmmm

    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. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by archriku View Post
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    #include<stdio.h>

    Code:
    main(void)
    {
    	int a[3], 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
    Last edited by archriku; 04-09-2009 at 01:42 PM.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    sum is not initialized

    average declared as int - so it cannot store something like 2.5
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    sum is not initialized

    average declared as int - so it cannot store something like 2.5
    Code:
    #include<stdio.h>
    
    main(void)
    {
    	int a[3], 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
    Last edited by archriku; 04-09-2009 at 01:56 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. calculate average from a file
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2009, 01:25 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. Average score
    By wiz23 in forum C++ Programming
    Replies: 22
    Last Post: 05-05-2005, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21