how to display the maximum value in the array?
int main()
{
int numbers[30];
int i;
// making array of 30 random numbers under 100
for(i=0;i<30;i++)
{
numbers[i] = rand() % 100;
}
// fin开发者_如何学JAVAding the greatest number in the array
int greatest = 0;
srand(1);
for(i=0;i<30;i++)
{
if ( numbers[i] > greatest)
greatest = numbers[i];
}
How do I then tell the program to display the max value of the array?? Thank you
To display it in the basic console output:
#include <iostream>
...
std::cout << "Max value is: " << greatest << "\n";
#include <iostream>
std::cout << greatest << '\n';
On a sidenote, you might want to call srand()
before your call rand()
(and might want to supply a more meaningful parameter).
If you are not doing this for home work I would suggest using std::max_element (available in <algorithm>
).
std::cout << "Max Value: " << *(std::max_element(number, numbers+30)) << std::endl;
Otherwise, in your program all thats left to do is to print the value. You could use std::cout (available in <iostream>
). After you've computed the great in the for loop.
// Dump the value greatest to standard output
std::cout << "Max value: " << greatest << std::endl;
Is this what you're referring to?
printf("%d", greatest);
Make sure to include "cstdio".
std::cout << greatest <<endl;
精彩评论