arrays

I need help writing a program to fill ten integer numbers into an array and print out the maximum number. If some one could help ASAP i have to turn this code in at 3 thanks.
closed account (48T7M4Gy)
What code have you written? If no code what pseudocode or other plan.
where are these 10 numbers coming from - read from file? randomly generated? input from keyboard? etc? please provide complete information
the numbers are inputted from the keyboard
OK, and given that this is homework what have you done of your own so far? have you at least done a google search?
this is what I've got so far is this right?



#include <iostream>
using namespace std;

int main()
{
int index = 0, nb = 0;
int arr [10];

for ( int i = 0; i < 10; i++)
{
cout << "Type an integer ";
cin >> arr[i];
if (arr[i] > nb)
{
nb = arr[i];
index = i;
}
}
cout << index;
return 0;
}
use std::max_element: http://en.cppreference.com/w/cpp/algorithm/max_element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>

constexpr auto SIZE = 10;

int main()
{
    int arr[SIZE] {};
    for (auto i = 0; i < SIZE; ++i)
    {
        std::cout << "Enter number " << i + 1 << "\n";
        std::cin >> arr[i];
    }
    std::cout << "The largest array element is " << *std::max_element(arr, arr+SIZE) << "\n";
}

edit: some input validation makes more robust program


Last edited on
Topic archived. No new replies allowed.