If you have an array declared like
int a[N];
where N is some positive integer value then the valid range of indices to access elements of the array is [0, N)
.
It means that for example this for loop
for(i=1;i<=10;i++)
{
printf("enter 10 nos. for arr[%d] :",i);
scanf("%d",&arr[i]);
}
must look like
for ( i = 0; i < 10; i++ )
{
printf("enter 10 nos. for arr[%d] :",i);
scanf("%d",&arr[i]);
}
This while loop
while(arr[i]>c)
{
c=arr[i];
}
just does not make a sense and can be an infinite loop.
Moreover this call of printf
printf("Greatest number in a given array is:%d",c);
is placed within a for loop.
The program can look the following way
#include <stdio.h>
int main( void )
{
enum { N = 10 };
int arr[N];
printf( "Enter %d numbers:\n", N );
for ( int i = 0; i < N; i++ )
{
printf("\t%d: ", i + 1 );
scanf( "%d", arr + i );
}
int max = 0;
for ( int i = 1; i < N; i++ )
{
if ( arr[max] < arr[i] ) max = i;
}
printf( "The greatest number in the given array is: %d\n", arr[max] );
return 0;
}
1
solved Write a programme to input 10 numbers from the user and print greatest of all