Thread: calculate average

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  2. #17
    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, 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;
    	}
    ok this is my newest code.. still.. the average is still not the correct answer.. plz help me

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    plz state out my problem can?i cant find it thats y i post it up here..

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by archriku View Post
    plz state out my problem can?i cant find it thats y i post it up here..
    Do you actually want to count up mcount and fcount EVERY time, or only when you have a match of "male" and "female" respectively? I think you are missing some braces. Can you see where?

    --
    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.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I will narrow it down to:
    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;
    }
    Think of how you are counting the number of students from the respective sex.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    okey thx alot i found out my problem d.. thx bracket wasted my time=[

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by archriku View Post
    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.

  9. #24
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    -other tread-
    Last edited by archriku; 04-10-2009 at 04:43 AM.

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, 02: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