1st time using Class, getting garbage output & crashing

Hello all, I hate posting here with my questions, but I'm using this as a last resort as my head is spinning.

Background:
My assignment is to create a class that stores up to 10 integers in an array and defines 4 public methods:
setNumber - accepts integer values to store in array. Stored in next available element of array, returns true if there is room, returns false if not. ***NEED HELP ON THIS ONE***

clear - removes all values from the array so it can be reused.
***This seems really simple, but I cannot find anything to just clear all the numbers. Do I just set the size of the array back to 0?"

displayNumbers - self explanatory, think I'm good here but it is currently displaying garbage and then crashing.

getStats - determines largest, smallest, and average of values in array. Values are returned via reference parameters. Actually think i'm okay here but program is never getting that far so don't know for sure.

Also, think I have an issue with the constructor but I can't think of what else to do there.

Here's my implementation 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include "Array.h"
#include <iostream>
using namespace std;


Array::Array()
{
   numbers[0];
}



void Array::setNumber()
{	
   for(int i = 0; i < 10; i++)
   {
      input >> numbers[i];
   }
}


void Array::clear()
{
   numbers[0];
}


void Array::displayNumbers()
{
   for (int i = 0; i < 10; i++)
   {
      cout << "The numbers are " << numbers[i] << ", ";
   }
}


void Array::getStats(int large, int small, int avg)
{
   for (int i = 1; i < 10; i++)
   {
      if (numbers[i] > numbers[large])
      {
         large = i;
      }
   }
   
   for (int index = 1; index < 10; index++)
   {
      if (numbers[index] < numbers[small])
      {
         small = index;
      }
   }
   
   
   int numTotal;
   for (int j = 0; j < 10; j++)
   {
      numTotal += numbers[j];
   }
   
   avg = numTotal / 10;
}


And here's my client code:
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
#include <iostream>
#include "Array.h"

using namespace std;


int main()
{
	
   int largest;
   int smallest;
   int average;
   int input;
   char repeat = ' ';
	
   Array myArray;
	
   do
   {
	for(int count = 0; count < 10; count++)
	{
	    cout << "Enter number " << count + 1 << ": ";
	    cin >> input;
	    myArray.setNumber();
	}
		
	myArray.displayNumbers();
		
	myArray.getStats(largest, smallest, average);
	cout << "The values range from " << smallest << " to " << largest << 
                " with an average " << " value of " << average;
		
        "Do you have another set of 10 numbers? (y or n): ";
	cin >> repeat;
	if (repeat == 'y' || repeat == 'Y')
	{
	    myArray.clear();
	}
   }
   while(repeat == 'y' || repeat == 'Y');
}
1) In your constructor, numbers[0] won't do anything.

2) Same for the clear function. What I would do there is loop through the array and set all
values to 0.

3) Your SetNumber function will overwrite anything that is already in the array.

4) If you want to know if your getStats function works, it doesn't, as you don't pass the
parameters by reference. A correct prototype would be
void getStatsByReference(int &smallest, int &largest, int &average);

I hope this answers a few of your questions.

Also, you shouldn't hate to post your questions here, this forum exists to answer questions like yours :)
1) I kind of figured that but have no idea what else to do for a constructor.

2) Thank you for confirming this as I had thought of doing it that way as well just didn't know if there was like a clear function that I was unaware of

3) I get setNumber will overwrite but I feel like it's currently not working correctly. Do you see any other errors that need fixing?

4) Yes you are correct here, oversight on my part.

Right now, I am getting to input the numbers 1-10 and then it is spitting out different numbers over and over again, and then crashing without doing anything else. Would this be because of the constructor or because of an error in setNumber, etc.?

Thank you for responding!
**** UPDATE ****

After playing around with this a little more I'm now getting a different output. It is still far from correct, but I thought posting it may help me get help. I'm thinking it HAS to be something with my setNumber() function at this point as it is not printing out the numbers I am inputting.

