# to find max and min

i am a beginner. i am on my first problem and its not working. i really want help.

the question is to find max and min of n numbers .
the code is not working as it is supposed to, my code is-

``````#include<stdio.h>
``````

int main()
{
int n,m,i,max,min,num1,num2,x,y,temp;
int large[100],small[100],index1=1,index2=1;

``````printf("Give N : \n");
scanf("%d",&n);
printf("N=%d\n",n);

scanf("%d",&max);
min=max;
m=n-1;
if(n%2==0){
scanf("%d",&min);
m=m-2;}
if(min>max){
temp=max;
max=min;
min=temp;
}
``````

for(i=1;i<=m/2;m++){

``````scanf("%d %d",&num1,&num2);
if(num1<num2)
{
temp=num1;
num1=num2;
num2=temp;
}
if(num1>max) max=num1;
if(num2<min) min=num2;
``````

}
printf(“Max of given N numbers is : %d\nMin of given N numbers is : %d\n”,max,min);

``````return 0;
``````

}

Can you explain what you are trying to do? Why are you reading max and min at runtime?

You are incrementing ‘m’ instead of ‘i’ in the for loop

That still doesn’t give required output.

use selection sort its easy
#include<stdio.h>
void main()
{
int ,i,j,temp,a[100],pos ;
printf(“Enter the number of item “);
scanf(”%d”,&n);
\input element to sort
printf(“enter the items to sort \n”);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
\sort the elements
for(i=0;i<n-1;i++)
{
pos =i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[pos])pos=j;
}
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
}
printf(“the sorted items are \n”);
for(i=0;i<n;i++)
{
printf("%d"\n ,a[i]);
}
}

@vedant2080 can you give a solution link, i am not getting what you wrote here…!!

paste your code on ideone.com and give a link of ideone here…!!

Given proper input, I am sure that this code will work fine! Link : http://ideone.com/pMAsW0

@vedant2080

I have not understood your solution but if you want to print maximum and minimum number then you may sort the array and for minimum print array[0] and for maximum print array[lengthOfArray-1].

After hours of debugging it is finally working.
But the original code from where i am learning is still not working.
After i have modified the code upto much extent it is working.
I am currently learning from nptel.ac.in ,hope you people know about nptel.
The course is from IIT Kharagpur and course name is- Programming and Data Structures.
I am currently completed it’s 5th lecture ,the link is- `http://nptel.ac.in/courses/106105085/5`

No need to print Extra things , You have to print according to the sample output given.
Such as
printf(“Give N : \n”);

#include<stdio.h>
main(){
int min,max,n,i,j,t;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i-1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(“Min is:%d, Max is:%d”,a[0],a[n]);
}

Well still the original code from where i am learning is not working.I have provided the link to the video tutorial above.The link to original code that is not working on my computer but working on the proffesor’s computer is- link text

Why is this code not working on my computer properly. I am using Dev C/C++ compiler on windows 7.