mike
mike

Reputation: 21

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; 
   }

   // finding 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

Upvotes: 1

Views: 4530

Answers (5)

HariKrishnan Vr
HariKrishnan Vr

Reputation: 1

std::cout << greatest <<endl;

Upvotes: -1

Sonny Saluja
Sonny Saluja

Reputation: 7287

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;

Upvotes: 2

sbi
sbi

Reputation: 224049

#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).

Upvotes: 3

user541686
user541686

Reputation: 210402

Is this what you're referring to?

printf("%d", greatest);

Make sure to include "cstdio".

Upvotes: 0

Tim
Tim

Reputation: 9172

To display it in the basic console output:

#include <iostream>
...
std::cout << "Max value is: " << greatest << "\n";

Upvotes: 6

Related Questions