When Compiling, Value Not What It Should Be

EDIT: I realized what was wrong. Thanks anyways.
My assignment is the following:
Design a class that has an array of floating-point numbers.The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The destructor should free the memory held by the array. In addition, there should be member functions to perform the following operations:
-Store a number in any element of the array
-Retrieve a number from any element of the array
-Return the highest value stored in the array
-Return the lowest value stored in the array
-Return the average of all the numbers stored in the array
Demonstrate the class in a program.

I have attached the code that I have written. When I run the program, the value for the value of the smallest element always comes out to be 1.4013e-45, and I don't know what I should do to fix it.

Thank you.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>

using namespace std;

class NumArray
{
private:
    float* ptrarray;
    int sizearray;
public:
    NumArray(int i)
    {
        ptrarray=new float[i];
        sizearray=i;
    }
    ~NumArray()
    {
        delete ptrarray;
    }
    void setnumelem(int j, int numelem)
    {
        *(ptrarray+j)=numelem;
    }
    float getelem(int k)
    {
        return *(ptrarray+k);
    }
    float highestval()
    {
        float large;
        int m;
        m=*ptrarray;
        for (m=0; m<sizearray; m++)
        {
            if (*(ptrarray+m)>large)
                large = *(ptrarray+m);
        }
        return large;
    }
    float lowestval()
    {
        float small;
        int n;
        n=*ptrarray;
        for (n=0; n<sizearray; n++)
        {
            if (*(ptrarray+n)<small)
                small = *(ptrarray+n);
        }
        return small;
    }
    float getavg()
    {
        float sumelem;
        float avg;
        int l;
        for (l=0; l<sizearray; l++)
        {
            sumelem+=*(ptrarray+l);
        }
        avg=sumelem/sizearray;
        return avg;
    }

};

int main()
{
    int sizearray;
    int p;
    float num;

    cout << "How many elements do you wish to store in the array?" << endl;
    cin >> sizearray;
    NumArray elements(sizearray);
    cout << "Enter the elements you wish to store in the array: " << endl;
    for (p=0; p<sizearray; p++)
    {
        cout << "Enter element " << p+1 << ": ";
        cin >> num;
        elements.setnumelem(p, num);
    }
    cout << "The elements in the array are: " << endl;
    for (p=0; p<sizearray; p++)
    {
        cout << elements.getelem(p) << " ";
    }
    cout << endl << "The highest value stored is " << elements.highestval() << "." << endl;
    cout << "The lowest value stored is " << elements.lowestval() << "." << endl;
    cout << "The average value of the elements entered is " << elements.getavg() << "." << endl;

    return 0;
}
Last edited on
Your code for working out what the smallest value is, is wrong (as is your code for largest value, and average value).

You don't set a value for small on line 42. So it could be anything. What happens if it's smaller than the smallest number the user entered?

You make a similar mistake on line 30 and 54. Here's a good idea for you; never ever use a variable that has never had a value set. Don't read it, don't compare with it, don't add it to anything, nothing nothing nothing, except setting its value.

While I'm here, lines 32 and 44 look wrong too.
Last edited on
don't set a value for small on line 42
Topic archived. No new replies allowed.