Thread: I am missing something in regards to min?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Red face I am missing something in regards to min?

    This is a program that prompts the user of how many numbers they would like to enter. It is suppose to choose the smallest number. I think my problem is min isn't changing. I have tried a for statement to get min to change in relation to int1 but it doesn't seem to change min. What am I missing here?



    Code:
     #include <stdio.h>
      4 
      5 int main (void)
      6 
      7 
      8 {
      9 float int1 = 0;
     10 float min = 1;
     11 float a, b;
     12 
     13 printf("How many numbers would you like to compare?\n");
     14 scanf("%f",&b);
     15 
     16 
     17         for ( a = 1; a <= b; a++ ){  /* beginning of for loop */
     18 
     19 
     20 
     21 printf("A is to %f\n",a);
    22 
     23  
     24 printf("Enter intenger\n");
     25 scanf("%f",&int1);
     26 
     27 
     28 
     29 if ( int1 < min )
     30 {
     31 min = int1;
     32 printf("The minimum is equal to %f\n",min);
     33 }
     34 
     35 }/* end for loop */
     36 
     37 printf("The minimum value is %f\n",min);
     38 return 0;
     39 
     40 }
     41


    How many numbers would you like to compare?
    4
    A is to 1.000000
    Enter intenger
    1
    A is to 2.000000
    Enter intenger
    2
    A is to 3.000000
    Enter intenger
    3
    A is to 4.000000
    Enter intenger
    4
    The minimum value is 1.000000

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Thanks for the help.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What is your question, exactly? Since you initialize min to 1, and none of the numbers you enter are less than that, min doesn't change. You should initialize min to a very very very very large number, so that you are sure to type something smaller.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    tabstop you are a genius. I am learning man. I miss the simple things.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    By the way, a perfect "very very very very large number" would be INT_MAX, if you were using ints. (I'm thinking that you might want ints instead of floats since you prompt for an "integer".)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Also, keep note that "a" should be an int. It is the for-loop counter so you need an integer. Having it as float serves nothing. The program loses time in order to make it an integer (propably).
    Note important on your case, but since you are learning...it might be useful later on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM