How to return std::array<int, size?> arrName ?

I have the code:
1
2
3
4
5
6
7
8
9
std::array<int, const int numberOfWeights> getContainerInput(const int numberOfWeights) {
    std::array<int, numberOfWeights> fruitWeights;

    for (int i = 0; i < numberOfWeights; ++i) {
        std::cin >> fruitWeights[i];
    }

    return fruitWeights;
}


I have no idea what to put for this part of the code:
<int, const int numberOfWeights> because this gives a ton of errors (all related to this type of issue I believe). I can't have a hardcoded number as the <int, NUM_HERE> because I ask for input to get that number (std::cin).

I've also tried replacing const int numberOfWeights with int and unsigned int and const int size etc.. No luck.

How can I fix this?
Please Note: If possible I'd like to use std::array not std::vector.

Help would be greatly appreciated. Thank you!

Note: If you would like to see my full code then here:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <array>
#include <cstring>
#include <cassert>

void test();
std::array<int, const int numberOfWeights> getContainerInput(const int numberOfWeights);
int getMaxNumOfFruits(const int bellySize, const std::array<int, const int size> weights);


int main(int argc, char* argv[]) {
    if (argc > 1 && strncmp(argv[1], "test", 4) == 0) {
        test();
    }
    else {
        int numberOfWeights = 0, sizeOfBelly = 0;
        std::cin >> numberOfWeights >> sizeOfBelly;

        const int size = numberOfWeights;
        std::array<int, size> weights = getContainerInput(numberOfWeights);

        std::cout << getMaxNumOfFruits(sizeOfBelly, weights) << '\n';
    }
}


std::array<int, const int numberOfWeights> getContainerInput(const int numberOfWeights) {
    std::array<int, numberOfWeights> fruitWeights;

    for (int i = 0; i < numberOfWeights; ++i) {
        std::cin >> fruitWeights[i];
    }

    return fruitWeights;
}


int getMaxNumOfFruits(const int bellySize, const std::array<int, const int size> weights) {
    const int numOfWeights = weights.size();
    int maxNumOfFruitsEaten = 0;
    bool continueLoop = true;

    for (int i = 0; i < numOfWeights && continueLoop == true; ++i) {
            int weightOfFruitsEaten = 0, countOfFruitsEaten = 0; 

            for (int weightPosition = i; weightPosition < numOfWeights && continueLoop == true; ++weightPosition) {
                if (weightOfFruitsEaten + weights[weightPosition] <= bellySize) { 
                    weightOfFruitsEaten += weights[weightPosition]; 
                    ++countOfFruitsEaten;
                    if (weightOfFruitsEaten == bellySize) {
                        continueLoop = false;
                    }
                }
            }

            if (countOfFruitsEaten > maxNumOfFruitsEaten) {
                maxNumOfFruitsEaten = countOfFruitsEaten;
            }
        }
    return maxNumOfFruitsEaten;
}


void test() {
    int numOfWeights = 10, bellySize = 3;
    //const int size = numOfWeights;
    std::array<int, numOfWeights> weights = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    assert(getMaxNumOfFruits(bellySize, weights) == 2);

    bellySize = 10;
    assert(getMaxNumOfFruits(bellySize, weights) == 4);

    bellySize = 60;
    assert(getMaxNumOfFruits(bellySize, weights) == 10);
}
Last edited on
I can't have a hardcoded number as the <int, NUM_HERE> because I ask for input to get that number (std::cin).
Then you can't have a (non-dynamic) array, unless you're willing to pre-allocate a "max" hardcoded number that the array could be, and then only access a sub-span of that array.

i.e. you have std::array<int, LargeHardcodedNumber> weights;
But you only assign values to and use the first numberOfWeights elements that the user provides.

The size of an array has to be known at compile time. If it isn't known, the easiest thing here, in my opinion, would be to use a vector. The only alternative is using raw dynamic arrays (i.e. new[]) but that's essentially just a vector.
Last edited on
Ganado wrote:
Then you can't have a (non-dynamic) array, unless you're willing to pre-allocate a "max" hardcoded number that the array could be, and then only access a sub-span of that array.

i.e. you have std::array<int, LargeHardcodedNumber> weights;
But you only assign values to and use the first numberOfWeights elements that the user provides.

The size of an array has to be known at compile time. If it isn't known, the easiest thing here, in my opinion, would be to use a vector. The only alternative is using raw dynamic arrays (i.e. new[]) but that's essentially just a vector.

Ah okay. I see what you mean, thank you for your reply & providing an example. :)

Issue solved.


----------------
Please Note for anyone else that may reply, because the post is solved I won't check it further. If you'd like to contact me the only way would be to DM me.
Last edited on
Topic archived. No new replies allowed.