GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2008, 17:03
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Greatest and smallest integers


Hello, i was asked to write a program that asks the user to enter a list of integers. The program should continue asking the user for integers until the user enters -99 which means finish the list. Then after the user is finished the program should display only the greatest and smallest integers.
So far so good, that part is not so hard, but he asked to made the program by using two functions (plus the main function). One function should keep track of the largest and smallest integers and the other one should just display the results.

I ve tried many ways the whole morning but with no success.. I don't know how to elavorate the program to make it work properly..
Can anyone give me some tips on how to arrange the program? Like, in what function to call first, or in which function should I set the loop to keep asking for integers?

Please, any help will be much appreciated!

thanks
  #2  
Old 29-Oct-2008, 01:54
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Greatest and smallest integers


Assume first integer as largest and smallest, then iterate over the list of integer.
  #3  
Old 29-Oct-2008, 08:30
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Re: Greatest and smallest integers


mmmm look what I ve done so far... the problem is that at the end when displaying the results.. the program gives me the greatest number only (as the smallest & greatest) Please, any ideas what am i doing wrong??

CPP / C++ / C Code:
 
#include <iostream>
using namespace std;

void output();
void track (int &);
int greatest, least;


int main()
{
        int num;
        
        cout << "This program lets you input a series of numbers and finds\nthe greatest and least values.\n";
        cout << "Type in a value. -99 will finish the series.\n";
        cin >> num;
        
        if (num == -99)
        cout << "\nNo values were entered, so there is no smallest and greatest value.\n";
        greatest = num, least = num;
        
        
while (num != -99)
      
      {
      track(num);
      cout << "Enter another value.\n";
      cin >> num;
      }
      
      
if (greatest != -99 && least != -99)
   { 
          cout << "\nSmallest value: " << least << endl;
          cout << "Greatest value: " << greatest << endl;
   }

system ("PAUSE");
return 0;

}

void track (int &num)
{
     greatest = num, least = num;
          
     if (num > greatest)
     greatest = num;
     else (num < least);
     least = num;  
}
  #4  
Old 29-Oct-2008, 09:26
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Greatest and smallest integers


Quote:
Originally Posted by bostero22
Please, any ideas what am i doing wrong??
CPP / C++ / C Code:
 
void track (int &num)
{
     greatest = num, least = num;
          
     if (num > greatest)
     greatest = num;
     else (num < least);
     least = num;  
}
Your have an unintended semicolon:
CPP / C++ / C Code:
else (num < least);
  #5  
Old 29-Oct-2008, 09:43
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Re: Greatest and smallest integers


It doesn't compile if i take it out.. I think there is other problem though.. the program is not really doing what i want it to do...
  #6  
Old 29-Oct-2008, 09:45
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Re: Greatest and smallest integers


The program is setting the last integer entered by the user as the smallest and greatest value...
  #7  
Old 29-Oct-2008, 10:45
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Re: Greatest and smallest integers


this felt a little better.. I re-arranged the whole program, still doesnt work right....

Any ideas or tips? please?

CPP / C++ / C Code:
//This program should get an integer list from the user
//then display only the largest and smallest number in the list.
//using two functions.
#include <cstdlib>
#include <iostream>

using namespace std;

void track (int &, int &, int &); //Function Prototypes.
void output();

int main(int argc, char *argv[])
{
    
    output(); //Call First Function.
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void output() //Function Definition.
     {
               int integer; //to hold the integers.
               int greatest, least; //To hold the greatest & Smallest values.
               
               do
               {
                   cout << "This program lets you input a series of numbers and finds\nthe greatest and least values.\n";
                   cout << "Type in a value. -99 will finish the series.\n";
                   cin >> integer;
               
                   if (integer == -99)
                   
                      cout << "\nNo values were entered, so there is no smallest and greatest value.\n";
               }
                   while(integer /= -99);
                   {
                         track(integer, greatest, least);
                         cout << "Please enter another integer: \n";
                         cin >> integer;
                   
                   if (greatest == -99 && least == -99)
                      {
                          cout << "\nSmallest value: " << least << endl; 
                          cout << "Greatest value: " << greatest << endl;
                      }  
                   }
        }
        
void track (int &integer, int &greatest, int &least)
{
     greatest = integer, least = integer;
     
     if (integer > greatest)
     greatest = integer;
     else if (integer < least)
     least = integer;  
}
  #8  
Old 30-Oct-2008, 05:04
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 73
cpit is on a distinguished road

Re: Greatest and smallest integers


Look that the result depends on the first number you give.

For example: if you give the integer 10 at first and 8 next, your program will assign 8 only to greatest, when should also assign it to the least.

CPP / C++ / C Code:
void track (int &integer, int &greatest, int &least)
{
     greatest = integer, least = integer; <-- pay attention here also!
     
     if (integer > greatest)
     greatest = integer;
     else if (integer < least)
     least = integer;  
}

  #9  
Old 01-Nov-2008, 00:13
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Greatest and smallest integers


Quote:
Originally Posted by bostero22
CPP / C++ / C Code:
int main(int argc, char *argv[])
{
    
    output(); //Call First Function.
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
You create a function called output() which does the input, calculations, and output. The name is bad.

Quote:
Originally Posted by bostero22
CPP / C++ / C Code:
                   while(integer /= -99);
                   {
What is this supposed to do?

Quote:
Originally Posted by bostero22
CPP / C++ / C Code:
void track (int &integer, int &greatest, int &least)
{
     greatest = integer, least = integer;
     
     if (integer > greatest)
     greatest = integer;
     else if (integer < least)
     least = integer;  
}
Upon entering this function you set greatest and least to the value integer. Therefore each of the if statements will be false since evarything is equal.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 04:48
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:15.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.