Chandler
Chandler

Reputation: 45

C programming if statement

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;

}   

Upvotes: 0

Views: 2253

Answers (5)

Michael Todd
Michael Todd

Reputation: 17081

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];

Upvotes: 1

abelenky
abelenky

Reputation: 64730

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);
}

Upvotes: 0

Chris Lutz
Chris Lutz

Reputation: 75469

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.

Upvotes: 2

Tom
Tom

Reputation: 45174

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.

Upvotes: 1

Andrew Keith
Andrew Keith

Reputation: 7563

like this ?

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

do
{
 /* do something */
} while(x == 1 && c == 'a' && y == 0.1f); 

Upvotes: 0

Related Questions