You are not logged in. Please login at www.codechef.com to post your questions!

×

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;

}

asked 25 Dec '14, 17:44

vedant2080's gravatar image

0★vedant2080
914
accept rate: 0%

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

(25 Dec '14, 18:41) vinayawsm4★

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

(25 Dec '14, 19:35) rishabhprsd72★

You are incrementing 'm' instead of 'i' in the for loop

link

answered 25 Dec '14, 19:08

p00r's gravatar image

3★p00r
13916
accept rate: 10%

That still doesn't give required output.

(25 Dec '14, 19:13) vinayawsm4★

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

(25 Dec '14, 21:59) p00r3★

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]); } }

link

answered 25 Dec '14, 19:17

sharma_prachi's gravatar image

0★sharma_prachi
1
accept rate: 0%

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

link

answered 26 Dec '14, 01:07

rishabhprsd7's gravatar image

2★rishabhprsd7
1.9k11243
accept rate: 14%

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

link

answered 26 Dec '14, 19:03

vedant2080's gravatar image

0★vedant2080
914
accept rate: 0%

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

link

answered 26 Dec '14, 19:58

ambika93's gravatar image

2★ambika93
8138
accept rate: 0%

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&lt;n;i++){" for(j="i-1;j&lt;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]); }

link
This answer is marked "community wiki".

answered 26 Dec '14, 20:33

syedazar's gravatar image

0★syedazar
1
accept rate: 0%

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!

link

answered 27 Dec '14, 10:12

vedant2080's gravatar image

0★vedant2080
914
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×690
×432
×26

question asked: 25 Dec '14, 17:44

question was seen: 1,180 times

last updated: 27 Dec '14, 10:12