Design a C program to find the
largest element of an array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int values[1000];
int i,n,largestElement;
//get the number of values
printf("Enter the number of values: ");
scanf("%d",&n);
fflush(stdin);
//get the values from the user
for(i=0; i<n; i++){
printf("Enter the value [%d]: ",i);
scanf("%d",&values[i]);
}
//search the largest element of an array
largestElement=values[0];
for(i=1; i<n; i++){
if(values[i]>largestElement){
largestElement=values[i];
}
}
//display the largest element of an array
printf("\nThe largest element of an array is %d.\n\n",largestElement);
getchar();
getchar();
return 0;
}
Comments
Leave a comment