Thread: Function Question

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    16

    Cool Function Question

    Code:
    #include<stdio.h>
     
    int calcAbvAvg(int marks[10], int avg)
    {
        int students = 0;
        int i;
     
        for(i=0;i<10;i++)
        {
            if(marks[i] > avg)
            {
                students++;
            }
        }
      printf("Students above average = %d",abv_avg);
        printf("Students below average = %d",10 - abv_avg);    
    }
     
    main (void)
    {
        int marks[10];
        float avg;
        int min = 100;
        int max = 0;
        int tot = 0;
        int count;
        int abv_avg = 0;
     
        for (count=0;count<10;count++)
        {
            printf("Enter marks for student %d",count+1);
     
            while(scanf("%d",&marks[count) != 1)
            {
                printf("\nWrong input\nEnter marks for student %d",count+1);
                fflush(stdin);
            }
     
            if(min > marks[count])
            {
                min = marks[count];
            }
     
            if(max < marks[count])
            {
                max = marks[count];
            }
        }
     
        for (count=0;count<10;count++)
        {
            total += marks[count];
        }
     
        avg = total/10;
        abv_avg = calcAbvAvg(marks, avg);
     
        printf("Minimum marks = %d",min);
        printf("Maximum marks = %d",max);
        printf("Average marks = %0.2f",avg);
      calcAbvAvg(marks, avg);
     
    }
    I suck at function and linked list

    can u help me with this question! the only problem i am having in this question is the function calcAbvAbg() not working! i want that function to display the students who are above and below average!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Yet more flushing of stdin.
    I give up.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    printf("Students above average = %d",abv_avg);
        printf("Students below average = %d",10 - abv_avg);
    I think you meant to put students instead of abv_avg..

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    2. Don't use magic numbers (e.g. 10) - use named constants instead (e.g. #define MAX_MARKS 10)
    3. You should heed compiler warnings. In fact, I got several warnings and errors when I tried to compile your code:

    Code:
    main.c||In function 'calcAbvAvg':|
    main.c|15|error: 'abv_avg' undeclared (first use in this function)|
    main.c|15|error: (Each undeclared identifier is reported only once|
    main.c|15|error: for each function it appears in.)|
    main.c|20|warning: return type defaults to 'int'|
    main.c||In function 'main':|
    main.c|33|error: expected ']' before ')' token|
    main.c|52|error: 'total' undeclared (first use in this function)|
    main.c|25|warning: unused variable 'tot'|
    ||=== Build finished: 5 errors, 2 warnings ===|
    You're trying to use a variable "abv_avg" in your "calcAbvAvg()" function, but that variable doesn't exist that function.

    Also, on line 33, you have a closing parenthesis ')' where a closing bracket is supposed to go ']'.

    I didn't look too much further into your code. Fix up the warnings/errors so it compiles, and post your updated code.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Function question!
    By Tarento in forum C Programming
    Replies: 7
    Last Post: 05-18-2006, 08:41 PM
  2. function question
    By MoneyH20 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2005, 08:10 PM
  3. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  4. Function Question
    By Spectrum48k in forum C Programming
    Replies: 1
    Last Post: 05-24-2002, 06:07 PM
  5. Function Question
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2002, 07:13 PM