to find max and min

begginer
beginner
problem

#1

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 : 

“);
scanf(”%d",&n);
printf("N=%d
",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
Min of given N numbers is : %d
",max,min);

return 0;

}


#2

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


#3

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
“);
for(i=0;i<n;i++)
{
scanf(”%d”,&a*);
}
\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*;
a*=temp;
}
printf(“the sorted items are
“);
for(i=0;i<n;i++)
{
printf(”%d”
,a*);
}
}


#4

@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].


#5

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


#6

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


#7

#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*);
for(i=0;i<n;i++){
for(j=i-1;j<n;j++){
if(a*>a[j]){
t=a*;
a*=a[j];
a[j]=t;
}
}
}
printf(“Min is:%d, Max is:%d”,a[0],a[n]);
}


#8

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.

Please Help!


#9

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


#10

That still doesn’t give required output.


#11

@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…!!


#12

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