Thread: Quick Questions Max/min & Break/EXIT

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Quick Questions Max/min & Break/EXIT

    I've been working on this program and most things are working... However I was wondering if there is an easier way to write my last funtion without using an array... For the sorting. I need them to be sorted least to greatest I think I have it backwards at the moment. Also, the teacher recommended using swap how would I do this? This is the only function holding me up. Also is do my case five and default case statements look ok. Case five should exit and defualt should ask the user to re enter values.

    Code

    Code:
    // ***************************************
    //Doug Miller
    //Lab 3, Part 3
    //Working with numbers; to compute average!
    //****************************************
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    float Do_Sum (float, float, float);
    float Do_Product (float, float, float);
    float Do_Average (float, float, float);
    void Do_Sort (float&, float&, float&);
    void Swap(int& x, int& y); 
    
    int main()
    {
    	int option = 0;
    	float v_1 = 0;
    	float v_2 = 0;
    	float v_3 =0;
    
    	while (option != 5)
    	{	
    		cout << "Options" << endl;
    		cout << "-------" << endl;
    		cout << "\n\n";
    		cout << "1.) Sum" << endl;
    		cout << "2.) Product" << endl;
    		cout << "3.) Average" << endl;
    		cout << "4.) Sort" << endl;
    		cout << "5.) Exit" <<endl;
    		cout << "\n\n";
    		cout << "Please enter your choice: " << endl;
    		cin >> option;
    		
    		switch (option)
    		{
    			case 1:
    				{
    					cout << "Please enter three values: " << endl;
    					cin >> v_1 >> v_2 >> v_3;
    					cout << "\n\n";
    					cout << "The sum of these numbers is " << Do_Sum (v_1, v_2, v_3);
    					cout << "\n\n";
    					break;
    				}
    			case 2:
    				{
    					cout << "Please enter three values: " << endl;
    					cin >> v_1 >> v_2 >> v_3;
    					cout << "\n\n";
    					cout << "The product of these numbers is"  << Do_Product (v_1, v_2, v_3);
    					cout << "\n\n";
    					break;
    				}
    			case 3:
    				{
    					cout << "Please enter three values: " << endl;
    					cin >> v_1 >> v_2 >> v_3;
    					cout << "\n\n";
    					cout << "The average of these numbers is " << Do_Average (v_1, v_2, v_3);
    					cout << "\n\n";
    					break;
    				}
    			case 4:
    				{
    					cout << "Please enter three values: " << endl;
    					cin >> v_1 >> v_2 >> v_3;
    					cout << "\n\n";
    					Do_Sort (v_1, v_2, v_3);
    					cout << v_1 << " " << v_2 << " " << v_3;
    					cout << "\n\n";
    					break;
    				}
    			case 5:
    				{
    					option = 5;
    					break;
    				}
    			default:
    				{
    					cout << "Please renter option: " << endl;
    					cin >> option;
    				}
    		}
    	}	
    
    }//END OF MAIN
    
    float Do_Sum (float v_1, float v_2, float v_3)
    {
    	float total = 0;
    
    	total = (v_1 + v_2 + v_3);
    
    	return total;
    
    }//END OF DO SUM
    
    float Do_Product (float v_1, float v_2, float v_3)
    {
    
    	float product = 0;
    
    	product = (v_1 * v_2 * v_3);
    
    	return product;
    
    }//END OF DO PRODUCT
    
    float Do_Average (float v_1, float v_2, float v_3)
    {
    	float average = 0;
    
    	average = ((v_1 + v_2 + v_3) / 3);
    
    	return average;
    
    }//END OF DO AVERAGE
    
    void Do_Sort (float& v_1, float& v_2, float& v_3)
    {
    
    	
    	if (v_2 < v_1)
    	{
    		swap(v_2, v_1);
    	}
    	else if (v_3 < v_1)
    	{
    		swap (v_3, v_1);
    	}
    	else if (v_3 < v_2)
    	{
    		swap (v_3, v_2);
    	}
    	else if (v_1 < v_1)
    	{
    		swap (v_1, v_3);
    	}
    
    
    }//END OF SORT
    
    void Swap(int& u, int& v)
    { 
        int temp; 
        temp = u; 
        u = v; 
        v = temp; 
    
    } //END OF SWAP
    Last edited by GCNDoug; 11-07-2007 at 12:44 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	{
    		v_1 = v_1;
    		v_2 = v_2;
    		v_3 = v_3;
    	}
    }//END OF SORT
    This bit is obviously completely un-necessary, as it doesn't actually do ANYTHING [nor does it have to].

    More "unneeded".
    Code:
    	if (v_1 > v_2 && v_1 > v_3)
    	{
    		v_1 = v_1;
    I'm pretty sure that this is broken:
    Code:
    		temp = v_1;
    		v_1 = v_2;
    
    		if (v_1 > v_3)  // v_1 is now  the same as v_2. 
    		{
    			v_2 = v_1;  // So we set v_2 to itself. 
    			v_3 = v_3; // And v_3 to itself. 
    		}
    		else
    		{
    			v_2 = v_3;
    			v_3 = temp;
    		}
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    GCNDoug, did you compile and test your program? If so, copy and paste it when posting instead of rewriting it. I noticed that you wrote "Do_Profuct" which should be "Do_Product". Other typographical errors might have been introduced.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I'm actually without a complier right now I'll update the code later; I shouldn't have posted it at its current state. Sorry everyone.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    switch(option)
    {
        ...
        case 4:
        {
            Do_Sort (v_1, v_2, v_3);
            cout << v_1, v_2, v_3;
            cout << "\n\n";
            break;
        }
        case 5:
        {
            option = 5;
            break;
        }
        ...
    }
    That cout statement is incorrect, it should be something like:
    Code:
    cout << v_1 << ' ' << v_2 << ' ' << v_3;
    The code for case 5 is redundant. If you reach that code, option is already equal to 5 so why set it to 5 again?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm actually without a complier right now
    You could use the Comeau C++ Online compiler to check if your code at least compiles.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    77
    And you're right, you do have the sorting function backwards. The first line, for example, should be
    Code:
    if (v_1 < v_2 && v_1 < v_3)
    	{
    		v_1 = v_1;
    And you shouldn't need that code following it (in green). If v_1 is less than the other two, then it is already set as it needs to be.

    Also, what happens if v_3 is the least of the three? That's what your last part should be about doing, not about remaking the values as they are.

    Next, when you're passing the variables to the functions, they're not passing them back. Probably the easiest way to do it, since you're needing to return more than one variable, is to simply put the "cout" part of it inside each function instead of doing it in the cases. That way, you pass the variable to to the functions, and they can do whatever they need to with them, and not worry about sending them back.

    Finally, there is a much cleaner way to do the sorting function. Instead of rearranging the variable definitions (i.e. "temp = v_2, v_2 = v_3, v_3 = temp"), just output them in order of how they're already set.
    Last edited by Molokai; 11-07-2007 at 10:47 AM.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Thanks everyone I got everything working but the sort (updated my code from the first post). I'm getting there. So close! lol

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Alright well I made a whole lot more if statements and still can't get it to sort right. Can someone lead me in the right direction?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Thanks for the reply you are the best! I updated my first post with the current code. Everything is working but the sort...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem with your sort is that it only ever swaps exactly once. Take for example: (2, 3, 1). To sort to (1, 2, 3), you need at least two swaps.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Would you happen to know where I could go to find some examples for sorting that aren't for arrays? I can't use arrays for this... If i could it would be done by now. I still can't seem to get this

    I tried this and it should at least work if they're backwards like 10 9 8
    ... since it's by refrence
    Code:
    	if (v_2 < v_1)
    	{
    		swap (v_2, v_1);
    	}
    
    	if (v_3 < v_2)
    	{
    		swap (v_3, v_2);
    	}
    Still won't work though

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Think of it this way. If you have three numbers to sort in ascending order, first get the smallest number and put it in the first position. Then, if the last number is smaller than the middle number, swap those.

    Perhaps something like this:
    Code:
    /* make n1 the smaller of n1 and n2 */
    if(n1 > n2) swap(n1, n2);
    
    /* make n1 the smaller of n1 and n3, and thus the smallest of all three */
    if(n1 > n3) swap(n1, n3);
    
    /* make n3 the largest of n2 and n3, which are the largest and second-largest numbers */
    if(n2 > n3) swap(n2, n3);
    I think I got that right . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need three if-statments and three swaps, in the trivial case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two quick questions about open file dialog boxes
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 04-05-2005, 08:49 AM
  2. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  3. A quick question(s)
    By EvBladeRunnervE in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2003, 09:39 PM
  4. A few quick questions...
    By cpp4ever in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2001, 09:28 AM