Number Array Class get highest function

Jul 24, 2019 at 9:27pm
Hello, I'm attempting to brush up on my C++ and am having issues with this program challenge. Everything was going good, but then I started having issues with my getHighest function (which returns the highest float in the array.) When attempting to use the SIZE data member inside a for loop I get a loop that goes on forever, but when I replace SIZE with an actual int value the loop will terminated as it's suppose to. I'm thinking I could just pass the int value of SIZE into the function and use that, but I would hope there's a better way. Can anyone see where I'm going wrong? Thanks!

Header File:
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
class NumberArray
{
private:
    int SIZE;
    float* numArray;
    
public:
    NumberArray(int);
    ~NumberArray();
    
    // Store a number in any element of the array
    addNumber(float num, int ele);
    // Retrieve a number from any element of the array
    float getNumber(int ele);
    // Return the highest value stored in the array
    float getHighest();
    // Return the lowest value stored in the array
    float getLowest();
    // Return the average of all the numbers stored in the array
    float getTheAverage();
    
    // Finds if this is a valid element to add too
    bool validEle(int ele);

};


CPP File:
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
#include "NumberArray.h"

NumberArray::NumberArray(int arraysize)
{
    float* numArray;
    numArray = new float [SIZE];
 }

NumberArray::~NumberArray()
{
    delete[] numArray;
}

bool NumberArray::validEle(int ele) {
    bool status;
    
    if (ele < 0 || ele >= SIZE)
        status = false;
    else
        status = true;
    return status;
}

NumberArray::addNumber(float num, int ele) {
    if(validEle(ele))
        numArray[ele] = num;
}

float NumberArray::getNumber(int ele) {
    float value{};
    
    if (validEle(ele))
        value = numArray[ele];
    return value;
}

float NumberArray::getHighest() { 
    float highest{0};
    highest = numArray[0];
    
    for(int i{1}; i < SIZE; i++) {
        if (numArray[i] > highest)
            highest = numArray[i];
    }
    return highest;
}


main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "NumberArray.h"

using namespace std;

int main() {
    
    NumberArray numArr{5};
    numArr.addNumber(20.1, 0);
    numArr.addNumber(25.0, 1);
    numArr.addNumber(1.2, 2);
    numArr.addNumber(13,3);
    numArr.addNumber(-1,4);
    
    // Should return 25
    cout << numArr.getHighest() << endl;
    
    
    return 0;
}


Like I said, when it comes to returning 25 in main I can only get it to do so when I replace the SIZE in getHighest() line for(int i{1}; i < SIZE; i++) with a 5.. As I type this I'm thinking that maybe I should use a getter considering SIZE is private?? Hmm....

Edit: Nope using a getter didn't work.

Double Edit: And now I've done something to where it crashes each time.. Ugh -_- I think my problem was that SIZE was never even in use. So passing the size of the array into the getHighest function was probably my best bet, but now since I can't get the program back into a working state for some odd reason I'll just have to scrap this challenge and move onto the next.
Last edited on Jul 24, 2019 at 9:42pm
Jul 24, 2019 at 9:46pm
You are never initializing the SIZE to arraysize in the constructor.
Even worse, you created a local variable shadowing the member variable numArray, so you never even really create a proper array, let alone of a given size.

1
2
3
4
5
6
7
8
9
10
11
NumberArray::NumberArray(int arraysize)
    : size(arraysize), numArray(new float[size]) {
}

bool NumberArray::validIndex(int index) {
    return index >= 0 && index < size;
}

float NumberArray::getNumber(int index) {
    return validIndex(index) ? numArray[index] : {};
}

Also note that it's so easy to make this a template that it's kind of silly not to.
Last edited on Jul 24, 2019 at 9:48pm
Jul 24, 2019 at 10:00pm
Thank you, it looks like the SIZE data member was the problem.. And can you please explain what you mean by "shadowing" the member variable numArray and where I did it? Thank you! My program isn't crashing anymore, now I can implement the getLowest() and getAverage() functions I wanted to do.
Jul 24, 2019 at 10:02pm
In your constructor, even if you set SIZE to arraysize (which I've added), it is still wrong since you have shadowed your numArray member variable with an identically-named local variable:

1
2
3
4
5
6
NumberArray::NumberArray(int arraysize)
{
    float* numArray;    // GET RID OF THIS!!!
    SIZE = arraysize;
    numArray = new float [SIZE];
}

Last edited on Jul 24, 2019 at 10:03pm
Jul 24, 2019 at 11:28pm
Ah! I see what you're saying now. Thanks so much for clarifying that for me.
Topic archived. No new replies allowed.