how do i locate the largest number in the array?

Dec 1, 2016 at 5:01am
the array size is randomly generated

lets say the size is 34
i found the largest number in the away in cell 16

how do i cout cell 16
Dec 1, 2016 at 5:26am
Sort the array in descending order (largest number is array[0]) or ascending order (largest number is array[n]) where n is the size of the array

edit: assuming we're talking about C-style arrays here, if you are using std::array or vector you can also use the algorithms like std::max etc
Last edited on Dec 1, 2016 at 5:29am
Dec 1, 2016 at 5:49am
No need to sort.

Use a variable int max = array[0];
Iterate through the array. If the current item is greater than the one you already have, then update. If you keep track of the index as well, then just do something like std::cout << "Max at index: " << maxIndex << ", value: " << array[maxIndex] << std::endl;
Dec 1, 2016 at 6:14am
Iterate through the array. If the current item is greater than the one you already have, then update.
... and this is not called 'sorting' ;))
Dec 1, 2016 at 6:16am
Well, just not altering the order of elements in the array. Sorting the whole thing would take longer than iterating through and just finding the largest value.
Dec 1, 2016 at 6:42am
You piqued my interest and it seems there is more to this than meets the eye at first glance
https://www.toptal.com/developers/sorting-algorithms/
Dec 1, 2016 at 7:49am
Randomly generates size of range, randomly generates elements in the range, and finds the largest number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <random>
#include <chrono>
#include <functional>
#include <vector>
#include <algorithm>

int main()
{
    std::mt19937::result_type seed =
    std::chrono::high_resolution_clock::now().time_since_epoch().count();

    auto rand_size =
    std::bind(std::uniform_int_distribution<int>(2, 100), std::mt19937(seed));

    auto rand_element =
    std::bind(std::uniform_int_distribution<int>(0, 1000), std::mt19937(seed));

    std::vector<int> vec(rand_size());

    for (int &i : vec)
    {
        i = rand_element();
    }

    std::cout << *(std::max_element(vec.cbegin(), vec.cend())) << '\n';

    std::cin.ignore();
    std::cin.get();

    return 0;
}

If you want to use arrays, the size has to be determined at compile time and cannot be randomly generated. A way around this is to dynamically create the array.
Last edited on Dec 1, 2016 at 7:57am
Dec 1, 2016 at 9:10am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int MAXNUM = 10;                                               // I don't like big numbers when I'm debugging

int main()
{
   srand( time(0) );
   int N = rand() % MAXNUM + 1;   cout << "N=" << N << "\n\n";       // random size for array
   int *a = new int[N];                                              // dynamically allocate array
   for ( int i = 0; i < N; i++ ) a[i] = rand() % MAXNUM;             // fills with random numbers
      for ( int i = 0; i < N; i++ ) cout << a[i] << endl;                // for checking only
   cout << "Maximum is " << *max_element( a, a + N ) << endl;        // calculates maximum and outputs it
   delete [] a;                                                      // tidy up
}
Dec 1, 2016 at 9:25am
I don't mean any offense here.

But, with the above code you might as well be writing C code. Also, a range-based for loop is preferred for iterating through a range (hence the name).
Topic archived. No new replies allowed.