Here is the C program to find the largest and smallest element in a one dimensional (1-D) array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#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
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:- [email protected]
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?