Implementation 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
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
#include "Array.h"
#include <iostream>
using namespace std;


Array::Array()
{
	for(int i = 0; i < 10; i++)
	{
		numbers[i] = 0;
	}
}



void Array::setNumber()
{
	
	for(int i = 0; i < 10; i++)
	{
		input >> numbers[i];
	}
}


void Array::clear()
{
	for(int i = 0; i < 10; i++)
	{
		numbers[i] = 0;
	}
}


void Array::displayNumbers()
{
    cout << "The numbers are ";
    for (int i = 0; i < 10; i++)
    {
        cout << numbers[i] << ", ";
    }
}


void Array::getStats(int &large, int &small, int &avg)
{
   for (int i = 1; i < 10; i++)
   {
      if (numbers[i] > numbers[large])
      {
         large = i;
      }
   }
   
   for (int index = 1; index < 10; index++)
   {
      if (numbers[index] < numbers[small])
      {
         small = index;
      }
   }
   
   
   int numTotal;
   for (int j = 0; j < 10; j++)
   {
      numTotal += numbers[j];
   }
   
   avg = numTotal / 10;
}


client 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
#include <iostream>
#include "Array.h"

using namespace std;


int main()
{
	
	int largest;
	int smallest;
	int average;
	int input;
	char repeat = ' ';
	
	
	Array myArray;
	do
	{
		for(int count = 0; count < 10; count++)
		{
			cout << "Enter number " << count + 1 << ": ";
			cin >> input;
			myArray.setNumber();
		}
		
		myArray.displayNumbers();
		
		cout << endl;
		
		myArray.getStats(largest, smallest, average);
		cout << "The values range from " << smallest << " to " << largest << " with an average "
		<< " value of " << average;
		
		"Do you have another set of 10 numbers? (y or n): ";
		cin >> repeat;
		if (repeat == 'y' || repeat == 'Y')
		{
			myArray.clear();
		}
	}
	while(repeat == 'y' || repeat == 'Y');
}	// end main 



Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Enter number 8: 8
Enter number 9: 9
Enter number 10: 10
The numbers are 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 
The values range from 0 to 2 with an average  value of 187800635
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
#include <iostream>

struct array
{
    // we don't need to declare a constructor
    // the implicitly declared constructor will initialise the array

    void clear() ; // set all elements in the array to zero

    void display_numbers() const ; // const: does not modify the array

    void get_stats( int& large, int& small, double& avg ) const ; // use double for average

    // convenient: easy to change the number of elements in the array
    static constexpr int N = 10 ;

    // default member initialiser: initialises numbers with all zeroes
    // http://www.stroustrup.com/C++11FAQ.html#member-init
    int numbers[N] {} ;
};

void array::clear()
{
    // range based loop http://www.stroustrup.com/C++11FAQ.html#for
    for( int& n : numbers ) n = 0 ;

    // *this = {} ; // alternative (cute) way of doing it.
    // assign to this array a default constructed array (which has numbers as all zeroes)
}

void array::display_numbers() const
{
    std::cout << "The numbers are [ " ;
    for( int n : numbers ) std::cout << n << ' ' ;
    std::cout << "]\n" ;
}

void array::get_stats( int& large, int& small, double& avg ) const
{
    int sum = numbers[0] ; // sum so far is the first number
    large = numbers[0] ; // largest so far is the first number
    small = numbers[0] ; // smallest so far is the first number

    // for each number other than the first number
    for( int i = 1 ; i<N ; ++i )
    {
        if( numbers[i] > large ) large = numbers[i] ;
        else if( numbers[i] < small ) small = numbers[i] ;
        sum += numbers[i] ;
    }

    avg = double(sum) / N ; // avoid integer division
}

bool again()
{
    std::cout << "again (y/n)? " ;
    char c ;
    std::cin >> c ;
    return c == 'y' || c == 'Y' ;
}

