how do we compare multiple arrays and print the largest number?

I want to make a program which compares the indices of 3 arrays and then display the largest number amongst them. How can I make this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
	int array1[5]={1,5,9,10,15};
	int array2[5]={5,3,11,5,16};
	int array3[5]={6,4,14,3,18};
	int max=0;
	int count=0;
	
	for(int index=0;index<5;index++)
	{
		max=array1[index];
		
		
	}
	
}

I can't go further than this.
This being C++, we can use the standard library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int array1[5]={1,5,9,10,15};
	int array2[5]={5,3,11,5,16};
	int array3[5]={6,4,14,3,18};

	cout << "Max val is: " << max(max(*max_element(array1, array1+5),
                                          *max_element(array2, array2+5)),
                                      *max_element(array3, array3+5));

	
}

Last edited on
My teacher hasn't taught us how to use libraries other than <iostream> and <iomanip>. I'm a noob by the way.

Oh and please tell me what <algorithm> is used for.

And I ran your program, this should be the output;
6 5 14 10 18
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
    int array1[5] = {1,5,9,10,15};
    int array2[5] = {5,3,11,5,16};
    int array3[5] = {6,4,14,3,18};

    for (int index = 0; index < 5; index++)
    {
        int max = array1[index];

        //see if array2[index] is bigger than max
        //if it is, make it the new max

        //see if array3[index] is bigger than max
        //if it is, make it the new max

        //print max
    }

    return 0;
}
Last edited on
@booradley60
Your for loop only compares the elements at a specific index. Like, it compares the elements of each array at index 0, then at index 1, etc. It doesn't compare, say array1[0] with array2[1].

I would do this.
1
2
3
4
5
6
7
8
// function to return to highest number in the array
int getMax(int arr[], int elements);

/*
Use getMax() for each array, to get the highest number for each. 
Store the highest numbers in another array.
Call getMax() for the highest numbers array.
*/


It's pretty inefficient, but hey it gets the job done. Hope this helps.
OP made the post before mine that said this:
this should be the output;
6 5 14 10 18

Based on this information, it looked like the goal of the program was to output the maximum number among the three arrays on a per-index basis. My code suggests this.
EDIT: Complete code and output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
    int array1[5] = {1,5,9,10,15};
    int array2[5] = {5,3,11,5,16};
    int array3[5] = {6,4,14,3,18};

    for (int index = 0; index < 5; index++)
    {
        int max = array1[index];
        if (array2[index] > max) max = array2[index];
        if (array3[index] > max) max = array3[index];
        cout << max << ' ';
    }

    return 0;
}
6 5 14 10 18  
Last edited on
Topic archived. No new replies allowed.