Arrays - Terminating loop with sentinal value

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 20940 Views - Last Post: 25 October 2010 - 05:25 PM Rate Topic: -----

#1 Guest_jrs7892*


Reputation:

Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 01:13 PM

Hi there.

I am new here and to programming in general so I hope I'm posting this correctly. I have a homework assignment with several parts to it, and I've got most of the program working, but I'm stuck. Was wondering if someone could lead me in the right direction. I'll post the question below and the code I've come up with so far. Thanks in advance for your help!!

Exercise:
Write a C program that reads a list of double-precision grades from the keyboard into an array named grades. The grades are to be counted as they are read, and entry is to be terminated when a negative value has been entered. Once all of the grades have been input, your program should find and display the sum and average of the grades. The grades should then be listed with an asterisk (*) in front of each grade that is below the average.

The teacher has told us to set the size of the array to something specific in the beginning(100 etc.), but to keep track of how many grades have been entered. I've been using 5 for now for testing purposes. I've gotten through most of the exercise, accepting grades, determining average and displaying total, averages and grades with/without the asterisk, but can't for the life of me figure out how to make the sentinel value work. My compiler keep crashing. I've tried a while statement before the first for loop and am just at a loss for what else to try at this point.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    #define SENTINEL -1
    #define NUMGRADES 5

    float grades[NUMGRADES];
    float total =0;
    float gradecount =0;
    float average;
    int i;
    
    
          for (i=0; i< NUMGRADES; i++)
          {
          printf ("Enter a grade: ");
          scanf ("%f", &grades[i]);
          total += grades[i];
          gradecount ++;
          
          }
    
    average = total/gradecount;
    printf("The sum of all the grades is: %.2f\n", total);
    printf("The average grade is: %.2f\n", average);
    
    printf("The grades are as follows: \n");
    for (i=0; i< NUMGRADES; i++)
        if (grades[i] < average)
           printf("* %5.2f\n", grades[i]);
        else
        printf(" %6.2f\n", grades[i]);
       
    system("PAUSE");
    return 0;
}



Is This A Good Question/Topic? 0

Replies To: Arrays - Terminating loop with sentinal value

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 01:29 PM

You're close!
Instead of looping from 0 to < NUMGRADES, you want to loop while the user input is >= 0 and while the number of grades entered is less than NUMGRADES.
Then in the output you want to loop to the number of grades entered, not NUMGRADES.

Making those changes to your code, I got this output:
Enter a grade: 75
Enter a grade: 85
Enter a grade: 95
Enter a grade: -2
The sum of all the grades is: 255.00
The average grade is: 85.00
The grades are as follows: 
* 75.00
  85.00
  95.00


This post has been edited by CTphpnwb: 24 October 2010 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 jrs7892   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 10

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 02:07 PM

Thanks so much for the quick reply!

Unfortunately, I'm still not getting it. I understand exactly what you're telling me to do (in English), just have trouble translating it to code. I was able to change the output part to loop to gradecount vs. NUMGRADES.. so that part is done. I'm just not sure how to go about the first loop. From your reply I'm thinking I need a while loop, but do I need to get rid of the for loop, or just put the while loop before it? When I try a while loop it crashes my compiler. I'm sorry if I seem dense, this stuff is just not coming to me very easily!

Thanks again for your help!
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 02:15 PM

The while() replaces the for():


	while(user_input >= 0 && gradecount < NUMGRADES)



user_input is my variable for what the user enters. I only add this to the array if it is not negative.
Was This Post Helpful? 0
  • +
  • -

#5 jrs7892   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 10

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 02:47 PM

Sigh.. still working.

I tried the same while loop (even before you posted it, with the exception that I used > SENTINEL instead of >= 0). This is what was crashing my compiler.. so I'm assuming I've messed something else up along the way. If I get rid of the for loop, the i is not initialized or incrementing, and I don't have the user input going into the array at all. Replacing the for loop with just the while loop doesn't address that.. or am I missing something?
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 03:14 PM

Can you repost your code as it is right now? (You made some modifications...)
Was This Post Helpful? 0
  • +
  • -

#7 jrs7892   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 10

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 03:25 PM

I'll post what I have, but I know it's very wrong. My book has nothing on arrays AND sentinel values.. and I just can't figure out how to get the user input into the array with the changes that I've made.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    #define SENTINEL -1
    #define NUMGRADES 5

    float grades[NUMGRADES];
    float total =0;
    float gradecount =0;
    float average;
    int i;
          
          while (grades[i] > SENTINEL && gradecount < NUMGRADES)
          {
                printf ("Enter a grade, or negative number to exit: ");
                scanf ("%f", &grades[i]);
                grades[i]++;
                total += grades[i];
                gradecount ++;
          }
    
    average = total/gradecount;
    printf("The sum of all the grades is: %.2f\n", total);
    printf("The average grade is: %.2f\n", average);
    
    printf("The grades are as follows: \n");
    for (i=0; i< NUMGRADES; i++)
        if (grades[i] < average)
           printf("* %5.2f\n", grades[i]);
        else
        printf(" %6.2f\n", grades[i]);
       
    system("PAUSE");
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 05:01 PM

The problem is that you aren't iteratively constructing a correct algorithm.

Let me show you. Let's say you didn't have to worry about a whole bunch of grades. You read 1 grade, or if you get a negative number, you ignore it.

Strip away the while loop, and you have:

printf ("Enter a grade, or negative number to exit: ");
scanf ("%f", &grades[i]);
grades[i]++;
total += grades[i];
gradecount ++;



Is this correct? Let's take an example. I type in 90. So you ask for a grade, on line 1. On line 2, you read into &grades[i ]. What is the value of i? int i; : You didn't specify a value for i. It could be anything. 5. 100. 1000. That's a problem. Let's assume the value is 2. So you read into grades[2].

Then, on line 3. you have grades[i ]++; . You increase the grade value by 1. Why are you doing this?

Let's take another example. I type in -100. So you should read my input, check and discover that it less than 0. And then do nothing. But look at line 2. You read directly into the array. Is this OK? Shouldn't you be reading and checking before storing anything in the grades array?

Notice how neither of these problems have anything to do with looping. The basic act of reading one grade is broken already. So fix that first.

This post has been edited by Oler1s: 24 October 2010 - 05:02 PM

Was This Post Helpful? 1
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 05:10 PM

Read the explanation by Oler1s and then look at my while statement:
while(user_input >= 0 && gradecount < NUMGRADES)


Base on what Oler1s has shown you, do you know why there is no array variable in my statement?
Was This Post Helpful? 0
  • +
  • -

#10 jrs7892   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 10

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 05:19 PM

I was actually just playing with the code I pasted earlier and added i = 0; before the while loop. I'm hoping this addresses what you've just explained to me. My compiler stopped crashing at least. I also moved the grades[i]++; down so it's not incrementing too early.

The program as I have it now is still not stopping upon entering a negative value. And the grade output at the end is messed up as well. I will keep working on it, but here is what I've changed so far.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    #define SENTINEL -1
    #define NUMGRADES 5

    float grades[NUMGRADES];
    float total =0;
    float gradecount =0;
    float average;
    int i;
          
          i=0;
          while (grades[i] > SENTINEL && gradecount < NUMGRADES)
          {
                printf ("Enter a grade, or negative number to exit: ");
                scanf ("%f", &grades[i]);
                total += grades[i];
                gradecount ++;
                grades[i]++;
          }
    
    average = total/gradecount;
    printf("The sum of all the grades is: %.2f\n", total);
    printf("The average grade is: %.2f\n", average);
    
    printf("The grades are as follows: \n");
    for (i=0; i< gradecount; i++)
        if (grades[i] < average)
           printf("* %5.2f\n", grades[i]);
        else
        printf(" %6.2f\n", grades[i]);
       
    system("PAUSE");
    return 0;
}



View PostCTphpnwb, on 24 October 2010 - 04:10 PM, said:

Read the explanation by Oler1s and then look at my while statement:
while(user_input >= 0 && gradecount < NUMGRADES)


Base on what Oler1s has shown you, do you know why there is no array variable in my statement?


I'm guessing this has to do with what you said earlier.. that the input won't be added to the array unless it is a positive value.
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 05:23 PM

Just making changes here and there isn't going to work. You need to iteratively build up a correct program. Can you correctly handle reading 10 grades if you can't read one grade correctly? I don't think so.

Setting i to 0 does solve one problem. But it ignores everything else I talked about in my post.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 05:36 PM

View Postjrs7892, on 24 October 2010 - 07:19 PM, said:

View PostCTphpnwb, on 24 October 2010 - 04:10 PM, said:

Read the explanation by Oler1s and then look at my while statement:
while(user_input >= 0 && gradecount < NUMGRADES)


Base on what Oler1s has shown you, do you know why there is no array variable in my statement?


I'm guessing this has to do with what you said earlier.. that the input won't be added to the array unless it is a positive value.


Yes, and maybe if I make my code more closely match your assignment you'll see it better:
while(user_input != SENTINEL && gradecount < NUMGRADES)

Now, my code will only stop adding grades if the user enters -1 (not just any negative number!) or they enter enough grades to reach the end of the array.
Here's a hint:
Inside my while loop I have an if statement. It checks the value entered by the user and does what?
Was This Post Helpful? 0
  • +
  • -

#13 jrs7892   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 10

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 05:47 PM

View PostOler1s, on 24 October 2010 - 04:23 PM, said:

Just making changes here and there isn't going to work. You need to iteratively build up a correct program. Can you correctly handle reading 10 grades if you can't read one grade correctly? I don't think so.

Setting i to 0 does solve one problem. But it ignores everything else I talked about in my post.


I'm not purposely ignoring everything you talked about. I don't understand how to apply it. I'm learning this from a book and my program was reading data fine until I started messing with the sentinel value. So I've been making adjustments one at a time. I understand that you know what you're talking about, but pointing out that my lack of understanding the basics (which I already know) is not helping. Thanks for your time anyway.
Was This Post Helpful? 0
  • +
  • -

#14 jrs7892   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 10

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 05:58 PM

View PostCTphpnwb, on 24 October 2010 - 04:36 PM, said:

View Postjrs7892, on 24 October 2010 - 07:19 PM, said:

View PostCTphpnwb, on 24 October 2010 - 04:10 PM, said:

Read the explanation by Oler1s and then look at my while statement:
while(user_input >= 0 && gradecount < NUMGRADES)


Base on what Oler1s has shown you, do you know why there is no array variable in my statement?


I'm guessing this has to do with what you said earlier.. that the input won't be added to the array unless it is a positive value.


Yes, and maybe if I make my code more closely match your assignment you'll see it better:
while(user_input != SENTINEL && gradecount < NUMGRADES)

Now, my code will only stop adding grades if the user enters -1 (not just any negative number!) or they enter enough grades to reach the end of the array.
Here's a hint:
Inside my while loop I have an if statement. It checks the value entered by the user and does what?



If the value entered is positive and < NUMGRADES it will put it into the array. So I should have the while loop, with an if loop inside that? When the program reads the user input (the scanf part) should I be storing this value under a separate variable, because right now it's going directly into the array only because I don't know how else to store it?
Was This Post Helpful? 0
  • +
  • -

#15 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Arrays - Terminating loop with sentinal value

Posted 24 October 2010 - 06:08 PM

Quote

I'm not purposely ignoring everything you talked about. I don't understand how to apply it.
Ok, let me repeat my questions.

1. "Then, on line 3. you have grades[i ]++; . You increase the grade value by 1. Why are you doing this?"
2. "You read directly into the array. Is this OK? Shouldn't you be reading and checking before storing anything in the grades array?"

If there's anything you don't understand about the questions, feel free to ask for clarification. But...you do need to answer them.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3