Need help writing a program that reads a series of 5 integers and determines the largest and smallest.

9
Contributors
15
Replies
16
Views
10 Years
Discussion Span
Last Post by JRM

What have you got so far?

use selsort

``````#include<iostream.h>
#include<conio.h>
void sel_sort (int array[20],int x)
{
int i, j, first, temp;

for (i= x - 1; i > 0; i--)
{
first = 0;

for (j=1; j<=i; j++)
{
if (array[j] < array[first])
first = j;
}
temp = array[first];

array[first] = array[i];
array[i] = temp;
}
return;
}

#include<iostream.h>
#include<iostream.h>
void main ()
{
clrscr();
int ch,x=0,r,i,arr[50];
cout<<"1) Sort and array\n";
cout<<"2) Sort an array and insert a integeraccordingly\n";
cin>>ch;
switch(ch)
{
case 1 : cout<<"How many terms will be there in the array ?\n";
cin>>x;
for(i=0;i<x;i++)
{
cin>>arr[i];
}
sel_sort(arr,x);
break;
case 2 : cout<<"How many terms will be there in the array ?\n";
cin>>x;
for(i=0;i<x;i++)
{
cin>>arr[i];
}
sel_sort(arr,x);
cout<<"Input the integer\n";
cin>>y;
clrscr();
for(r=0;r<x;r++)
{
if(y<arr[r])
{
y=arr[r+1];
r++;
}
getch();
}``````

>use selsort
"How do I cross this creek without getting my feet wet?"
"Use a 747 to fly over it."

What narue is trying to say, in her own clever little way, is that your solution (atish00) is a bit overkill.

Think about it, you don't need to sort anything.

Also some elements of your code are just plain wrong. I bet you are using the antiquated version of turbo c...

I prefered sel-sort because I have already got the function in my HDD so Instead of writing a whole new prog. I will just access the first and last term of the resultant array.

My c++ version is TC++ v3.0

>I prefered sel-sort because I have already got the
>function in my HDD so Instead of writing a whole new prog.
That's not the point. Your solution is painfully wasteful compared to the obvious solution, even if you generalize the obvious solution to take any amount of numbers.

Um, yeah that one was over my head...lol. I'm a newbie as well and I was thinking just a simple cout and cin to read in 5 numbers, then some nested if else statements or lazy person's if else line.

my final code will be

include"sel_sort.cpp"
clrscr
cout<<"No. of numbers \n"
cin>>x;
largerst=arr[0];
smallest=arr[x-1];

sorry even I am a new programmer and started c++ just 10 months ago.
Thats all I can help.

You don't need to sort. Just declare an array of 5 integers and declare an integer called "largest" and an integer called "smallest". Start with element 0. Assign "largest" "and smallest" to that value. Then go through the other 4 integers one at a time. Compare each integer in the array to the current "largest"and the "smallest". If the integer is smaller or larger than the current "smallest" or "largest", assign "smallest" or "largest", whichever the case may be, to the value of the integer.

You actually don't even need the array if you don't care about storing the 5 integers for later use and only care about the largest and smallest. Just compare as you read in the numbers.

my final code will be

include"sel_sort.cpp"
clrscr
cout<<"No. of numbers \n"
cin>>x;
largerst=arr[0];
smallest=arr[x-1];

sorry even I am a new programmer and started c++ just 10 months ago.

Yikes.

the selection sort atish00 suggested actually works by going through your list of numbers and finding the smallest number this could easily be modified to find the greatest also.
But still, it is overkill. The best way is to just compare the numbers to the current highest and lowest as they are inputted. That way you don't even need to store the inputs.

>sorry even I am a new programmer and started c++ just 10 months ago.
10 months, eh? Is that on and off? Or with regular practice and reading? I can forgive the bad habits caused by your choice of compiler, but if that program is indicative of your programming ability, I think hanging around on Daniweb can help you jump to the next level.

How many months does it takes to get into Beautiful code club :P

>How many months does it takes to get into Beautiful code club
The Beautiful Code club isn't about how long you've been programming, it's about how seriously you work toward elegance. ;) It could be as little as a few months to get situated with programming, or it could never happen. It all depends on your attitude.

Rube Goldberg is alive and well in the C++ forum...

This topic has been dead for over six months. 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.