-
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?
-
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... ;)
-
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();
}
-
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;
}
-
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
-
How would you be able to write a function that would sort the numbers out from highest to lowest?
-
Rather extensive tutorial at Prelude's site.
Cheers
-
-
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();
}
-
You are missing the type declaration in this line:
Code:
for ( i = 1; i < size; i++ )
-
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.
-
Oops, sorry. Thanks for correcting me...