0

WAP to read 'n' numbers and print the biggest and smallest number. Pls help me with this. The program should be done with without using arrays.......

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by NathanOliver
0
#include<iostream.h>
#include<conio.h>

void main()
{
int arr[20],temp,cnt,cnt2,small,num;
clrscr();
cout<<"how many elements you want to insert for sorting:";
cin>>num;
cout<<"\nenter elements:";
for(cnt=0;cnt<num;cnt++)
cin>>arr[cnt];
for(cnt=0;cnt<num-1;cnt++)
{
small=cnt;
for(cnt2=cnt+1;cnt2<num;cnt2++)
{
if(arr[small]>arr[cnt2])
small=cnt2;
}
if(small!=cnt)
{
temp=arr[small];
arr[small]=arr[cnt];
arr[cnt]=temp;
}
}


cout<<"\nsorted elements";
for(cnt=0;cnt<num;cnt++)
cout<<"   " <<arr[cnt];
cout<<"\n\nsmallest number is:"<<arr[0];
cout<<"\nbiggest number is:"<<arr[--cnt];
getch();
}

By using this code you can find biggest and smallest number also you can sort a whole list of numbers in ascending order. Here in this program i used selection sort method you read more methods of sorting in your book good luck.

0

@ Nandu Das The purpose of this forum is to help not to just give you the code. If you are having a particular problem with this problem then state what that is and we can help. With that said you can do this very simply. you will need three variables and a while loop. I would use min, max and input and I would set them all to 0. Ask the user to enter a number and store that number into min and max. Then in a while loop that loops forever ask the user to enter a number or something like -999 to quit. use cin >> input; to get the number entered by the user. Then compare input to -999. If it is equal then break the loop. if not then see if the number is less than min. if it is then set min = input. Then check to see if input is greater than max. If it is than set max = input.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.