views:

498

answers:

5

Hey guys,

I know in C you can use do-while for single integers and characters but I was wondering if it is possible to use the same do-while feature for whole groups of numbers?

EDIT: Okay i am really confused as to what this homework question is asking. But i think i get the question, just don't know what function will work. It wants the user to input several temperatures. The program will then take the temperatures and check them with three categories. These three categories are divided by how hot or cold it is. 85 or higher is 1, 60-84 is another, and less than 60 is another. I think instead of printing out the numbers again, it will just tally the temperatures into each category and then give the total number for each category at the end. Does that make any sense? Can anyone tell me where to start?

Okay guys, I've got this so far:

    #include <stdio.h>

int main (void)
{

    int temp;
    double average;
    int hot_days, pleasant_days, cold_days;
    char x;
    hot_days =  0;
    pleasant_days = 0;
    cold_days =     0;

    printf("Enter the temperatures you wish to be categorized, each followed by a space.\n\n");

do
{
    scanf("%d", &temp);
    if (temp >= 85)
     hot_days++;
    if (temp < 60)
     cold_days++;
    else   pleasant_days++;
{

while(x != 'q')
printf("Insert 'q' and press return.");
scanf("%c", &x);

printf("\n*************\n\nHot Days: %d\nPleasant Days: %d\nCold Days: %d\n",
     hot_days, pleasant_days, cold_days);



    return 0;

}
A: 

like this ?

int x=1,char c ='a',float y=0.1f;

do
{
 /* do something */
} while(x == 1 && c == 'a' && y == 0.1f);
Andrew Keith
I mean inputting multiple ints into the program, then having the output be all the numbers that were input in different categories
Chandler
+1  A: 

like this?

int num;
char c;

do
{
  printf ("Input number: ");
  scanf("%d",&num);

  //do your thing here with num. Store it, categorize it, etc...
  //e.g -pseudocode
  if (num below 59) print num, category 1
  else if (num between 60,84) print num category 2
  else print num category 3

  printf("Continue?");
  scanf("%c",&c);
}while (c!='q');

//Now write a do while loop to print out your numbers.
Tom
How would you print out your numbers if you haven't stored them anywhere? The only number you'd have is the last one.
Chris Lutz
Thats where the "do your thing with num" comes into action. Dont wanna give away the farm.
Tom
how do i do this with multiple numbers? not just one
Chandler
oh, the while-do loop
Chandler
+1  A: 

One way to handle tallying the categories is to create an array with the same number of elements as categories, then increasing the count each time you hit a value that should be in that category bucket. So, if the number is 85 or higher, increase the zeroth element in the array, if it's 60-84, increase the first element, etc.

(And don't forget to 0 out values in the array prior to increasing their value. Otherwise you'll end up with very weird results.)

EDIT: Quick and dirty pseudocode:

int categories[3];

zero_out_categories_array;

do
  int value;

  read_into value;

  if (value >= 85)
    categories[0]++;
  else 
    if (value >= 60)
      categories[1]++;
    else
      categories[2]++;
while not_done;

print "High: " + categories[0];
print "Medium: " + categories[1];
print "Low: " + categories[2];
Michael Todd
Can you clarify on this? I think this is where I need to go.
Chandler
Chandler: Merge this answer with mine below , and i think you are done.
Tom
I can't get the loop to stop executing
Chandler
What does the loop look like, i.e. what's after the "while"?
Michael Todd
+2  A: 

In response to your new edits, the question is much clearer, and fairly simple to do:

  1. First, we need three ints (or unsigned ints depending on your preference). Let's call them highs, middles, and lows. We'll use them to keep track of how many temperatures of a given temperature range we've seen. Alternatively, we could use an array, or a struct, but that's overkill for now.
  2. Secondly, we need to loop. I don't know what signal your assignment says should mean "end of input," but that's what the loop condition should be.
  3. Inside the loop, you should read in a number, check to see what range this number is in, and increment the appropriate counter variable (e.g. high++ if the number is < 80).
  4. After we've read in the number and incremented the counter, we should probably check for the end conditions (i.e. ask the user "Are you done?" or whatever) and then end the loop.
  5. Once we're out of the loop it's a simple matter of printing out the values of our three variables.
Chris Lutz
so instead of trying to declare the int before hand, i should declare the categories as ints? I never thought of that
Chandler
You can declare the `int` you need in the loop _in the loop_ which is a good practice, since you won't need it outside the loop. The loop creates a new scope, and you can create variables in that scope. Use it!
Chris Lutz
when you say loops do you mean things like for if and else statements?
Chandler
Well, yes. `if` / `else` isn't usually considered a loop since it only executes once, but anything that uses brackets (including if, else, while, for, do/while, switch, and even just random brackets) creates a new scope in which you can declare new variables.
Chris Lutz
should i create seperate loops for each category?
Chandler
No. Each number can only go into one category, so you create only one loop that reads numbers, and use `if` / `else if` / `else` to decide what category it goes into.
Chris Lutz
I can't get the loop to stop executing
Chandler
A: 
int main (void)
{
    int temp;
    double average;

    hot_days      = 0;
    pleasant_days = 0;
    cold_days     = 0;

    printf("enter the temperatures you want to be categorized. Press Enter after each. -1 to end.");

    do
    {
        scanf("%d", &temp);

        if (temp == -1)
            break;

        if(temp >= 85)
            hot_days++;

        if(60 < temp && temp <= 84)
            pleasant_days++;

        if(temp <= 60)
            cold_days++;
    } while (temp != -1);

    printf("%d Hot days, %d Pleasant Days and %d Cold Days\n",
        hot_days, pleasant_days, cold_days);
}
abelenky