Thread: Help with functions usage on project plz

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    98

    Thumbs up Help with functions usage on project plz

    Hi, I was hoping somebody could help me with this program I wrote the other day, I need to modify it to include functions to find the highest, and lowest values of test scores.

    I have begun to insert the functions, but I'm not sure what to do next. My program already calculates the highest grade, but for an extra ten points, I need it to do it with a function.

    Because the if then for highest score already exists, I was wondering if I could take it out and put it into the function for highest, for starters. Then write a similar one for lowest?
    How do I call the values at the end, what goes inside the parameters?

    Code:
    //This programs prompts the user for the grades of ten tests, then prints the av
    erage score, the high score, and a list of scores associated with each student a
    nd test, including a letter grade.
    
    
    #include <stdio.h>
    
    float findLowest(float[], int)
     {
     //  statements
     //return Lowest;
     }
    
    
    float findHighest(float[], int)
     {
     //statements
     //return Highest;
     }
    
    
    #define N 10
    
    int main(void)
    
    {
    
     double grade[N], highscore, average_score;
     float sum, numraise, studentname, i;
     char lettergrade;
     highscore=0.0;
     sum=0.0;
    
     printf("Enter %d grades: ", N); //Prompts the user for grade inputs
    
     for (i=0; i<N; i++)
    
     {
      scanf("%lf", &grade[i]);
      sum += grade[i];
      if (highscore < grade[i])    // Move this to function? Modify how?
      highscore = grade[i];
    
     }
    
    average_score= sum/10;
    
     for (i = 0;i < 10; i++)
     {
       if (grade[i] >= 90.0)
       lettergrade='A';
      else if (grade[i] >=80.0)
       lettergrade='B';
      else if (grade[i] >=70.0)
       lettergrade='C';
      else if (grade[i] >=60.0)
       lettergrade='D';
      else
      lettergrade='F';
    
     printf("Student %f's grade is %.1lf, this is an %c\n", i+1, grade[i], lettergra
    de);
     }
    
     printf("The highest score is %.1lf\n", highscore); //Prints high score
     printf("the average score is %.2lf\n", average_score); //prints average
    
     printf("The lowest score is: %.2f", lowest());
     printf("The highest score is: %.2f", highest());
    
     return 0;
     }

    Thanks for reading!
    Last edited by LightYear; 04-10-2010 at 09:03 AM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Because the for loop for highest score already exists, I was wondering if I could take it out and put it into the function for highest, for starters. Then write a similar one for lowest?
    In short - yes. Just put your loops in the functions and fix whatever compiler errors show up.

    EDIT: For future reference, you ought to try this kind of stuff on your own before asking about it. Don't worry about messing up your code - that's what Undo is for.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Thank you. I spent 11 hours on this last night and we just started functions. So if I had known what to try, then I would have tried it. Thx

    The examples in my book are quite ambiguous.


    I can't just put my for loops into the functions. The first for loop causes scanf to scan the numbers into the array, what will happen if that part is outside main?
    Last edited by LightYear; 04-10-2010 at 09:13 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Clue (here's a function):
    Code:
    float findHighest(float grades[], int len) {
          int i;
          float high = 0.0f
          for(i=0;i<len;i++) 
                 if (grades[i] > high) high = grades[i];
         return high;
     }
    Here's how you call it:
    Code:
    highest = findHighest(grades,N);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Code:
    //This programs prompts the user for the grades of ten tests, then prints the av
    erage score, the high score, and a list of scores associated with each student a
    nd test, including a letter grade.
    
    
    #include <stdio.h>
    
    float findLowest(float grades[], int n)
     {
      int i, int n=10;
    
      float  low=100.0;
       for (i=0;i<n;i++)
       if (grades[i] < low) low = grades[i];
    
      return low;
     }
    
     float findHighest(float grades(), int n)
     {
      int i, int n=10;
      float high = 0.0;
       for(i=0;i<n;i++)
    
       if (grades[i] > high) high = grades[i];
    
      return high;
     }
    
    
    
    
    #define N 10
    
    int main(void)
    
    {
    
     double grade[N], highscore, average_score;
     int i, numraise;
     float sum, studentname;
     char lettergrade;
     highscore=0.0;
     sum=0.0;
     average_score= sum/10;
    
     printf("Enter %d grades: ", N); //Prompts the user for grade inputs
    
     for (i=0; i<N; i++)
    
     {
      scanf("%lf", &grade[i]);
      sum += grade[i];
    
     }
    
    
     for (i = 0;i < 10; i++)
     {
       if (grade[i] >= 90.0)
       lettergrade='A';
      else if (grade[i] >=80.0)
       lettergrade='B';
      else if (grade[i] >=70.0)
       lettergrade='C';
      else if (grade[i] >=60.0)
       lettergrade='D';
      else
      lettergrade='F';
    
     printf("Student %d's grade is %.1lf, this is an %c\n", i+1, grade[i], lettergra
    de);
     }
     
     
     //printf("The highest score is %.1lf\n", highscore); //Prints high score
     printf("the average score is %.2lf\n", average_score); //prints average
    
     printf("The lowest score is: %.2f", findHighest(grades, n));
     printf("The highest score is: %.2f", findLowest(grades, n));
     return 0;
     }

    Almost done, gonna compile
    Last edited by LightYear; 04-10-2010 at 10:05 AM.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Okay can anyone see what is wrong with the above still, I cannot see it wtf...?

    Says syntax error in function findlowest


    Compiler gives the following errors:

    Code:
    P4_2.c: In function `findLowest':
    P4_2.c:8: error: syntax error before "int"
    P4_2.c:11: error: subscripted value is neither array nor pointer
    P4_2.c:11: error: subscripted value is neither array nor pointer
    P4_2.c: In function `findHighest':
    P4_2.c:18: error: syntax error before "int"
    P4_2.c:21: error: subscripted value is neither array nor pointer
    P4_2.c:21: error: subscripted value is neither array nor pointer
    P4_2.c: In function `main':
    P4_2.c:72: error: `grades' undeclared (first use in this function)
    P4_2.c:72: error: (Each undeclared identifier is reported only once
    P4_2.c:72: error: for each function it appears in.)
    P4_2.c:72: error: `n' undeclared (first use in this function)
    Last edited by LightYear; 04-10-2010 at 10:04 AM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is a problem:
    Code:
    float findLowest(float grades[], int n)
     {
      int i, int n=10;
    You have now declared (or tried to declare, since the second one "int" var comma "int" again is a syntax error) more than once.

    n is a parameter to the function. It is declared in the (parameters). Also, in this case that parameter is a supplied value: why do you want to redefine it arbitrarily as 10?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    rewrote it as this:

    Code:
    float findLowest(float grades(), int n)
     {
      int i;
      float  low=100.0;
       for (i=0;i<10;i++)
       if (grades[i] < low) low = grades[i];
    
      return low;
     }
    
     float findHighest(float grades(), int n)
     {
      int i;
      float high = 0.0f;
       for(i=0;i<10;i++)
       if (grades[i] > high) high = grades[i];
    
      return high;
     }
    This gives the following error:
    Code:
    P4_2.c: In function `findLowest':
    P4_2.c:11: error: subscripted value is neither array nor pointer
    P4_2.c:11: error: subscripted value is neither array nor pointer
    P4_2.c: In function `findHighest':
    P4_2.c:21: error: subscripted value is neither array nor pointer
    P4_2.c:21: error: subscripted value is neither array nor pointer
    P4_2.c: In function `main':
    P4_2.c:72: error: `grades' undeclared (first use in this function)
    P4_2.c:72: error: (Each undeclared identifier is reported only once
    P4_2.c:72: error: for each function it appears in.)
    P4_2.c:72: error: `n' undeclared (first use in this function)

    I just wanted n to be 10 because I thought that would cause i to loop 10 times... but I see that different values need to be stored into n?

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    float findLowest(float grades(), int n)
    Whoops! Those are not square. Empty square brackets just indicates this is an array of some size (actually it's a pointer, blah blah). Nb, "subscript" notation refers to this:

    grades[i]

    "[i]" is the subscript. I think the "subscripted value" would be grades.

    Quote Originally Posted by LightYear View Post
    I just wanted n to be 10 because I thought that would cause i to loop 10 times... but I see that different values need to be stored into n?
    Yes, it would cause the loop to iterate 10 times, but you want the loop to iterate N times -- as in, N from main() -- right? That is the length of the array pointed to by the other parameter (grades). And you submitted both of them.

    To clarify:
    Code:
    void eg(int X) {
    	printf("The number is %d\n", X);
    }
    If I call this function with eg(666), what number do you think will be printed out?
    Now, if I insert a new line:
    Code:
    void eg(int X) {
            X = 10;
    	printf("The number is %d\n", X);
    }
    What will be different?
    Last edited by MK27; 04-10-2010 at 10:24 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    I would think the first one would print 666 or whatever you put there, and the second one, always 10?


    Now, in the bottom where I call the functions, the later parameter should be n and not N right?
    Last edited by LightYear; 04-10-2010 at 10:33 AM.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by LightYear View Post
    I would think the first one would print 666 or whatever you put there, and the second one, always 10?
    Yep. Will grades always have a length of 10? If so, then you don't even need a parameter for this, you could just set it inside the function.

    Now, in the bottom where I call the functions, the later parameter should be N right?
    Yep too.* Remember: parameters passed from one function to another (such as from main() to findHighest()) do not have to have the same name, they just need to be of the same type. So N from main() could be called len, or n, or whatever you want, in findHighest(). As long as they are both ints, the value will be passed thru unchanged.

    *[edit] so your edit, which crossed my post is incorrect...hopefully this makes sense
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Code:
    //This programs prompts the user for the grades of ten tests, then prints the av
    erage score, the high score, and a list of scores associated with each student a
    nd test, including a letter grade.
    
    
    #include <stdio.h>
    #define N 10
    
    float findLowest(float grades[i], int N)
     {
      int i;
      float  low=100.0;
       for (i=0;i<N;i++)
       if (grades[i] < low) low = grades[i];
    
      return low;
     }
     }
    
     float findHighest(float grades(), int n)
     {
      int i;
      float high = 0.0f;
       for(i=0;i<N;i++)
       if (grades[i] > high) high = grades[i];
    
      return high;
     }
    
    int main(void)
    {
    
     double grade[N], highscore, average_score;
     int i, numraise;
     float sum, studentname;
     char lettergrade;
     highscore=0.0;
     sum=0.0;
     average_score= sum/10;
    
     printf("Enter %d grades: ", N); //Prompts the user for grade inputs
     for (i=0; i<N; i++)
    
     {
      scanf("%lf", &grade[i]);
      sum += grade[i];
    
     }
    
    
     for (i = 0;i < 10; i++)
     {
       if (grade[i] >= 90.0)
       lettergrade='A';
      else if (grade[i] >=80.0)
       lettergrade='B';
      else if (grade[i] >=70.0)
       lettergrade='C';
      else if (grade[i] >=60.0)
       lettergrade='D';
     printf("Student %d's grade is %.1lf, this is an %c\n", i+1, grade[i], lettergra
    de);
     }
     printf("the average score is %.2lf\n", average_score); //prints average
    
     printf("The highest score is: %.2f\n", findHighest(grades, N));
     printf("The lowest score is: %.2f\n", findLowest(grades, N));
    
     return 0;
     }

    Here's what I have now, see what I'm asking?

    compiler says i is undeclared???
    Last edited by LightYear; 04-10-2010 at 10:43 AM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by LightYear View Post
    Here's what I have now, see what I'm asking?
    I understand what you are asking. Read my last post again, about variable names and passing parameters. They are not passed "by name", they are passed by value. The name is just a label for the programmer, in fact it does not exist in the compiled program. "grades" could also have a different name in findHighest(). The choice is yours.

    You want to pass (the value of) N, so you refer to it by name (N).

    Now, you have a unrelated problem here:
    Code:
    int main(void)
    {
         double grade[N],
    N has not been defined at this point! If you want (or have) to do this, you will need to use a "dynamic" array, which means using malloc().
    Code:
    int main (void) {
        double *grades;
        int N;
        
        // get a value for N from user
    
        grades = malloc(N*sizeof(double));
    If you use malloc, you must also use free() when you are done with that variable (in this case, at the end):
    Code:
    free(grades);
    Also, as I said, parameter names do not need to match but the types should. You declare grades an array of double, but your functions accept floats. Choose one or the other and be consistent (floats are fine here).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    N has not been defined at this point!
    Well actually there's a #define N 10 at the top of the file. I'm pretty sure gcc (probably msvc too) supports declaring an array with a preprocessor macro, as an extension.

    EDIT: as far as declaring arrays go, it looks like variables are fair game too
    Last edited by bernt; 04-10-2010 at 10:57 AM.
    Consider this post signed

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Thank you I will try and get it right. We can't use malloc because we haven't studied it yet; I need to do it without malloc.


    N is in the #define is that ok?


    the compiler says i is undeclared, can somebody compile this and tell me what's up, I changed all the variables to floating point because that has range sufficient and double is overkill like you said and they need to be same type to transfer etc...

    Can someone throw this in the toaster and tell me what the deal is? I know this is darn close to right

    Code:
    //This programs prompts the user for the grades of ten tests, then prints the av
    erage score, the high score, and a list of scores associated with each student a
    nd test, including a letter grade.
    
    #include <stdio.h>
    #define N 10
    
    float findLowest(float grades[i], int N)
     {
      int i=0;
      float  low=100.0;
       for (i=0;i<N;i++)
       if (grades[i] < low) low = grades[i];
    
      return low;
     }
    
     float findHighest(float grades(), int N)
     {
      int i=0;
      float high = 0.0f;
       for(i=0;i<N;i++)
       if (grades[i] > high) high = grades[i];
    
      return high;
     }
    
    int main(void)
    {
    
     float grade[N], highscore, average_score, sum, studentname, i, numraise;
     char lettergrade;
     highscore=0.0;
     sum=0.0;
     average_score= sum/10;
    
     printf("Enter %d grades: ", N); //Prompts the user for grade inputs
    
     for (i=0; i<N; i++)
    
     {
      scanf("%f", &grade[i]);
      sum += grade[i];
    
     }
    
     for (i = 0;i < 10; i++)
     {
       if (grade[i] >= 90.0)
       lettergrade='A';
      else if (grade[i] >=80.0)
       lettergrade='B';
      else if (grade[i] >=70.0)
       lettergrade='C';
      else if (grade[i] >=60.0)
       lettergrade='D';
      else
      lettergrade='F';
    
     printf("Student %f's grade is %.1f, this is an %c\n", i+1, grade[i], lettergrad
    e);
     }
     printf("the average score is %.2f\n", average_score); //prints average
    
     printf("The highest score is: %.2f\n", findHighest(grades, N));
     printf("The lowest score is: %.2f\n", findLowest(grades, N));
    
     return 0;
     }
    Last edited by LightYear; 04-10-2010 at 11:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with this project plz
    By ice_jr in forum C Programming
    Replies: 1
    Last Post: 11-10-2009, 12:34 AM
  2. Functions! Need help ASAP plz!
    By andone in forum C Programming
    Replies: 2
    Last Post: 11-02-2006, 04:07 AM
  3. Project details: Dedicated
    By Stack Overflow in forum Projects and Job Recruitment
    Replies: 9
    Last Post: 02-22-2005, 03:10 PM
  4. DJGPP project problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 07:16 PM