2

I'm taking a C programming class this semester and I have absolutely no programming experience, this is making it extremely difficult for me to do the assignments or even study.

the assignment is to write a program that will allow the user to input any amount of values and display the largest and smallest values that were inputted.

it has to accept positive and negative number and entering 0 will terminate the program.

if the first number entered is 0 then a message stating this must be displayed.

this may be quite laughable to some of you, but here is what I have.

#include <stdio.h>

int main(void)

{ 

float max=0, a;
float min=0, b;

printf("Entering 0 will terminate the sequence of input values.\n");

do{ printf("Enter Number:");

    if (scanf(" %f", &a)==1);{

    if(a<max){

        a=max;}

    if(a>min){

        a=min;}

    }

} while(a!=0);

printf("Your largest number was %.3f. Your smallest number was %.3f.", max, min);

return 0;
}

also, can any of you recommend and reference materials that will help me learn this stuff, thank you.

aawhite
  • 33
  • 1
  • 1
  • 4
  • Welcome to Stack Overflow. Please read the [About] page soon. You don't want the semicolon in `if (scanf(" %f", &a)==1);{` because it forms a null statement so the code in the braces is executed unconditionally. You might want to use `if (scanf("%f", &a) != 1) break;` so that if the input fails, you stop reading. You don't use `b`. I am curious about where the formatting style that ends blocks like `a=max;}` comes from. You don't identify when zero is entered as the firs value. – Jonathan Leffler Feb 18 '14 at 04:46
  • I suggest to read this book for basic understanding of C https://www.google.com/search?q=c+dennis+ritchie+link&oq=c+dennis+ritchie+link&aqs=chrome..69i57j69i64l3.10385j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8#q=c+dennis+ritchie+book+pdf – Jeyaram Feb 18 '14 at 04:48
  • 2
    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 001 Feb 18 '14 at 04:48
  • You should initialize with max = FLT_MIN and min = FLT_MAX – 001 Feb 18 '14 at 04:53

3 Answers3

4

should be:

if(a > max) {
    max = a;
}
if(a < min) {
    min = a;
}
wizard23
  • 191
  • 2
  • 4
2

It should work if you fix the following issues.

  1. You need to change

    if (scanf(" %f", &a)==1);{
               ^            ^
    

    to

    if (scanf("%f", &a)==1){
    
  2. For

    while(a!=0);
           ^^^
    

    It's a bad practice to compare float using !=. Better to use the following instead

    while(fabs(a) > 0.001);
    

    .

    As commented by @JonathanLeffler, actually it will be OK for this case. But in general, you certainly need to be careful about whether it is appropriate to compare two floating point values for equality, especially after a computation

  3. Your logic is wrong, you should update max/min instead of a. So change

    if(a > max) {
        a = max;
    }
    if(a < min) {
        a = min;
    }
    

    to

    if ( fabs(a) < 0.001 ) // if a~0, stop evaluating
                           //, otherwise, you will always get 0 as the min
        break;
    if(a > max) {
        max = a;
    }
    if(a < min) {
        min = a;
    }
    
  4. You should initialize min to be a very large number (e.g. FLT_MAX) at first to make it able to update based on a < min. And you'd better set float max=FLT_MIN if you want to handle negative numbers.


See it live: http://ideone.com/XQYkeD.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
1

Try this:

while(1)
{ 
    printf("Enter Number:");

    if (scanf("%f", &a)==1)
    {
      if(a==0)  //check if the input value is 0 then break the loop
         break;
      else
         {
            if(a>max)
               max=a;

            if(a<min)
               min=a;
         } 
    }
    else
        break;
}

If entered value is greater than max, then max is replaced by that number. And if a is less than min, then min is replaced by that number.

Raging Bull
  • 18,593
  • 13
  • 50
  • 55