Function help

This is a discussion on Function help within the C++ Programming forums, part of the General Programming Boards category; Hello, I've been taking a class on C++, and the summer semester is now over but I'm very interested in ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Function help

    Hello,
    I've been taking a class on C++, and the summer semester is now over but I'm very interested in taking more classes during the fall. I've ran into a few problems here and there. I have a few questions about functions. How would I write a function that takes the highest number in an array of 10 and out put it?

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    I suggest you make the function accept the array as an arguement, and then test each cell against the next, from one end, using a separate variable and if statements. then you could simply output it with cout.

    Edit:
    Oh, and a loop, too...
    Last edited by muggizuggi; 07-31-2005 at 05:04 PM.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Here is what i was working on, can you tell me where I've been going wrong?


    Code:
    #include <iostream>
    
    using namespace std;
    double max()
    {
          int max = (i = 0; i < number; i++);
    }
      int main()
      {
        int number[12];
        int i;
        i = 1;    
        while( i < 12 )
        {
          cout << "What is the number " << i << " ";
          cin >> number[i]; 
        
        }
        i = 1;          
        int max = 0;  
        while( i < 10 )
        {
          max += number[i];
       
        }
        cout << "The Highest number is "
             << max << endl;  
    cin.get();
    cin.ignore();
    }

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I tried not to murder your code to much, but maybe you can learn from this remodeling. I used the ternary operator to get the biggest number. If the number was bigger than the one it was currently on, then it would make it the new biggest number. (I hope I didn't have any bad errors in my code)
    Code:
    #include <iostream>
    
    using namespace std;
    int max(int a, int b)
    {
    	return (a < b) ? b : a;
    }
    int main()
    {
    	int number[12];
    	int maxnum = 0;    
    	for(int k = 0; k < 12; k++)
    	{
    		cout << "Enter a number for number[k]: ";
    		cin >> number[k];
    	}
    	for(int i = 0; i < 12; i++)
    	{
    		maxnum = max(number[i], maxnum);
    	}
    	cout << "The Highest number is "
    		 << maxnum << endl;  
    	
    	
    	cin.get();
    	cin.ignore();
    	return 0;
    }

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Tonto, I assume you want to make the following substitution in your code:
    Code:
    // Exchange this line :
    cout << "Enter a number for number[k]: ";
    // With this line :
    cout << "Enter a number for number[" << k << "]: ";


    joeh158, the problem you have is that you have half of an assignment statement with half of a for-loop statement, and the two don't agree well. I suggest checking out some tutorials (perhaps the ones on this site) on some of the basics (assignment), and loop constructs.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    How would you be able to write a function that would sort the numbers out from highest to lowest?

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Rather extensive tutorial at Prelude's site.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Very helpfull.

    thanks

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Trying to sort number from highest to lowest, but using the code below, with the help from the link. There are some mistake in here I know, but need alittle help.

    Code:
    #include <iostream>
    
    using namespace std;
    int desent( int list[], int size )
    {
      int i;
      int max = list[0];
    
      for ( i = 1; i < size; i++ ) 
      {
        if ( list[i] > max )
          max = list[i];
      }
    
      return max;
    }
    
    int main()
    {
    	int list[12];
        
    	for(int i = 0; i < 12; i++)
    	{
    	cout << "What are the numbers" << i << "?";
    	cin >> list[i];
    	}
    	
    	cout << "The numbers sorted "<< desent << endl;  
    	
    	
    	cin.get();
    	cin.ignore();
    
    }

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    You are missing the type declaration in this line:

    Code:
    for ( i = 1; i < size; i++ )

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    That's not the problem, as the variable is defined above.

    I recommend reading further in that tutorial. Prelude even provides code for several sorting algorithms. I recommend Bubble Sort first ( http://www.eternallyconfuzzled.com/t...ng.html#bubble ). It is worthwhile to work through the tutorial to make sure you really understand sorting principles.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    Oops, sorry. Thanks for correcting me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21