Thread: help with a simple programe :)

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    6

    help with a simple programe :)

    hi everybody
    here is the question

    write a program using functios to find out the average of three marks(m1,m2,m3)of n number of students , the function should read the 3 marks and calculate the average and anther function to take the average and print the grade depending on the following category .
    the main call the two functions

    60-70 pass
    71-80 good
    81-90 v.good
    91-100 excellent

    this is what i've wrote so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    double avg (int m1 ,int m2, int m3);
    void cate (void);
    int main()
    {
      int m1,m2,m3 ;
        printf("Enter the first mark\n");
        scanf("%d",&m1);
        printf("Enter the second mark\n");
        scanf("%d",&m2);
        printf("enter the third mark\n");
        scanf("%d",&m3);
        printf("the average is %f\n",avg(m1,m2,m3));
        cate();
        return 0;
    }
    double avg (int m1 ,int m2,int m3)
    {
        return((m1+m2+m3)/3);
    }
    void cate (void)
    {
        double avg ();
     if ( avg >=60 && avg <=70){
     printf("pass");
     }
     else if ( avg >=71 &&  avg  <= 80){
     printf ("good");
     }
     else if ( avg  >=81 &&  avg <= 90){
     printf("v.good");
     }
     else
     printf("excellent");
     }
    the problem is that i keep taking this messege

    in function 'cate'
    warning: a comparison between a pointer and integer (line 25)
    warning: a comparison between a pointer and integer (line 25)
    warning: a comparison between a pointer and integer (line 28)
    warning: a comparison between a pointer and integer (line 28)
    warning: a comparison between a pointer and integer (line 29)

    so any help about this?? please

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, the avg declaration in cate() is wrong. Are you trying to call the function avg()? If so that takes three parameters, you defined it that way. Are you trying to have a variable take the return value of that function? I recommend scrapping the avg() function and just passing m1,m2,m3 as parameters to cate(). Then compute the average there in some avg variable and use that.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Because you have the following line in your cate function:
    Code:
    double avg ();
    Which makes avg effectively a pointer to the function avg above. Try passing the result of avg into cate and using that to produce your output:
    Code:
    int main(void)
    {
        double a;
        ...
        a = avg(m1, m2, m3);
        printf("the average is %f\n",a);
        cate(a);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    thanks for help
    but still there a problem , that
    it keeps giving me excellent for every average
    is there any error in if statments

    btw , this is the new code
    tell me if it's right please
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    double avg (int m1 ,int m2, int m3);
    void cate (void);
    int main()
    {
      int m1,m2,m3 ;
      double a;
        printf("Enter the first mark\n");
        scanf("%d",&m1);
        printf("Enter the second mark\n");
        scanf("%d",&m2);
        printf("enter the third mark\n");
        scanf("%d",&m3);
        a = avg(m1,m2,m3);
        printf("the average is %f\n",a);
        cate();
        return 0;
    }
    double avg (int m1 ,int m2,int m3)
    {
        return((m1+m2+m3)/3);
    }
    void cate (void)
    {
        double avg;
    
     if ( avg >=60 && avg <=70){
     printf("pass");
     }
     else if ( avg >=71 &&  avg  <= 80){
     printf ("good");
     }
     else if ( avg >=81 &&  avg <= 90){
     printf("v.good");
     }
     else
     printf("excellent");
     }
    Last edited by goby; 12-08-2010 at 04:28 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You don't pass a into cate like I suggested, so the avg variable in there is uninitialized and has who knows what value in it.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    when i pass (a)into cate functions ; many errors ocurrs :S

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As opposed to the many errors you have now.

    You need to pass the average number into the cate function; that's not an option. You will need to fix whatever errors you have when you do that (i.e., make the function prototype/declaration correct).

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    Code:
    void cate ( double );
    ...
    cate ( a );
    ...
    void cate ( double avg)
    {
     if ( avg >=60 && avg <=70){
    ...

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    25

    The whole program should be able to be read aloud, just like a smoothly flowing story

    When I read the functions in the example program, I'm not seeing that they are reading right. Functions are just like a formula for an algebra problem when you're working word problems.

    I suggest reading functions and writing functions in a pattern similar to this:

    When declaring that a function exists, read as:
    Code:
    " . . . answer type formula name (answer type variable 1, answer type variable 2)
    When getting prepared to call a function, read the need for that function as:
    • formula to be used
    • value to go in to the formula
    • value to come out of the formula
    • e.g. formula-name (x,y)


    When calling a function, read the code as:
    Code:
    " . . . formula-name (apply to variable 1, apply to variable 2) . . . "
    When defining or describing a function, read the code as:
    Code:
    " . . . answer type formula-name (variable 1, variable 2){
        declare 
        work variable 1 against some math
       return an answer to fit variable 2
        }
        . . ."
    Notice that in and around the function, the names would be so that "apply to variable 1" and "variable 1" would be similar, but not the same; this would help keep the reading of the program smoother.

    In the example program, I see that the functions are there, but I don't read the code as though they are a part of the program. The whole program should be able to be read aloud as a set of instructions.

    If you cannot read the program aloud so that it makes sense, and it's breaking, then that spot where things stop flowing: that's the kink in the logic.

    I apologize if that was confusing, but that is how I think about it.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    return((m1+m2+m3)/3);
    ... this won't return the correct result since all your variables are ints. Use ((m1+m2+m3)/3.0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM