A more efficient code?

Is there a more efficient way, better than this to find the largest or smallest number out of a set of data?! Thanks in advance.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;

int main()
{
	int r[10];
	int randome;

	srand(time(0));

	for(int counter=0;counter<10;++counter)
	{
		randome=rand()%100+1;
		r[counter]=randome;
	}

	if(r[0]>=r[1] && r[0]>=r[2] && r[0]>=r[3] && r[0]>=r[4] && r[0]>=r[5] && r[0]>=r[6] && r[0]>=r[7] && r[0]>=r[8] && r[0]>=r[9])
		cout<<"The largest array has a value of "<<r[0] <<" and it is at position 0."<<endl;

	if(r[1]>=r[0] && r[1]>=r[2] && r[1]>=r[3] && r[1]>=r[4] && r[1]>=r[5] && r[1]>=r[6] && r[1]>=r[7] && r[1]>=r[8] && r[1]>=r[9])
		cout<<"The largest array has a value of "<<r[1]<<" and it is at position 1"<<endl;

	if(r[2]>=r[1] && r[2]>=r[0] && r[2]>=r[3] && r[2]>=r[4] && r[2]>=r[5] && r[2]>=r[6] && r[2]>=r[7] && r[2]>=r[8] && r[2]>=r[9])
		cout<<"The largest array has a value of "<<r[2]<<" and it is at position 2"<<endl;

	if(r[3]>=r[0] && r[3]>=r[2] && r[3]>=r[1] && r[3]>=r[4] && r[3]>=r[5] && r[3]>=r[6] && r[3]>=r[7] && r[3]>=r[8] && r[3]>=r[9])
		cout<<"The largest array has a value of "<<r[3]<<" and it is at position 3"<<endl;

	if(r[4]>=r[0] && r[4]>=r[2] && r[4]>=r[3] && r[4]>=r[1] && r[4]>=r[5] && r[4]>=r[6] && r[4]>=r[7] && r[4]>=r[8] && r[4]>=r[9])
		cout<<"The largest array has a value of "<<r[4]<<" and it is at position 4"<<endl;

	if(r[5]>=r[0] && r[5]>=r[2] && r[5]>=r[3] && r[5]>=r[4] && r[5]>=r[1] && r[5]>=r[6] && r[5]>=r[7] && r[5]>=r[8] && r[5]>=r[9])
		cout<<"The largest array has a value of "<<r[5]<<" and it is at position 5"<<endl;

	if(r[6]>=r[0] && r[6]>=r[2] && r[6]>=r[3] && r[6]>=r[4] && r[6]>=r[5] && r[6]>=r[1] && r[6]>=r[7] && r[6]>=r[8] && r[6]>=r[9])
		cout<<"The largest array has a value of "<<r[6]<<" and it is at position 6"<<endl;

	if(r[7]>=r[0] && r[7]>=r[2] && r[7]>=r[3] && r[7]>=r[4] && r[7]>=r[5] && r[7]>=r[6] && r[7]>=r[1] && r[7]>=r[8] && r[7]>=r[9])
		cout<<"The largest array has a value of "<<r[7]<<" and it is at position 7"<<endl;

	if(r[8]>=r[0] && r[8]>=r[2] && r[8]>=r[3] && r[8]>=r[4] && r[8]>=r[5] && r[8]>=r[6] && r[8]>=r[7] && r[8]>=r[1] && r[8]>=r[9])
		cout<<"The largest array has a value of "<<r[8]<<" and it is at position 8"<<endl;

	if(r[9]>=r[0] && r[9]>=r[2] && r[9]>=r[3] && r[9]>=r[4] && r[9]>=r[5] && r[9]>=r[6] && r[9]>=r[7] && r[9]>=r[1] && r[9]>=r[8])
		cout<<"The largest array has a value of "<<r[9]<<" and it is at position 9"<<endl;
}
Last edited on
use a loop
1
2
3
for(unsigned int i = 0; i<10; i++)
for(unsigned int j = 0; j<10; j++)
if(r[i]>=r[j]) cout << "The largest array has a value of: " << r[i] << " at position " << j << endl;

there are probably still better ways than to compare each individual thing like 0-9 10 times. but I am tired right now =p

actually thinking about it you could just use the sort method lol much easier

#include <algorithm>
sort(r, r+10); // 10 is the size of the int array
cout << r[9] << endl; // 9 is the last position in the array since it is now
sorted in ascending order. if you don't want to change that array you can
create a temp array and put the r array info into it then sort the temp array
and cout that.
Last edited on
closed account (3qX21hU5)
Yes there is a much better way. Use the max_element() and min_element() functions in the algorithm header :).

max_element() example. max_element() returns a pointer to the largest element in the array. So we would use it like so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
using namespace std;

const int SIZE = 5;

int main()
{
    int myNumber[SIZE] = {1, 4, 56, -4, 68};

    // The first parameter of the function is the start of the continer or where you want to start
    // the search, and the second is one past the lastn umber you want to search. So we do
    // myNumber + 5 because that is the arrays size.
    int *largest = max_element(myNumber, myNumber + SIZE);

    cout << "The largest number is: " << *largest;
}


min_element() example. It works just like the max_element() function but returns the lowest.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
using namespace std;

const int SIZE = 5;

int main()
{
    int myNumber[SIZE] = {1, 4, 56, -4, 68};

    // The first parameter of the function is the start of the continer or where you want to start
    // the search, and the second is one past the lastn umber you want to search. So we do
    // myNumber + 5 because that is the arrays size.
    int *lowest = min_element(myNumber, myNumber + SIZE);

    cout << "The lowest number is: " << *lowest;
}



That is the easiest way to find the minimum number and max number in the array or other containers like vectors. It will also work with other types.

You can find the documentation here

http://www.cplusplus.com/reference/algorithm/min_element/?kw=min_element

http://www.cplusplus.com/reference/algorithm/max_element/?kw=max_element

closed account (D80DSL3A)
This should work:
1
2
3
4
5
int indexToMax = 0;
for(unsigned int i = 1; i<10; i++)
    if( r[i] > r[indexToMax] ) indexToMax = i;

cout<<"The largest element has a value of "<< r[indexToMax] <<" and it is at position "<< indexToMax <<endl;
Topic archived. No new replies allowed.