kingslayer42
kingslayer42

Reputation: 11

Print the positive, negative, and all entries made using while loop

So I've tried to calculate the average of the positive and negative entries made in the while loop but I can't seem to enter any type of number into the console. So I need to enter numbers as long as it's not zero and add the positive numbers as well as negative numbers. I'm required to count how many positive and negative entries were entered. I need also to print the average of the positive and negatives entries.

Here is my code:

int num; 
int positivenum = 0, negativenum = 0;
int cpositive = 0, cnegative = 0;
float average = 0;
printf("Enter a positive and negative integer:");
while (num!=0)
{
scanf("%d", &num);
    if (num > 0){
    positivenum += num;
    cpositive++;
    }
    
    else if (num < 0){
    negativenum += num;
    cnegative++;
    }

Upvotes: 1

Views: 885

Answers (3)

user9381360
user9381360

Reputation:

#include <stdio.h>

int main() {
    int num; 
int positivenum = 0, negativenum = 0;
int cpositive = 0, cnegative = 0;
int range = 0;
int i = 0;
printf("Enter the range.\n");
scanf("%d", &range);
printf("Enter a positive and negative integer:\n");
do
{
    scanf("%d", &num);
    if (num > 0){
        positivenum += num;
        cpositive++;
    }
    
    else if (num < 0){
        negativenum += num;
        cnegative++;
    }
    i++;
} while ( (num!=0) && (i < range));

if (cpositive > 0)
    printf("Average of positive numbers: %f\n", (float)positivenum / (float)cpositive);

if (cnegative > 0)
    printf("Average of negative numbers: %f\n", (float)negativenum / (float)cnegative);

}

I have also added a range to break the loop.

Upvotes: 0

user9706
user9706

Reputation:

Changed the type of your counts from int to unsigned (negative counts doesn't make sense). Also moved the prompt inside the loop and and wrote a function to calculate the average:

#include <stdio.h>

void average(const char *what, int value, unsigned count) {
    if(!count) return;
    printf("average of %s numbers: %f\n", what, (float) value / count);
}

int main() {
    int num;
    int positivenum = 0, negativenum = 0;
    unsigned cpositive = 0, cnegative = 0;
    for(;;) {
        printf("Enter a positive and negative integer: ");
        scanf("%d", &num);
        if(!num) break;
        if(num < 0) {
            negativenum += num;
            cnegative++;
            continue;
        }
        positivenum += num;
        cpositive++;
    }

    average("positive", positivenum, cpositive);
    average("negative", negativenum, cnegative);
}

Upvotes: 0

Antonin GAVREL
Antonin GAVREL

Reputation: 11249

This is exactly when do while is judicious:

int num; 
int positivenum = 0, negativenum = 0;
int cpositive = 0, cnegative = 0;
printf("Enter a positive and negative integer:");
do
{
    scanf("%d", &num);
    if (num > 0){
        positivenum += num;
        cpositive++;
    }
    
    else if (num < 0){
        negativenum += num;
        cnegative++;
    }
} while (num!=0);

if (cpositive > 0)
    printf("Average of positive numbers: %f\n", (double)positivenum / (double)cpositive);

if (cnegative > 0)
    printf("Average of negative numbers: %f\n", (double)negativenum / (double)cnegative);


Upvotes: 1

Related Questions