Thread: Need help finding the minimum value of an array

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    15

    Need help finding the minimum value of an array

    hello everyone so im trying to make my program read a bunch of numbers in an array to find its maximum and minimum. the program will ask the user to enter in as much number as possible until they enter a non number/letter. i got my program to find the maximum value but the program couldn't read the minimum value. it always says zero. also everytime i enter the number 0, the program will just finish its loop statement. If i typed in a negative number, it'll read the negative number as its minimum.

    Code:
    
    
    
    
    #include <stdio.h>
    
    
    
    
    int main()
    {
       //-------variables------------------ 
        double list[1000];  // can hold 1000 items
        int i;
        char letter;
        int max = list[0];
        int min = list[0];
            
        printf("Please enter a number\n");
    
    
        /* initialize the array items */
        for (i = 0; i < 1000; ++i)
        {
            scanf("%lf", &list[i]);
         
         if(list[i] > max)
         {
          max = list[i];
          }
          
          if(list[i] < min)
          {
           min = list[i];
           }
           
            if(list[i] == letter)
            {
               
               printf("the maximum element in the array is :%i\n", max);
               printf("the minimum element in the array is :%i\n", min);
               
               
             
             getch();
             }          
           
        
        }
    
    
    }
    Attached Files Attached Files
    • File Type: c stat.c (775 Bytes, 156 views)
    Last edited by Kevin Nguyen; 09-09-2013 at 07:57 PM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The problem is that you set min and max before you read in the array.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    15
    Quote Originally Posted by rcgldr View Post
    The problem is that you set min and max before you read in the array.
    so what do i exactly do then? sorry this is really bothering me.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Wait until the first element of the array (list[0]) has had a value assigned

    Code:
    if (i == 0) 
    {
         max=min=list[0];
    }
    if(list[i] > max)      //'else if' might be better
    {       
         max = list[i];       
    }
    OR

    Set 'max' to the smallest number possible and 'min' to the largest number possible.

    Note that these init values are reversed, 'max' is the smallest possible, so any value in the array will be greater than the starting value for 'max' (and overwrite the intialisation value).

    You might want to use limits.h, which has defines for INT_MAX and INT_MIN

    Code:
    int max = INT_MIN, min = INT_MAX;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    *line 33 - letter has not been initialised to anything. You've said that
    if list[i] == letter, but what is letter? Not sure how you would fix this. I'll hand it over to the experts.

    *Also, what is getch(); doing inside the if loop? (line 41).

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    *line 33 - letter has not been initialised to anything. You've said that
    if list[i] == letter, but what is letter? Not sure how you would fix this. I'll hand it over to the experts.
    Well, you are right that it is a mistake.

    On fixing it: Part of the problem is that letter is being used before it contains a value, which leads to unpredictable results. You would want to make sure that letter has a decent value by this point in the code, which probably means reading it from the user. For if the letter had something to do with a quit condition, a comparison that makes sense might look like this:
    Code:
    if ( letter == 'q' ) ...
    Comparing a number with a letter is pretty absurd, and I can guarantee that thanks to promotion rules, the outcome will be unwanted.

    A simple way to code such a thing, is to wait for scanf to fail and then try to read the errant character to see if you should close.
    Code:
    while (1) {
    	if ( scanf("%lf", &list[i]) != 1 ) {
    	   if ( scanf(" %c", &letter) != 1 ) {
    		  printf("Read error\n");
    		  while ( getchar() != EOF ) /* nothing */ ;
    		  continue;
    	   }
    	   if ( letter == 'q' ) {
    		  return 0; /* return from main() */
    	   }
    	}
    }
    Something like that.

    *Also, what is getch(); doing inside the if loop?
    That's probably meant to keep the console open because the code would be executed right before the program ends.
    Last edited by whiteflags; 09-10-2013 at 12:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding maximum and minimum number in 2D array
    By wonderwall in forum C Programming
    Replies: 4
    Last Post: 10-23-2011, 10:21 PM
  2. Needing a little bit of help with finding the minimum value
    By nobletype in forum C++ Programming
    Replies: 20
    Last Post: 10-28-2010, 09:38 AM
  3. Finding the minimum value in an array?
    By kabuatama in forum C Programming
    Replies: 8
    Last Post: 02-18-2006, 01:45 PM
  4. finding non-zero minimum in array
    By xstudent in forum C Programming
    Replies: 8
    Last Post: 12-16-2002, 03:58 AM
  5. finding minimum in array
    By xstudent in forum C# Programming
    Replies: 3
    Last Post: 12-15-2002, 12:23 AM