2

So when I enter the input of positives or positives and negatives the code works fine, but just entering negatives result in floating point error, I know dividing by zero would result in this, but I'm diving by the number of inputs

#include <stdio.h>
int main()
{
    int integer, pos, neg;
    int poscounter, negcounter;
    integer = 0;
    pos = 0;
    neg = 0;
    poscounter = 0;
    negcounter = 0;

    do {
        printf("Please enter an integer:");

        scanf("%d", &integer);

        if (integer > 0) {
            pos += integer;
            poscounter++;
        }
        else
            neg += integer;
        negcounter++;

    } while (integer != 0);

    printf("Positive average: = %d", pos / poscounter);
    printf("Negative average: = %d", neg / negcounter);
}

So the output of entering -3 -2 -1 0 should result in "Negative average: -2"

TheParam
  • 10,113
  • 4
  • 40
  • 51
Luis
  • 21
  • 3
  • 1
    If `poscounter` is zero you're dividing by zero. – tkausl Feb 07 '19 at 03:49
  • 2
    Note that `negcounter` is incremented always. It's not part of the else block. You need to use `else { neg += integer; negcounter++; }` – drescherjm Feb 07 '19 at 03:51
  • 1
    Possible duplicate [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – n. m. could be an AI Feb 07 '19 at 03:59

3 Answers3

1
#include <stdio.h>
int main()
{
    int integer, pos, neg;
    int poscounter, negcounter;
    integer = 0;
    pos = 0;
    neg = 0;
    poscounter = 0;
    negcounter = 0;

    do {
        printf("Please enter an integer:");

        scanf("%d", &integer);

        if (integer > 0) {
            pos += integer;
            poscounter++;
        }
        else if(integer < 0)    // Added else if for the logic as it was considering 0 as negative
        {
            neg += integer;
            negcounter++;
        }

    } while (integer != 0);
    printf("posc = %d\n", poscounter);      // Printed for confirmation
    printf("negc = %d\n", negcounter);      // Printed for confirmation

    /* Added these two ifs so that it can check any of the counters is not zero and it will not give (core dumped) */

    if(poscounter)
        printf("Positive average: = %d\n", pos / poscounter);
    if(negcounter)
        printf("Negative average: = %d\n", neg / negcounter);

        return 0;       // Adding this is a good practice
}

Hey there their was a bit problem in the logic as when positive/negative isn't encountered it was dividing the expression by 0. Moreover it was considering 0 as negative integer. Please go through comments it may help

Mann
  • 89
  • 1
  • 7
  • why does adding the if statement " if(poscounter)" work is it basically saying if theres any value in poscounter print the following?? – Luis Feb 07 '19 at 04:33
  • 1
    See, in there, your `poscounter` is used as dividend, so if it is zero, it will produce error, the one which you're getting. First `pos/poscounter` is evaluated and then `printf()` is using it. – Mann Feb 07 '19 at 05:11
  • if you find this answer useful then please approve it :) – Mann Feb 07 '19 at 06:05
1

This is crashing with the input -3,-2,-1,0 because if (integer > 0) is false always, so poscounter will not be incremented any time, so poscounter will be ZERO after while loop.

printf("Positive average: = %d", pos / poscounter); will make program crash because of the decided by ZERO operation.

Better to have a if check for the denominator and make sure that it is not ZERO before using in devide operation:

if (0 != poscounter)
     printf("Positive average: = %d", pos / poscounter);

this will make sure printf will be done only in valid cases when poscounter is a non zero value.

Deepak
  • 86
  • 9
1

if (integer > 0) is never executed, so poscounter is never incremented, so finally the division pos / poscounter cannot work.

alinsoar
  • 15,386
  • 4
  • 57
  • 74