Thread: Can anyone tell me what's wrong with this program?

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

    Can anyone tell me what's wrong with this program?

    Hi, we have just begun studying arrays and functions, and I have spent the weekend working on this, what is relatively very simple, source.

    Can anyone help me fix this? I have been trying to change it to include two functions, findLowest and findHighest. They are supposed to print the highest and lowest of ten grades at the end of the program.

    Here's the source:

    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[N])
     {
      float i=0;
      float  low=100.0;
       for (i=0;i<N;i++)
       if (grades[i] < low) low = grades[i];
     printf("The lowest score is: %.2f\n", low);
    
      return 0;
     }
    
     float findHighest(float grades(N))
     {
      float i=0;
      float high = 0.0f;
       for(i=0;i<N;i++)
    "P4_2.c" 68 lines, 1388 characters
       if (grades[i] > high) high = grades[i];
     printf("The highest score is: %.2f\n", high);
    
      return 0;
     }
    
    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
    
     findHighest(i);
     findLowest(i);
    return 0;
     }

    Thank you!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is there something wrong with it? If so, what? (Or at least, what do you notice that is incorrect?)

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Passing an integer to functions that expect an array of floats...not gonna work.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    where do you see an integer? Everything is defined as being float? (the N?)

    Also, I thought integers were converted to floats if needed?

    Okay...so the i is the grades being stored in the array, yes?

    So does that make the call of the functions at the end correct at least?
    What do I need to do, write #define N 10.0f ? I've never seen that done with any of our examples, it's always been just a constant. How does this work?


    As far as I can tell, I am passing i, which is float, into an array of floats?


    Come on, shed some light, I've spent over 20 hours on this one thing, adding functions, just to get 10 extra points. I have calc III test next week, and chem, and cmp science test. I really need to finish this already. It's the weekend, can't call the professor.
    Last edited by LightYear; 04-10-2010 at 06:01 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    float grades(N)
    That's not an array parameter! Post your compile errors next time.

    Don't use non-void return types if you aren't going to return anything meaningful, or take notice of the return value in the caller.
    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"

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    I didn't have float grades(N) earlier, but in another thread a more experienced poster suggested that be my prototype...that's what led to all this confusion I think.

    Why isn't it N? This is an array of 10 numbers, and N is 10.


    I am wondering, does #define N 10 make N an integer 10?

    Then that N integer is being stored into a float array? Is that a problem or will integer upconvert to float in this case?


    An off topic question; does anyone use a debugger, are they helpful for simple programs like this? We were told not to use them
    Last edited by LightYear; 04-10-2010 at 06:18 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    1) You need to add the variable lowscore to the list of floats, in main().
    2) you need to add highscore to the parameter list for findHighest()
    3) you need to add lowscore to the parameter list for findLowest()
    (by this I mean in the call to the function, and in the functions parameters list, both)

    4) You need to return the highscore (or lowscore for findLowest), from these two functions.

    5) The grades array only deals with 1 students grades. So lowscore and highscore have to be called (both of them), after every student's grades has been entered, but before another students grades start being entered, into the grades[] array.
    They are both "running" totals, that have to be updated for each student's grades.

    That should get you closer.
    Last edited by Adak; 04-10-2010 at 07:14 PM.

  8. #8
    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[N])
     {
      float i=0;
      float  low=100.0;
       for (i=0;i<N;i++)
       if (grades[i] < low) low = grades[i];
     printf("The lowest score is: %.2f\n", low);
    
      return 0;
     }
    
     float findHighest(float grades(N))
     {
      float i=0;
      float high = 0.0f;
       for(i=0;i<N;i++)
     if (grades[i] > high) high = grades[i];
     printf("The highest score is: %.2f\n", high);
    
      return 0;
     }
    
    int main(void)
    {
    
     float grade[i], 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
    
     findHighest(i);
     findLowest(i);
     return 0;
     }

    Compiler errors:


    God Admiral is being so slow. I will post errors later if I can't get this to work
    between now and then. I need a break...


    Adak (and claudiu) you're my hero

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry about my 'lil screw-up earlier, LightYear, I hope I did not screw up too much.

    Quote Originally Posted by iMalc View Post
    That's not an array parameter! Post your compile errors next time.
    I was going to say something along these lines before. It looks to me like you are trying to "compose" most of a program and then compile it. That is a bad way to write. You should compile (and test, if possible) every 5-10 lines you write, even if this means having to shim something in temporarily to make it fit together, because that saves more time than trying to demangle 50 lines of untested code. So even if the compiled program doesn't do anything, you should still do this. Compile every 5-10 lines with warning on and pay attention to the error and warnings, which it the point of test running something thru the compiler.

    To try and make up for my mistake, I corrected the errors enough in your code (from post #1) to make it compile and run.
    Code:
    #include <stdio.h>
    
    #define N 10
    
    float findLowest(float grades[N]) 
    {
    	int i=0;	// NOTE ONE
    	float  low=100.0;
    	for (i=0;i<N;i++)
    	if (grades[i] < low) low = grades[i];
    	printf("The lowest score is: %.2f\n", low);
    
    	return 0;
     }
    
    float findHighest(float grades[N])	// NOTE TWO
    {
    	int i=0; //NOTE ONE
    	float high = 0.0f;
    	for(i=0;i<N;i++)
    	if (grades[i] > high) high = grades[i];
    	printf("The highest score is: %.2f\n", high);
    
    	return 0;
    }
    
    int main(void)
    {
    
    	int i;  //NOTE ONE
    	float grade[N], highscore, average_score, sum;
    	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 %d's grade is %.1f, this is an %c\n", // NOTE THREE
    			i+1, grade[i], lettergrade);
    	}
    
    	printf("the average score is %.2f\n", average_score); //prints average
    
    	findHighest(grade);   // NOTE FOUR
    	findLowest(grade);
    	return 0;
    }
    Here's what was wrong:

    NOTE ONE:
    If you use i as an array subscript, i must be an integer. There is no such element as "array[2.4]", so floats are not allowed.

    NOTE TWO:
    We've been thru this one -- () vs. [], it's more or less a typo.

    NOTE THREE:
    Your conversion specifiers should match your types. You had i as a float, but it need to be an int, so I changed a %f to a %d appropriately here.

    NOTE FOUR:
    This one is kind of interesting. Why do you think you should be calling those "find" functions with i, instead of the array, grade?

    [edit] just glancing thru the recent posts and noticed Adak is now doing the same thing...well, you'll get a few opinions then...
    Last edited by MK27; 04-10-2010 at 06:30 PM.
    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
    Dec 2007
    Posts
    2,675
    I don't even have a clue why i *is* a float. It's an index for your array.

    Even so, if you don't know the difference between a float and an ARRAY of floats, I doubt you *deserve* extra credit.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Ouch that burns. As someone who is just learning this, let me say you appear tactless and rude. Being a wise ass doesn't do anyone any good does it?

    However, I do see that i is only used as an index now, so I can make that an integer? I received conflicting information on that, and being new to this, it confused me as to what exactly is going on in these functions (storing variable x (or y, z anything) into a variable with a different name) and in the arrays (same problem.)

    a float is an approximation allowing for the use of decimals. In size, char is smallest, followeed by signed short int, then int, then float, then double, then long double, then unsigned long double. I may have left a couple out.


    An array can store floats, but its range has to be an integer, the number of boxes it contains. This is right yea?



    The problem I've been having is in the logic of all of this.

    Thanks MK, that is what I needed. You see rags, I can print something like he has posted, and keep it in my notes. Helps my learning style.
    Last edited by LightYear; 04-10-2010 at 06:41 PM.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Or you can hand it in as your own work and get the "extra credit" you're looking for. Call me tactless and rude, I don't care. You're a cheater who used the generosity of several people here to write your program for you.

    A good read for you

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    That's cool MK27 thank you. I am scrolling up and down, looking at mine, looking at yours.

    I am glad I had the logic right mostly. You know when you're just learning something, a small error can influence your judgment and throw you wildly off course. That's where I was before you came on.

    That typo () that's a bad habit I'm working on it heh

    As for note four; that is something I've been having a problem remembering, but this is only going to be week two of arrays, this is helping me get it down.t

    I have been trying to write the whole thing and compile. You make a good point about that, don't really know what to say. I will consider and implement that advice. I had the program running perfect without the functions, but as I went back in and started trying to add functions (which I am just learning), then changed everything from double to float. Each of these steps added errors.
    Looking at my text, the chapter on functions has very poor examples. I will have to find more online.



    rags to riches you're an asshat. I didn't post anything without first trying it. I have tests in this course that no one can help with. The point of these projects is to learn to program so that you can do it from memory ON the tests.
    This is a great resource, and you do a disservice to everyone when you post the way you do.



    Why don't you go pull the legs off a spider or something...troll
    Last edited by LightYear; 04-10-2010 at 07:06 PM.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Also, this thread will be useful to anyone compiling something similar, as it clearly illustrates the steps with which an inexperienced newbie programmer might go in walking the path to wisdom, so to speak.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are many ways that this program could be done. This is how I'd do it to handle lowscore, and get the rest of the tweaks it needed, corrected.

    findHighest() should be a near-copy of findLowest(), and I leave that undone.

    You know that this program will only process one student, right? It handles 10 scores, but that's only enough for 1 student.

    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
    
    void findLowest(float grades[N], float *lowscore)
     {
      int i=0;  
      for (i=0;i<N;i++) {
       if (grades[i] < *lowscore) 
         *lowscore = grades[i];    
     }
    }
    
    /* float findHighest(float grades(N))
     {
      float i=0;
      float high = 0.0f;
       for(i=0;i<N;i++)
     if (grades[i] > high) high = grades[i];
     printf("The highest score is: %.2f\n", high);
    
      return 0;
     }
    */
    int main(void)
    {
     char lettergrade;
     int i;
     float lowscore, highscore, average_score, sum, studentname, numraise;
     float grade[N] = { 0 };
     lowscore = 0.0;
     highscore=0.0;
     sum=0.0;
    
     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 < N; 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 %.1f, this is an %c\n", i+1, grade[i], lettergrade);
     }
     average_score= sum/N;
     printf("the average score is %.2f\n", average_score); //prints average
    
     lowscore = highscore = grade[0];
    // findHighest();
     findLowest(grade, &lowscore);
     printf("the lowest score is %.2f  the highest score is ", lowscore);
    
     return 0;
    }
    Good luck with your exams, LightYear. Rags means well, just doesn't emphathize atm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM