#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main()
{
int num; //number of elements
int *arr; //array of elements
int first_big; //biggest element
int second_big; //2nd biggest element
int i_second = 0; //initialization of the second elemnt
int i;
printf("enter how many elements in the array: ");
scanf( "%d", &num);
printf( "you enter %d\n", num);
if( num <= 1)
{
printf( "Bad value %d ; number of elements should be at least 2 \n", num );
return 0;
}
if( (arr = (int *)malloc( num *sizeof ( int ))) == NULL)
{
printf( "allocation failed for %d elements\n", num );
return (0);
}
for ( i = 0; i < num; i ++)
{
printf( "element %d : " , i+1);
scanf( "%d", &arr[i]);
if( !i )
{
//initialization of biggest element
first_big = arr[i];
}
else if ( arr[i] == first_big )
{
//nothing to do
continue;
}
else if( arr[i] > first_big )
{
//we switch
if( i_second)
{
second_big = first_big;
}
//we save the new biggest one
first_big = arr[i];
}
else if( !i_second )
{
//initialization of 2nd biggest element
second_big = arr[i];
i_second = 1;
}
else if( arr[i] > second_big )
{
//value of 2nd biggest element
second_big = arr[i];
}
}
//list of elements
printf( "***your data**** \n");
for ( i = 0; i < num; i ++)
{
printf( "element %d %d\n", i+1, arr[i]);
}
//and the result
if( first_big && !i_second )
{
printf( "all the values entered are the same %d\n", first_big );
}
else
{
printf( "the biggest is : %d\n", first_big );
printf( "the second biggest is : %d\n", second_big );
}
return 1;
}