# Thread: largest number in array

1. ## 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;

for ( i = 1; i < size; i++)
{
if (largest > array[i])
largest = array[i];
}

printf("largest = %d", largest);

return 0;
}``` 2. You are defining an array with variable length BEFORE initializing 'size';
In your final loop you are getting the SMALLEST, not the LARGEST. 3. 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 4. 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;
for ( i = 1; i < size; i++ )
if ( array[i] > largest )
largest = array[i];

printf ( "Largest = %d\n", largest );

return EXIT_SUCCESS;
}``` 5. Originally Posted by Hodor 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] 6. Code:
```if(largest > array[i]) {
largest = array[i];
}
```

Code:
`if (largest < array[i]) largest = array[i];` Popular pages Recent additions array, enter, largest, printf, size; 