int main()
{
    array myArray ;

    do
    {
        for( int i = 0 ; i < myArray.N ; ++i )
        {
            std::cout << "enter number #" << i+1 << ": " ;
            std::cin >> myArray.numbers[i] ;
        }

        myArray.display_numbers() ;

        int largest, smallest ;
        double average ;
        myArray.get_stats( largest, smallest, average );
        std::cout << "The values range from " << smallest << " to " << largest
                  << " with an average value of " << average << "\n\n" ;
    }
    while( again() ) ;
}
I think the assignment is looking for something like this:
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
struct array {
  bool set_number(int value) {
    bool const can_insert = _size < max_capacity;
    if (can_insert) _data[_size++] = value;
    return can_insert;
  }

  void clear() { _size = 0; }
  int size() const { return _size; }

  void get_stats(int& avg, int& min, int& max) {
    // TODO: your implementation here
    avg = min = max = 42;
  }
  static constexpr int max_capacity = 10;
private:
  int _data[max_capacity];
  int _size = 0;
};

# include <iostream>
int main() {
  array a;

  std::cout << "Please enter a few numbers (ctrl-D/ctrl-Z to stop).\n";
  for (int input; std::cin >> input; )
    if (! a.set_number(input)) {
      std::cout << "There is no more space in the array!\n";
      break;
    }

  std::cout << "The array contains "
            << (a.size() == a.max_capacity? "the maximum of ": " ") << a.size()
            << " numbers.\n";

  int avg, min, max; a.get_stats(avg, min, max);
  std::cout << "The mean element is " << avg << '\n';
  std::cout << "The smallest element is " << min << '\n';
  std::cout << "The greatest element is " << max << '\n';
}


The fundamental idea being the following:

Create an array of size C = 10. We'll call C the "capacity" of the array.
Keep track of an integer named S, which stands for the "size" of the array. Let S contain the value 0.

Each time a value needs to be added to your array (with set_value), if and only if S is less than C, write the new value at index S, and increment S, then return true. Otherwise, return false.

This denotes the difference between the concepts of "size" (the number of elements stored) and "capacity" (the number of elements it is currently possible to store) when it comes to containers, and nicely paves the way for a slightly more complicated assignment like the following:
http://www.cplusplus.com/forum/beginner/221604/
Last edited on
@JLBorges & @mbozzi,

Appreciate the help but both of these solutions have a lot of things we haven't learned yet. The assignment is looking for this to be accomplished with basic Class structure. I'm sure there may be a thing or too I can use off of these though.
closed account (48T7M4Gy)
accepts integer values to store in array. Stored in next available element of array, returns true if there is room, returns false if not. ***NEED HELP ON THIS ONE***


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
#include <iostream>
using namespace std;

class Array{
public:
    Array(){};
    
    bool setNumber(int aNumber)
    {
        if(count < limit)
        {
            ary[count] = aNumber;
            count++;
            return true;
        }
        
        return false;
    }
    
    void print()
    {
        for(int i = 0; i < count; ++i)
            std::cout << ary[i] << ' ';
        std::cout << '\n';
    }
    
private:
    int limit = 10;// for test purpose
    int count = 0;
    int *ary = new int[limit]{0};
    
};

int main()
{
    Array test;
    for (int i = 0; i < 12; ++i)
    {
        if( test.setNumber(5*i) )
            cout << i << " number entered\n";
        else
            cout << "Array is full\n";
    }
    
    test.print();
    
    return 0;
}
0 number entered
1 number entered
2 number entered
3 number entered
4 number entered
5 number entered
6 number entered
7 number entered
8 number entered
9 number entered
Array is full
Array is full
0 5 10 15 20 25 30 35 40 45 
Program ended with exit code: 0
closed account (48T7M4Gy)
PS Ignore the comment on line 28. You can change the value to say 2 so that manually inputting entries is easier for test purposes.
Topic archived. No new replies allowed.