Thread: more trouble with functions

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    more trouble with functions

    Code:
    #include <iostream>
    using namespace std;
    
    int numbers(int, int, int, int);
    int findHighest(int, int, int, int);
    
    
    int main()
    {	
    	
    	int num1 = 0;
    	int num2 = 0;
    	int num3 = 0;
    	int num4 = 0;
    	
    	int highest;
    
    	numbers(num1, num2, num3, num4);
    
    	highest = findHighest(num1, num2, num3, num4);
    
    	if (highest == num1)
    	cout << "The highest number entered was the first one" << endl;
    
    	if (highest == num2)
    	cout << "The highest number entered was the second one" << endl;
    
    	if (highest == num3)
    	cout << "The highest number entered was the third one" << endl;
    	
    	if (highest == num4)
    	cout << "The highest number entered was the fourth one" << endl;
    
    	return 0;
    
    }
    
    int numbers(int num1, int num2, 
    			   int num3, int num4)
    {
    
    
    	cout << "Enter four numbers" << endl;
    	cin >> num1;
    	cin >> num2;
    	cin >> num3;
    	cin >> num4;
    
    	return 0;
    
    }
    
    int findHighest(int num1, int num2, 
    				int num3, int num4)
    
    {
    	int highest;
    
    	if (num1 > num2 && num1 > num3 && num1 > num4)
    		highest = num1;
    		
    	else if (num2 > num1 && num2 > num3 && num2 > num4)
    		highest = num2;
    		
    	else if (num3 > num1 && num3 > num2 && num3 > num4)
    		highest = num3;
    		
    	else
    		highest = num4;
    		
    
    	return highest;
    }
    Why do all of the cout statements execute? highest can't equal num1, num2, num3, and num4 at the same time if they're all different.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>highest can't equal num1, num2, num3, and num4 at the same time if they're all different
    But they're not different. Do some debugging on your own to solve this one, it'll be good practice for you. Start by printing out the values of num1-4 in main.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Do you know how to use refrence variables? You might want to look into that.
    If you ever need a hug, just ask.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Re: more trouble with functions

    Originally posted by volk
    Code:
    #include <iostream>
    using namespace std;
    
    int numbers(int, int, int, int);
    int findHighest(int, int, int, int);
    
    int main() {	
    
    <...>
    
    	numbers(num1, num2, num3, num4);
    
    <...>
    Actually this code doesn't change the numX variables. You have to use the pass by reference mechanism...

    Code:
    void numbers(&amp;int, &amp;int, &amp;int, &amp;int);
    
    int numbers(int num1, int num2, 
    			   int num3, int num4)
    {
    
    
    	cout << "Enter four numbers" << endl;
    	cin >> num1;
    	cin >> num2;
    	cin >> num3;
    	cin >> num4;
    
    	return 0;
    
    }
    A good thing to do would be also to spend some time to write down the name of the parameter you are passing. Now it may not seem to see clear why, but when you will be working with .h include files, you will have to know what a function does by only watching its definition, so that if you find something like that:

    int insuranceCost(car insuredCar, person client);

    You will be able to tell what the function does.

    Something like:
    int foo(int int int int)

    is not clear.

    Why do all of the cout statements execute? highest can't equal num1, num2, num3, and num4 at the same time if they're all different.
    Because you never change the original ints, which are still set to 0. So findhigerst returns 0, and the ifs are all executed.

    When you will be more expert, I advice you to:

    - to use a variable list of integers and an integer representing the lenght of the list
    - use a linear scanning algorithm to find the maximum

    That is (in pseudocode)

    Code:
    int max = the first element of the list;
    int maxPosition = the first element of the list;
    listpointer = go to the next place of the list;
    add 1 to the list count;
    
    while (the list hasn't finished) {
    
      if (max < the current element of the list) {
         max = the current element of the list;
         maxPosition = the current elementPosition of the list;
      }
      listpointer = go to the next place of the 
      list;
      add 1 to the list count;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM