Thread: largest number in array

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    largest number in array

    I do not understand why my program is not finding largest number in array

    I am not getting any output when I run the code

    Code:
     #include<stdio.h>
    int main (void)
    {
     int i, largest,  size, array[size];
     
        printf (" Enter size of array \n");
     
     scanf("%d", &size);
     
     printf("%d /n", size);
     
      printf (" Enter element of array \n");
     for ( i = 0; i < size; i++)
     {
      scanf("%d", &array[i]);
      printf("%d /n", array[i]);
     }
     
     largest = array[0];
     
        for ( i = 1; i < size; i++)
     {
      if (largest > array[i])
       largest = array[i];
     }
     
     printf("largest = %d", largest);
       
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    697
    You are defining an array with variable length BEFORE initializing 'size';
    In your final loop you are getting the SMALLEST, not the LARGEST.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,764
    I can answer your question very easily (for a start look at line 23 and what the condition is) but I have to wonder what you're trying to achieve. If you're trying to learn programming then, I have to ask, why are you choosing C? Although your problem does contain C that doesn't make sense the bigger problems, as far as I can tell, are problems with logic. In my opinion you need to concentrate on logic first and then worry about other things. Is C the best language for you to learn logic? You seem to be missing fundamental knowledge. Trying to learn C at the same time as trying to learn problem solving skills might not the best use of your time
    Last edited by Hodor; 02-12-2020 at 06:34 AM.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    697
    Your code, changed a little:
    Code:
    #include <stdio.h>
    #include <stdlib.h>   // EXIT_SUCCESS and EXIT_FAILURE are defined here.
    
    int main ( void )
    {
      size_t i, size;
      int largest;
    
      fputs ( "Enter number of elements of the array: ", stdout );
      fflush ( stdout );  // just to be sure stdout is flushed before scanf.
      if ( scanf ( "%zu", &size ) != 1 )
      {
        fputs ( "ERROR!\n", stderr );
        return EXIT_FAILURE;
      }
    
      // FIX: Here it is advisable to limit the 'size' because the
      //      variable length array will be allocated on stack.
      if ( size > 4096 ) // 16 KiB stack limit.
      {
        fputs( "ERROR! Too many elements.\n", stderr );
        return EXIT_FAILURE;
      }
    
      // If you plan to use VLA, this 'array' must be defined AFTER
      // you have size defined. Is not, you should 'malloc' the buffer, as in:
      //
      //    int *array;
      //
      //    if ( ! ( array = malloc( sizeof( int ) * size ) ) )
      //    {
      //      fputs( "ERROR! Memory allocation.\n" );
      //      return EXIT_FAILURE;
      //    }
      //
      // In that case, remember to call free( array ) somewhere after the buffer isn't
      // necessary anymore.
      //
      // Using VLA here:
      int array[size];
    
      printf ( "Enter %zu elements of the array (separated by space): ", size );
      fflush ( stdout );
      for ( i = 0; i < size; i++ )
        if ( scanf ( "%d", &array[i] ) != 1 )
        {
          fputs ( "ERROR!\n", stderr );
          return EXIT_FAILURE;
        }
    
      // Find the largest element.
      largest = array[0];
      for ( i = 1; i < size; i++ )
        if ( array[i] > largest )
          largest = array[i];
    
      printf ( "Largest = %d\n", largest );
    
      return EXIT_SUCCESS;
    }

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by Hodor View Post
    I can answer your question very easily (for a start look at line 23 and what the condition is) but I have to wonder what you're trying to achieve. If you're trying to learn programming then, I have to ask, why are you choosing C? Although your problem does contain C that doesn't make sense the bigger problems, as far as I can tell, are problems with logic. In my opinion you need to concentrate on logic first and then worry about other things. Is C the best language for you to learn logic? You seem to be missing fundamental knowledge. Trying to learn C at the same time as trying to learn problem solving skills might not the best use of your time
    I am not too good in c language but I am working on it by writing my own program.
    I develop logic on the page before writing the program.


    There is one logic error in code but I think program should be found smallest number in the array

    Edit

    Code:
    #include<stdio.h>
    int main (void)
    {
     int size, array[];
      
        printf (" Enter size of array \n");
      
        scanf("%d", &size);
      
        printf("%d /n", size);
     
     return 0;
    }
    error: array size missing in 'array'
    int size, array[];
    ^~~~~
    warning: unused variable 'array' [-Wunused-variable]
    Last edited by Player777; 02-12-2020 at 07:48 AM.

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    271
    Code:
    if(largest > array[i]) {
      largest = array[i];
    }
    


    Code:
    if (largest < array[i]) largest = array[i];
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find Largest Number in File [array]
    By VasilP in forum C Programming
    Replies: 1
    Last Post: 01-15-2020, 01:06 PM
  2. Find smallest and largest Number of Array.
    By Kexplx in forum C Programming
    Replies: 1
    Last Post: 11-02-2015, 05:47 PM
  3. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  4. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 02:31 PM
  5. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM

Tags for this Thread