Please help - Array - out of range error

Sep 2, 2020 at 10:55am
Hi All,

Doing an assignment with one aspect being working out the maximum amount of a material used. Loading the material used into an array, then a maximum function that returns the highest. However, I get an out of range error. I thought I understood out of range issues with arrays..I only have 5 elements! Please help!

Output==================
0.61/1.7/0.52/1.34/0.25
The minimum isterminate called after throwing an instance of 'std::out_of_range'
what(): array::at: __n (which is 5) >= _Nm (which is 5)

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
#include <iostream>
#include <iomanip>
#include <array>
#include <string>

using namespace std;

float maximum(array<float, 5> score);

int main()
{    

    array<float, 5> MaterialusedArray {0.61, 1.70, 0.52, 1.34, 0.25};

    std::cout << MaterialusedArray.at(0)
              << "/" << MaterialusedArray.at(1)
              << "/" << MaterialusedArray.at(2)
              << "/" << MaterialusedArray.at(3)
              << "/" << MaterialusedArray.at(4)
              << std::endl;

    std::cout << "The maximum is" << maximum(MaterialusedArray);


    return 0;
}

float maximum(array<float, 5> score)
{
    float num = score.at(0);

    for (unsigned int i=1; 1<score.size(); i++)
    {
        if (score.at(i) > num)
            num = score.at(i);
    }
    return num;
}
Sep 2, 2020 at 11:07am
 
for (unsigned int i=1; 1<score.size(); i++)


you have 1 < rather than i < !
Sep 2, 2020 at 11:09am
Have a closer look at 1<score.size();
It will always be true.
Sep 2, 2020 at 11:11am
omg.... thank you so much... My brain is so burnt out.

The error is no longer there, but the output is showing the 1.70 element.. which isn't the lowest >_>_>_>_>

EDIT: WHoops im confused again, this is my maximum function, NOT my minimum function.

Thank you for the help!
Last edited on Sep 2, 2020 at 11:12am
Sep 2, 2020 at 11:13am
also note that you are passing the array by value, rather than by reference - so a copy of the array is performed when passing to maximum. Use const array<float, 5>& instead.

Also note, that there is an easier way to calculate the maximum value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	array<float, 5> MaterialusedArray {0.61, 1.70, 0.52, 1.34, 0.25};

	std::cout << MaterialusedArray.at(0)
		<< "/" << MaterialusedArray.at(1)
		<< "/" << MaterialusedArray.at(2)
		<< "/" << MaterialusedArray.at(3)
		<< "/" << MaterialusedArray.at(4)
		<< std::endl;

	std::cout << "The maximum is " << *std::max_element(MaterialusedArray.begin(), MaterialusedArray.end());
}

Sep 2, 2020 at 11:19am
We are only 1st year uni - and we are only just getting into passing by reference.

Also, this assignment is supposed to be modular, and there is many values to pass through minimum, maximum, and average functions.. So your suggestion is a lil over my head and doesn't fit the brief, but thank you for the insight.
Sep 2, 2020 at 12:38pm
Now, you have both:

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
#include <iostream>
#include <iomanip>
#include <array>
#include <string>

using namespace std;

float minimum(array<float, 5> score); // <--

int main()
{

    array<float, 5> MaterialusedArray {0.61, 1.70, 0.52, 1.34, 0.25};

    std::cout << MaterialusedArray.at(0)
              << "/" << MaterialusedArray.at(1)
              << "/" << MaterialusedArray.at(2)
              << "/" << MaterialusedArray.at(3)
              << "/" << MaterialusedArray.at(4)
              << std::endl;

    std::cout << "The maximum is" << minimum(MaterialusedArray); // <--


    return 0;
}

float minimum(array<float, 5> score) // <--
{
    float num = score.at(0);

    for (unsigned int i=1; i<score.size(); i++)
    {
        if (score.at(i) < num) // <--
            num = score.at(i);
    }
    return num;
}
Topic archived. No new replies allowed.