-2

I need to write a program where users can input their numbers as much as many as they defined, then the program will try to find which one is the lowest value and the highest value. The problems I face are:

  1. When the program executed, the second line will wait on user's input (number) before the printf
  2. The error "system" seems unreliable, sometimes works, sometimes doesn't work
  3. The program only checks the last number entry, therefore it only shows the last number in min and max

You may give hints or corrections along the answers. Thank you very much.

#include <stdio.h>
float max(float num1){
    float a=0, b;
        if(num1 > a){
            a=num1;
        }
    return a;
}

float min(float num2){
    float x=100, y;

        if(num2 < x ){
            x=num2;
        }

    return num2;
}

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : ");
    scanf("%d\n",&times);

    printf("Type in the number: ");
    scanf("%f", &data_Input);

    for(interval=2; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
    
    }
    maxi= max(data_Input);
    mini= min(data_Input);

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}

Output:

How many number would you like to type in? : 5
70
Type in the number : 
Type in the number : 90.7
Type in the number : 99
Type in the number : 30
Type in the number : 50
The Lowest Number is 50.00
The Highest Number is 50.00
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Exc4pe
  • 21
  • 1
  • 3
  • What do you mean with the sentence _"The error "system" seems unreliable"_? – Roberto Caboni Oct 09 '20 at 07:31
  • Welcome to SO. You seem to use `a` and `x` as storage for current min/max values and you seem to expect that you can use this between function calls. You might read again about lifetime of variables in functions. And about `static` keyword. – Gerhardh Oct 09 '20 at 07:31
  • If you want to check every input, you should call your functions with every input. Also `min` should probably return `x´. – Gerhardh Oct 09 '20 at 07:31
  • also, read how scanf handles newlines. – Sinking Oct 09 '20 at 07:43
  • For the user interaction, consider to suppress the "\n" in the scanf() calls otherwise, it hangs (cf. https://stackoverflow.com/questions/40948635/input-reading-using-scanf-hangs). Moreover as printf() is a buffering function. If you call it without terminating "\n", you may face no display sometimes. To avoid this, call fflush(stdout) right after each printf() without "\n" – Rachid K. Oct 09 '20 at 07:44
  • See also [MIN and MAX in C](https://stackoverflow.com/questions/3437404/min-and-max-in-c) -- you cannot initialize the min and max checks as you are doing without missing approximately `10E38` cases for `float`. What are the variables `b` and `y` used for? Always compile with *warnings enabled*, and **do not** accept code until it *compiles without warning*. To enable warnings add `-Wall -Wextra -pedantic` to your `gcc/clang` compile string (also consider adding `-Wshadow` to warn on shadowed variables). For **VS** (`cl.exe` on windows), use `/W3`. All compilers have similar options. – David C. Rankin Oct 09 '20 at 07:50
  • @RobertoCaboni What I meant is the warning code at printf(Invalid Input!...) – Exc4pe Oct 09 '20 at 13:07
  • @Gerhardh thank you for the welcome. Yeah, after I re-read, I still can't understand the concept behind it. Maybe I'll do it later on – Exc4pe Oct 09 '20 at 13:11
  • @RachidK. Ah, thanks for the opinion and the input, too – Exc4pe Oct 09 '20 at 13:12

5 Answers5

1

Okay, the thing is that you are not updating the data_input after every successive number is inputted. What you are doing is, comparing the last number to 0 or 100, which is logically incorrect. How about you take the first number as input, then after every successive input, compare it with the min and max value. Here is the sample code.

#include <stdio.h>
float max(float num1, float num2){
    if(num1 > num2){
        return num1;
    }
    return num2;
}

float min(float num1, float num2){
    if(num1 < num2){
        return num1;
    }
    return num2;
}

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : ");
    scanf("%d\n",&times);

    printf("Type in the number: ");
    scanf("%f", &data_Input);
    
    // the first number will be minimum and maximum
    mini = data_Input;
    maxi = data_Input;

    for(interval=2; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        // make it a composite if condition
        while(data_Input<0 || data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        maxi= max(maxi, data_Input);
        mini= min(mini, data_Input);
    
    }
    

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}
  • Ah, I've recalled the code along the lines. The only thing I can't understand is from the float function. Why return num2 at float max() when you already have return num1? Also, is it appropriate to use the same variable between two functions (at float max and float min)? – Exc4pe Oct 09 '20 at 12:56
  • To answer the latter part of the function, it is absolutely okay to use the same variables between two functions because the value of the variable we are passing is not being changed. – Muhammad Junaid Haris Oct 12 '20 at 06:35
  • And, for the former part of the question. The function max is supposed to return the maximum of two number. We first check if number1 is greater i.e. maximum, fair enough return it. And, if that block is not executed it means that number2 is greater, so we simply return it. So, now the function max() returned us the maximum of two variables, and we store it in our maxi variable like maxi = max(maxi, x). Now, maxi is not being changed it the function max so there is no issue in using it like that. Hope that helps. – Muhammad Junaid Haris Oct 12 '20 at 06:38
0

The program checks the last number because you are calling the min and max function out of the for bracelets so instead you can call them inside the for bracelets like:

for(interval=2; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        maxi= max(data_Input);
        mini= min(data_Input);
    }

and instead of rewriting the same code you can just ask for the numbers inside the for loop and to initialize your interval to 1 so your main will look like:

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : ");
    scanf("%d\n",&times);

    for(interval=1; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        maxi= max(data_Input);
        mini= min(data_Input);
    }

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}
  • yes, you are right! @MuhammadJunaidHaris you can see the right max and min function on the answer above@Exc4pe – Majd Zaatra Oct 09 '20 at 07:43
  • Yeah, I neither have fully grasped the logic behind function nor the logic of storing and comparing a value from variable. Thank you – Exc4pe Oct 09 '20 at 13:02
0

Solving the printf issue is easy. As stdout is line buffered (at least by default - it can be changed) it is flushed whenever a newline is inserted in the buffer. So, just add a newline after each message print, for example

printf("How many number would you like to type in ? : \n");

and you'll be fine.


Talking about the wrong calculation of min and max, your attempt has basically two big issues:

  • You are not acquiring times inputs, but only one. In fact, every time you call scanf you overwrite the same variable data_Input without performing any comparison with the previous inputs
  • You call min() and max() function only once, after the last input. Furthermore you try to compare the argument with a local variable that has local storage and a lifetime limited to the function itself so that at the next call it will be initialized again

In order to have a variable inside a function that it is initialized only the first time you can use static keyword. But it is not I suggest you to solve the issue.

In my opinion you don't need comparison functions: you can just update maxi and mini each time you get a new input (solving both the aforementioned issues at once):

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : \n");
    scanf("%d",&times);

    printf("Type in the number: ");
    scanf("%f", &data_Input);

    maxi = data_Input;
    mini = data_Input;

    for(interval=2; interval<=times; interval++){
        printf("\nType in the number: \n");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:\n");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:\n");
            scanf("%f",&data_Input);
        }
        
        /* Check input and, in case, update max and min */
        if(data_Input > maxi)
             maxi = data_Input;

        if(data_Input < mini)
             mini = data_Input;
    }

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}

The comparison is performed inside the loop, so you don't need to store the inputs into an array.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • Yeah, it's true that I missed the points. I also personally think more difficult to manage the code when using functions. Since this is an assignment, I need to try my creativity, anyway, thank you for the explanation – Exc4pe Oct 09 '20 at 12:37
0

You have plenty issues in your code.

  1. Functions min & max do not make any sense
  2. You do not check result of the scanf and you do not know if it was successfull
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int main()
{
    int times, interval;
    float mini, maxi, data_Input;

    do 
    {
        printf("\nHow many number would you like to type in ? : ");
    }while(scanf(" %d\n",&times) != 1);

    for(interval = 0; interval < times; interval++)
    {
        do 
        {
            printf("\nType in the number: ");
        }while(scanf(" %f",&data_Input) != 1 && (data_Input < 0 || data_Input > 100));

        printf("%f\n", data_Input);

        maxi = interval == 0 ? data_Input : MAX(maxi, data_Input);
        mini = interval == 0 ? data_Input : MIN(mini, data_Input);
    
    }
    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

When the program executed, the second line will wait on user's input (number) before the printf

Drop "\n" from "%d\n". It blocks until non-white space detected after the number and is not needed.

printf("How many number would you like to type in ? : ");
// scanf("%d\n",&times);
scanf("%d",&times);
printf("Type in the number: ");

If output still not seen when expected, flush it. Typically printing a '\n' will flush stdout, but not certainly.

printf("How many number would you like to type in ? : ");
fflush(stdout);  // add

The error "system" seems unreliable, sometimes works, sometimes doesn't work

At least this issue: sequential check vs. checking both conditions together input validity.

Rather than

    while(data_Input<0){
      ...
    }
    while(data_Input>100){
      ...
    }

Test together.

    while(data_Input<0 || data_Input>100){
      ...
    }

The program only checks the last number entry, therefore it only shows the last number in min and max

True as code only compares values once with one call to max(). That function only compares the number to 0 rather than prior values. Likewise for min().

Consider an algorithm change

// Pseudo code for max
prompt How many number would you like to type in ? : "
get times

max_value = -INF // set to minimum possible float

for each value 1 to times
  prompt "Type in the number: "
  get data_Input
  if  data_Input > max_value
    max_value = data_Input

print max_value
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    It seems I have homework to do and gained some knowledge after I read your answer. Thank you so much – Exc4pe Oct 09 '20 at 13:00