Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 9th, 2004, 6:04 AM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
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.
TecBrain is offline   Reply With Quote
Old Oct 9th, 2004, 6:15 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 9th, 2004, 7:09 AM   #3
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 4 Ravilj is on a distinguished road
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!
Ravilj is offline   Reply With Quote
Old Oct 9th, 2004, 10:37 AM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Oct 9th, 2004, 11:05 AM   #5
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
#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.
TecBrain is offline   Reply With Quote
Old Oct 10th, 2004, 8:10 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Didn't think of it that way - congrats.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 10th, 2004, 8:49 AM   #7
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 4 Ravilj is on a distinguished road
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!
Ravilj is offline   Reply With Quote
Old Oct 10th, 2004, 10:52 AM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Oct 10th, 2004, 11:23 AM   #9
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 4 Ravilj is on a distinguished road
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!
Ravilj is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC