Thread: Looking for critiquers on arrays

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

    Looking for critiquers on arrays

    My code is supposed to accept user input to make an array for grades. Then compute the average and sum of the array. Also, it is supposed to display an asterisk beside the grade if it below average, and the letter grade.

    Any suggestions of how to clean up the code or clean up any of the errors I am getting like numel and grade being undeclared identifier in my first for loop. I am looking to learn so any critique and help will be great appreciated

    Code:
    double findSum(double[], int);
    double findAvg(double [], int);
    int main()
    {
    #define NUMELS 100
    #define CUTOFF -1
      static double grades[100];
      double average, sum;
      int i;
      printf("Enter the grades, when finished enter a negative number:  ");
      
      while (grades < CUTOFF.o
      {
      printf("Enter a grade: ");
      scanf("%f", &grades[100]);
      }
      
      average = findAvg(grades, NUMELS);
      sum = findSum(grades, NUMELS);
      printf("The sum of the grades is %f", sum);
      printf("The average of the grades is %f", average);
     for (i = 0; i < numel; i++)
      if (grade[i] < average)
       printf("*%f", i)
      else if 
       printf("%f", i);
    
     if (grade[i] >= 90)
      printf("%f A", i);
     else if (grade[i] >= 80 && grade [i] < 90)
      printf("%f B", i);
     else if (grade[i] >= 70 && grade [i] < 80)
      printf("%f C", i);
     else if (grade[i] >= 60 && grade [i] < 70)
      printf("%f D", i);
     else 
      printf("%f F", i);
      return 0;
    }
    double findAvg(double grades[], int numel)
    {
      int i;
      double sumnums = 0.0;
      for (i = 0; i < numel; i++)
       sumnums = sumnums + grades[i];
      return (sumnums/numel); /*calculate and return average*/
    }
    double findSum(double grades[], int numel)
    {
      int i;
      double sumnums = 0.0;
      for (i = 0; i < numel; i++)
       sumnums = sumnums + grades[i];
      return (sumnums); /*reutrn the sum*/
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Show your #include file(s).

    I've never seen #defines inside a function. They always have been before main, so every function can use them.

    How many grades will be < CUTOFF.o? (Especially since o is a letter, not a zero). Use the clearer while(marks >= 0). Marks are scores for the student, and int's. Grades are letters. I've never seen marks as floating point numbers, Total marks would be a floating point number only if the marks were averaged for the student. They're just added to get the total marks for the student, so no averaging is needed, imo

    %f - is for floats in scanf(). %lf is for doubles in scanf() (note that printf() is a bit different).

    You should be getting errors or warnings from your compiler on some of the program when you try to compile it. Post them if you need help with figuring out what they mean.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Okay thank you, I cleaned up things you suggested.

    I'm getting two errors which are
    12- < and double[100] differ in levels
    26- syntax error printf identifier

    CUTOFF is supposed to allow the user to enter a negative grade to end user input for the array grade I may be using that incorrectly

    My idea behing the %f (which now that you point out should be %lf since my program is supposed to accept double precision numbe) is to display the numerical grade and the letter grade

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%f", &grades[100]);
    You're not reading into an array, you're reading into memory which is just off the end of your array.
    You need a subscript here.

    Also, lines 22 to 26 really need braces to show what the program flow is.
    Your code is correct at the moment, but it's only one edit from being broken.

    In fact, is it already broken?
    if (grade[i] >= 90)
    should this if/else chain be in the loop as well?

    The indentation needs work as well. 1 space is too little to be meaningful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Don't put your defines inside of main (or any function) since they are not block-scoped and putting them inside the function text makes it look like you think they are. And shouldn't you be using NUMELS to size your grades array?

    Actually, what is a NUMEL? What language is that?

    And the code doesn't even compile, so you're not looking for "critiquers" but simply for help.

    (And surely the word is "critics" not "critiquers".)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    NUMEL is what my text use and the main thing that I have to go off.

    Here is the updated code with the clean-ups everybody has suggested but I still receive two errors:
    12- < and double[100] differ in levels
    26- syntax error printf identifier

    Code:
    #include <stdio.h>
    #pragma warning (disable :4996)
    #define NUMELS 100
    #define CUTOFF -1
    double findSum(double[], int);
    double findAvg(double [], int);
    int main()
    {
      static double grades[100];
      double average, sum;
      int i;
       printf("Enter the grades, when finished enter a negative number:  ");
      
       while (grades < CUTOFF);
      {
       printf("Enter a grade: ");
       scanf("%f", &grades[i]);
      }
      
       average = findAvg(grades, NUMELS);
       sum = findSum(grades, NUMELS);
       printf("The sum of the grades is %f", sum);
       printf("The average of the grades is %f", average);
       for (i = 0; i < NUMELS; i++)
      {
       if (grades[i] < average)
       printf("*%lf", i);
       else if 
       printf("%lf", i);
      }
       for (i = 0; i < NUMELS; i++)
      {
       if (grades[i] >= 90)
       printf("%lf A", i);
       else if (grades[i] >= 80 && grades [i] < 90)
       printf("%lf B", i);
       else if (grades[i] >= 70 && grades [i] < 80)
       printf("%lf C", i);
       else if (grades[i] >= 60 && grades [i] < 70)
       printf("%lf D", i);
       else 
       printf("%lf F", i);
      }
       return 0;
    }
    double findAvg(double grades[], int numel)
    {
      int i;
      double sumnums = 0.0;
       for (i = 0; i < numel; i++)
       sumnums = sumnums + grades[i];
       return (sumnums/numel); /*calculate and return average*/
    }
    double findSum(double grades[], int numel)
    {
      int i;
      double sumnums = 0.0;
       for (i = 0; i < numel; i++)
       sumnums = sumnums + grades[i];
       return (sumnums); /*reutrn the sum*/
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
      int i;
       printf("Enter the grades, when finished enter a negative number:  ");
      
       while (grades < CUTOFF);
      {
       printf("Enter a grade: ");
       scanf("%f", &grades[i]);
      }
    1. What value is in i when this loop starts?
    2. What value is in i when this loop exits
    3. What's that ; at the end of the loop do anyway?
    4. Why are you comparing an array pointer with CUTOFF?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    For 1&2 I thought the first and last number the user enters
    3. The ; is not meant to be there
    4. The code is to allow the user to input grades until they enter a negative number to finish, should it be [i] instead?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by LearnOnTheFly View Post
    For 1&2 I thought the first and last number the user enters
    Why would you think that? The user hasn't even entered a number at the point it starts using the variable i.

    should it be [i] instead?
    I assume you mean grades[i], but why would you expect any of the grades to already be less than -1 when you haven't entered any yet?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Alright, so the code is supposed to allow the user to enter a negative number to stop entering grades for the array.
    Here is the example from my texterbook in that I found in repitition that made me think that would work

    Code:
    #define
    float grade = 0.01;
    float total = 0.01;
    
    printf("\nTo stop entering grades, type in any negative number. .\n\n");
    
    while (grade > CUTOFF)
    {
    printf("Enter a grade: ");
    scanf("%f", &grade: );
    total = total + grade;
    }
    Any help and reason why so I don't make this mistake again would be appreciated

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%f", &grade: );
    See, now you're just guessing aren't you.

    Every new post of yours takes out some old mistake and adds in some new mistake.

    Keep this up, and you're heading for being a troll, just posting endless mistakes to waste our time.

    READ the thread again, and pay attention - if you're at all serious about learning this stuff.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I meant to post #define CUTOFF -1 at the top of that code
    And it was an example from my TEXTBOOX not me repeating mistakes someone told me to fix. I was asking why the CUTOFF worked in that example for entering a negative number to exit but not in my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM