Thread: Find Min and Max of an array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Find Min and Max of an array

    Hi. I have assignment to find difference between Min and Max of an array of 5 numbers. I did some work, but after compiling - my program does not run, it just stops working.. Can you help me, where is my mistake ? Array values needs to be double.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
    int i;            // element in array 
    int size;            // size of array
    double array [i];    // number of elements in array
    double min;          //minimum value
    double max;          // maximum value
    double difference;   // difference between maximum and minimum values
     
     printf("Enter the size of the array: ");
     scanf("%d",&size);
     printf("Enter %d elements in to the array: ", size);
     scanf("%d",&array[i]);
           
    max = array[0];  // let first number in array be maximum
    for (i=0; i<size; i++)
    if (max < array [i])
        max = array [i];
        {
            printf("Maximum value is: %d\n",max);
        }
        
    min = array [0]; // let first number in array be minimum
    for (i=0; i<size; i++)
        if (min>array[i])
        min = array [i];
        {
          printf("Minimum value is: %d\n",min);
        }       
     
    difference = max - min;
     
    printf ("Difference between Min and Max values is %d\n", difference);
     
    /*   if (i>= 5)
       {
       printf ("Only 5 numbers allowed. \n");
       break;
       }
    */
     
      
      system("PAUSE");    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    what is happening here ..?? you got to loop size times with scanf if you want user to fill the array
    printf("Enter %d elements in to the array: ", size);scanf("%d",&array[i]);
    edit : on line 8 u declared array to random size as variable i is not initialized

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're using "%d" in your "scanf()" and "printf()" functions that are expecting doubles.
    And on line 16, you're using 'i' uninitialized.

    These corrections won't make your program correct, but it should let you compile and start troubleshooting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find smallest non-value in an 2-d array !! need help :D
    By newmem011 in forum C Programming
    Replies: 7
    Last Post: 07-10-2012, 10:37 PM
  2. how to find if array is cyclic
    By SA80 in forum C Programming
    Replies: 3
    Last Post: 05-05-2012, 03:53 AM
  3. Find size of int array
    By JonathanS in forum C Programming
    Replies: 2
    Last Post: 01-18-2012, 04:12 PM
  4. How to find the last value in an array
    By mkanimozhi in forum C++ Programming
    Replies: 11
    Last Post: 12-08-2009, 10:15 PM
  5. How to find the last value in an array
    By mkanimozhi in forum C Programming
    Replies: 2
    Last Post: 12-05-2009, 10:06 PM