Classes.

So I'm trying to make code for an assignment that asks for us to create an array with a random size between 10 and 20 filled with random ints between 1 and 10. We have to create an array, sort the array from least to greatest, compute the mode, and display the array along with its mode. When i run my code for class object, i get nothing for my array, and big random numbers for my mode and size. Anyone help?

Here is my class template (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
26
  #ifndef CS132_udodik_Lab02_ModeCalculator_H
#define CS132_udodik_Lab02_ModeCalculator_H

#include <iostream>

using namespace std;

class ModeCalculator
{
	private:
		int mode;
		int size;
		int *randomArr;

	public:
		void sortArray();
		void computeMode();
		void printMode();
		int getSize() { return size; }
		int getMode() { return mode; }

		ModeCalculator()
		{
		}
};
#endif 


Here is my .cpp file with my functions:
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
95
96
97
98
99
100
101
102
103
104
#include "CS132_udodik_Lab02_ModeCalculator.h"
#include <iostream>

using namespace std;


void ModeCalculator::sortArray()
{
   //variables and dynamic array creation
   int size = rand() % 11 + 10;
   int *arr;
   arr = new int [size];

   //sort variables
   bool swap;
   int temp;

	//fills the array with random numbers between 1-10
	for (int i = 0; i < size; i++)
	{
		arr[i] = rand() % 10 + 1;
   }

	//the next 14 lines of code sort the contents of array from least to greatest
	//This code can be found on page 466 on Gaddis Starting Out With C++ From
	//Control Structures through Objects 7th Edition
   do
   {
		swap = false;
      for (int i = 0; i < (size - 1); i++)
         {
            if (arr[i] > arr[i + 1])
				{
					temp = arr[i];
					arr[i] = arr[i + 1];
					arr[i + 1] = temp;
					swap = true;
				}
			}
	} while (swap);
  
	randomArr = arr;

	//delete dynamic array
   delete [] arr;

	 
}

void ModeCalculator::computeMode()
{
  int count = 1;
   int maxValue = 0;

	//We did not use our own code for this. We had used a code that we found online
	//on http://www.cplusplus.com/forum/beginner/116489/
	//We have only changed a few variable names and implemented the pointers
	//that we needed to complete our task. All credits go to abhishekm71

	//this section of code computes the mode from a sorted array
   int mode1 = randomArr[0];
   for (int i = 0; i < size - 1; i++)
	{
		if (randomArr[i] == randomArr[i+1])
		{
			count++;
         if (count > maxValue)
			{
				maxValue = count;
            mode1 = randomArr[i];
         }
      } else

		//reset counter
      count = 1; 
   }
  
   if (mode1 == 1)
   {
		mode = -1;
   }

   else 
	{
		mode = mode1;
   }

}

void ModeCalculator::printMode()
{
	//displays array contents
	cout << "This is the sorted array: ";
   for (int i = 0; i < size; i++)
   {
      cout << randomArr[i] << " ";
   }

	//displays other array information (mode and size)
   cout << endl;
   cout << "The mode for this array is: " << mode << endl;
	cout << "The size of the array is: " << size << endl;
   cout << endl;
}


Here is my main:

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

using namespace std;

int main()
{
	//
	srand(unsigned(time(NULL)));

	ModeCalculator m1;

	m1.sortArray();
	m1.computeMode();
	m1.printMode();

	return 0;
}
Last edited on
1) Your randomArr pointer is never initialized, so all operations with it leads to UB.
2) sortArray() method does not do anything at all. Literally. It can be optimised away entirely in theory.
Topic archived. No new replies allowed.