Home > database >  How do I get the input numbers to be counted?
How do I get the input numbers to be counted?

Time:09-26

The way the program is supposed to run is count out how many numbers the user inputs and outputs how many are in that group.

#include <stdio.h>

int main () {
    
    int input;
    int sum = 0;
    int count = 0;
    int hiprc, avgprc, loprc;
    int avg;
    
    while (input != -99)
    {
        printf("Enter the price (-99 to stop): ");
        scanf("%d", &input);
        sum = sum   input;
        count  ;
    }

    if (hiprc = input >= 85)
    printf("Number of high prices is \n", hiprc);
    
    if (avgprc = hiprc < input && input > loprc)
    printf ("Number of average prices is \n", avgprc);
    
    if (loprc = input >= 60)
    printf ("Number of low prices is \n", loprc);

    avg = sum/count;
    printf("Average Price is %.2f\n", avg);
    
    return 0;
}

When I run it, it displays the highest price, adds the average prices, and outputs 0 for the low prices. It should be outputting how many numbers there are for the high, average and low.

High prices: 4
Average Prices: 15
Low Prices: 3

The average price is: xx.xx

this was edited with some of the comment suggestions

CodePudding user response:

if(hiprc < input > loprc) is not the same meaning as in mathematics.

Use if(hiprc < input && input > loprc) instead.

CodePudding user response:



int main ()
{

    int input=0;
    int sum = 0;
    int count = 0;
    int hiprc=0, avgprc=0, loprc=0;

    printf("Enter the price (-99 to stop): ");
    scanf("%d", &input);

    while(input != -99)
    {
        sum = sum   input;


        if(input > hiprc)
        {
            hiprc = input;

        }
        else if(input < loprc)
        {
            loprc = input;

        }

        printf("Enter the price (-99 to stop): ");
        scanf("%d", &input);

        count  ;

    }

    avgprc = sum / count;

    printf("%d\n", avgprc);
    printf("%d\n", hiprc);
    printf("%d\n", loprc);
}

Try this format instead, this wont return a float since its all decimals.

  •  Tags:  
  • c
  • Related