![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
I just started taking C++ courses and I will need some guide from you guys, I will appreciate your support too.
I cant figure out how to solve this: I need to make a program that will read from a list of numbers, finding the largest and the smallest number from that list. Can some one guide on how to implement it. I will need to use loop, but i am not sure how would I compaer the numbers in order to choose the largest and smallest numbers. cout << “Thanks guys”;
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Look up a sort mechanism on Google - bubble sorting is the easiest to understand, whereas quicksorting is the fastest. Read the numbers into an array, sort them and take the first and last ones.
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 4
![]() |
The hardest part will be reading the numbers into an array from there you can just loop throught them and use a temp variable for largest and smallest. Set them to the number stored in the first element in the array. Than use two if statements to test whether the current element in the array is larger or smaller than ur two tempory variables. If so set the temporay variable to that number.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
This should cover it...
#include <iostream.h>
int main (void)
{ int j, n;
double max, min, sum;
cout << "Program to process real numbers." << endl;
cout << "Enter number of reals: ";
cin >> n;
double a[n]; /* declare array of n values,
with subscripts 0, ..., n-1 */
cout << "Enter " << n << " numbers: ";
for (j = 0; j < n; j++)
cin >> a[j]; /* subscripts given in brackets [ ] */
sum = max = min = a[0]; /* right to left assignment operator */
for (j = 1; j < n; j++)
{ if (a[j] > max)
max = a[j];
if (a[j] < min)
min = a[j];
sum += a[j];
}
cout << "Maximum: " << max << endl;
cout << "Minimum: " << min << endl;
cout << "Average: " << sum/n << endl << endl;
cout << "Enter the index (1..n) of the number to be printed: ";
cin >> j;
cout << "The " << j << "th number is " << a[j-1] << endl;
return 0;
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
#include <iostream>
using namespace std;
int main()
{
//Variables
int number = 0;
int min, max;
//Comments
cout << "This program will let you enter number after" <<endl;
cout << "number. Enter 99 when you want to quit the " <<endl;
//For initialising values of min and max
cin>>number;
if(number==99) return(0); //if the number is 99 the program will quit
min = max = number; //initialize the values of min and max to compare with later inputs
//Loop to get the numbers
while(number!=99) //loop
{
cin>>number;
//Evaluating the numbers
if(number ==99) break; //break the loop or else max will become 99 if this line is not there
else if(number>max) max = number; //if the no. is greater than max then it becomes the max
else if(number<min) min = number; // if the no. is smaller than min then it becomes min
} //end of loop
//Printing the smallest and biggest numbers
cout<<"The smallest number is: "<<min<<endl; //displays smallest no.
cout<<"The biggest number is: "<<max<<endl; //displays biggest no.
*
*system("PAUSE");
*return 0;
}Also works for me. Thanks guys.
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Didn't think of it that way - congrats.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 4
![]() |
OMW! *blush* I thought you were reading the numbers in from a file and than finding the largest and smallest number. lol!
My say... cin>>number;
if ( number != -99 )
{
* max = number; // not a fan of cascaded equates.
* min = number;
* do
* {
* * * if ( number > max ) max = number;
* * * if ( number < min ) min = number;
* * * cin>>number;
* *}
* *while ( number != -99 );
}Infinite Recursion: those are some pretty intense comments. I was under the imprtion comments where meant to be helpful to a person reading your code... lol.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() ![]() |
Oops...
![]() The program was pulled from a group of math code I have. I didn't check for commenting, figured they would ask questions in the thread if they have any. At anyrate, seems like he has a viable solution. lol
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 4
![]() |
Agreed had a good chuckly when i read the comments... most of them were more cryptic than the code... hehe! all good
![]()
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|