Here is the C program to find the largest and smallest element in a one-dimensional (1-D) array.
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
return 0;
}
Output
How many elements:5
Enter the Array:1 8 12 4 6
The largest element is 12
The smallest element is 1
Explanation:
The program starts by declaring an integer array ‘a’ of size 50 elements, along with integer variables ‘i’, ‘n’, ‘large’, and ‘small’ to store the array index, array size, largest and smallest values respectively.
The program asks the user to enter the number of elements in the array and then reads in the integers from the user using a for a loop. It then uses another for loop to compare each element in the array to the previously largest and smallest values.
If an element is greater than the current largest, it replaces the current largest. Similarly, if an element is smaller than the current smallest, it replaces the current smallest.
Finally, the program gives the largest and smallest values found in the array using printf statements.
it is actually a wrong program
I think program is correct, for what input it is giving wrong answer?
it is correct program.
Yes it is correct program thank you sir
program write but last line u replace line return 0; to getch();
Yes in above program big variable is stores a[0] instead of the 0 value ….
why don’t have you assigned large and small with 0 as it might take garbage value?
@Urvashi that not an intialization. he just take the first element in the array that 0 is the index number.
Is this programm correct or not.because i am not satisfied this program output….
Do yourself this program in desktop then you got it
sum of except largest and smallest element in a array c programming
the program is correct… thread closed.
that program is correct but give average of except 2nd large and 2nd small numbers
correct program
I think program is right but the output is not correct. it is giving a garbage type value.
what ‘s reason behind giving the wrong output in this program. as the program seems to be correct.
Yes its o utput is wrong
Yes this program is absolutely correct
difference between smallest and biggest elemnts of a given array.please tell me how to solve this in c language
Guys, those who have doubts on the above program. Send me a mail and i willl explain the code with step by step. Email id:- jenniferfeuntes@gmail.com
The above code is correct. Those who have doubts, just contact me I will explain it clearly.
why should we decrement
2 questions:
1. How can I use this to compute difference between maximum and minimum
2. How do I make it so when I input array, it is separated by commas
int a[50],i,n,large,small;
In this line, is it global variable?