Ben
Ben

Reputation: 1

My C code that is supposed to take positive values and end when -1 is inputted, and print the largest number is not working

Hi I am wondering why my code is not working. Here are the instructions for it: Suppose we want to determine the largest number of a series of non-negative real values entered by the user. The user repeatedly enters the values, one at a time, and finally enters the value −1 to indicate that there are no more values to enter. We then print the largest value entered by the user, on the screen. If the first number entered by the user is −1 (i.e., the user enters no non-negative values), then we’ll print the message “Error: no data!” on the screen. (Your program doesn’t have to verify that the numbers are non-negative.).

Now here is my code. For reference, some combinations of numbers work such as 5 5 5 5 -1, and 1 2 3 4 5 10 -1. Other combinations don't work such as 1 -1, 1 2 2 3 4 5 10 -1.

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL -1

int main(void) {
    
    double value = 0;
    double value2 = 0;
    double large = 0;
    
    printf("enter your values: ");
    scanf("%lf %lf", &value, &large);

    if (value == SENTINEL) {
        printf ("Error: no data!");
    } else {
        while (value != -1) {
            printf("enter value: ");
            scanf("%lf", &value2);
    
            if (value2 > value) {
                large = value2;
            }
    
            value = value2;
        }
    
        printf("%f\n", large ); 
    }
    return 0;
}

Upvotes: 0

Views: 289

Answers (4)

chqrlie
chqrlie

Reputation: 144695

The program does not work as coded because you compare the value to value2 and update value2, you should instead compare to large and update the same variable.

Furthermore, you read 2 values in the first step and assume the second is larger than the first, ad the second is never tested against SENTINEL.

You should simplify the code:

  • read one value at a time
  • initialize large to a value that will change if the first value read is not SENTINEL
  • update large if the value read is larger.

Here is a modified version:

#include <stdio.h>

#define SENTINEL (-1)

int main(void) {
    double largest = SENTINEL;
    double value;
    
    printf("enter your values: ");
    while (scanf("%lf", &value) == 1) {
        if (value == SENTINEL)
            break;
        if (largest < value) {
            largest = value;
        }
    }
    if (largest == SENTINEL) {
        printf("Error: no data!\n");
    } else {
        printf("%f\n", largest);
    }
    return 0;
}

Also note that comparing floating point values for equality is not recommended as the comparison can fail due to infinitesimal differences coming from limited precision on intermediary results. In this case it is not an issue, but since the input are supposed to be non negative, testing for a negative input seems safer:

#include <stdio.h>

int main(void) {
    double largest = -1.0;
    double value;
    
    printf("enter your values: ");
    while (scanf("%lf", &value) == 1) {
        if (value < 0.0)
            break;
        if (largest < value) {
            largest = value;
        }
    }
    if (largest < 0.0) {
        printf("Error: no data!\n");
    } else {
        printf("%f\n", largest);
    }
    return 0;
}

Upvotes: 1

SGeorgiades
SGeorgiades

Reputation: 1821

Try something like this:

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL -1

int main(void)
{
    double value = 0;
    double maxval = -1.0;
    int count = 0;
    int stat;

    while (value != SENTINEL) {
        printf("Enter a values (-1 when done)");
        do {
            printf(": ");
            stat = scanf(" %lf ",&value);
        } while (stat != 1);
        if (value != SENTINEL) {
            if (value > maxval)
                maxval = value;
            count++;
        }
    }

    if (count == 0)
        printf ("Error: no data!");
    else
        printf("%f\n",maxval); 
    return 0;
}

Upvotes: 0

Patrik Nespor
Patrik Nespor

Reputation: 11

I did not see C language for a long time, but I think that something like this might work:

double value = SENTINEL;
double large = SENTINEL;

printf("enter your values: ");
while (scanf("%lf", &value) != -1) {
    if (value > large)
        large = value;
        
    if (value == SENTINEL)
        break;
}

if (large == SENTINEL)
    printf("Error no data\n");
else
    printf("%lf \n", large);

return 0;

Upvotes: 1

Kcabus
Kcabus

Reputation: 1

To help you, try to print values you read. It seems you use double and you compare it to an int (-1). If numbers given are only integers use the according variable type.

Upvotes: -1

Related Questions