[1st year University Comp Sci] Using a ManagedArray and a GuardedArray to create a Polynomial class

I've been tasked with creating a C++ class called Polynomial that applies different mathematical formulas to Polynomial I was given 4 separate bits of code and I'm a little confused as to what exact it is asking of me here, I think I have to use what was given to create my new class. But i'm not too sure how, would someone be able to explain it to me or point me in the right direction?

"As for data representation, a polynomial is composed of a ManagedArray of coefficients. The last element of the array (i.e., the one with the largest index) must be a non-zero coefficient. With this representation, the zero polynomial is uniquely represented by an empty array. As well, the degree of a non-zero polynomial can be obtained by subtracting one from the current size of the array."

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
  #ifndef __GUARDED_ARRAY_H__
#define __GUARDED_ARRAY_H__

typedef int ItemType;

const unsigned MAX_LENGTH = 500;

//
// GuardedArray
// - A wrapper class for C++ arrays to make array access safe.
//   Specifically, initialization is guaranteed, and assertions are
//   in place to detect array index out of bound errors in array member
//   accesses.
//

class GuardedArray {
public:
	//
	// Constructor
	//
	// Purpose: Initializes array elements to zeros
	// Argument(s): N/A
	// Side Effect: All array elements are initialized to zero.
	// Return: N/A
	//

	GuardedArray();

	//
	// Constructor
	//
	// Purpose: Initializes all array elements to a given value.
	// Argument(s):
	//  x : value to which array elements should be initialized.
	// Side Effect: All array elements are initialized to x.
	// Return: N/A
	//

	GuardedArray(ItemType x);

	//
	// read
	//
	// Purpose: Read array element at index i.
	// Argument(s):
	//  i : index of element to be read.
	// Precondition(s): i < MAX_LENGTH
	// Return: Value of array element at index i
	// 

	ItemType read(unsigned i) const;

	//
	// write
	//
	// Purpose: Write x into array element at index i.
	// Argument(s):
	//  i : index of array element to be overwritten.
	//  x : value to be stored into array.
	// Precondition(s): i < MAX_LENGTH.
	// Side Effect: The array element at index i will be 
	//              overwritten by x.
	// Return: N/A
	//

	void write(unsigned i, ItemType x);

private:

	ItemType array[MAX_LENGTH];

};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cassert>
#include "polynomial.h"
#include "guarded_array.h"
#include "managed_array.h"

GuardedArray::GuardedArray() {
	for (unsigned i = 0; i < MAX_LENGTH; i++)
		array[i] = 0;
}

GuardedArray::GuardedArray(ItemType x) {
	for (unsigned i = 0; i < MAX_LENGTH; i++)
		array[i] = x;
}

ItemType GuardedArray::read(unsigned i) const {
	assert(i < MAX_LENGTH);
	return array[i];
}

void GuardedArray::write(unsigned i, ItemType x) {
	assert(i < MAX_LENGTH);
	array[i] = x;
}

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef __MANAGED_ARRAY_H__
#define __MANAGED_ARRAY_H__

#include "guarded_array.h"

//
// ManagedArray
// - A wrapper class for C++ arrays to facilitate insertion and removal of
//   array elements.
// - Every instance of ManagedArray has a size that can be increased
//   until the maximum capacity MAX_LENGTH is reached.
//

class ManagedArray {
public:
	//
	// Constructor
	//
	// Purpose: Initializes array to have zero size.
	// Argument(s): N/A
	// Side Effect: The array is initialized to be empty.
	// Return: N/A
	//

	ManagedArray();

	//
	// Constructor
	//
	// Purpose: Initializes array to a given size.  All array elements
	//          are initialized to zero.
	// Argument(s):
	//  N : size of array
	// Precondition: N <= MAX_LENGTH
	// Side Effect: The array is initialized to contain N occurrences
	//              of the number zero.
	// Return: N/A
	//

	ManagedArray(unsigned N);

	//
	// Constructor
	//
	// Purpose: Initializes array to a given size.  All array elements
	//          are initialized to x.
	// Argument(s):
	//  N : size of array
	//  x : initial value of array elements
	// Precondition: N <= MAX_LENGTH
	// Side Effect: The array is initialized to contain N occurrences
	//              of x.
	// Return: N/A
	//

	ManagedArray(unsigned N, ItemType x);

	//
	// size
	//
	// Purpose: Return the current size of the array.
	// Argument(s): N/A
	// Return: current size of the array
	//

	unsigned size() const;

	//
	// read
	//
	// Purpose: Read array element at index i.
	// Argument(s):
	//  i : index of element to be read.
	// Precondition: i < size()
	// Return: Value of array element at index i.
	//

	ItemType read(unsigned i) const;

	//
	// write
	//
	// Purpose: Write x into array element at index i.
	// Argument(s):
	//  i : index of array element to be overwritten.
	//  x : value to be written into array.
	// Precondition: i < size()
	// Return: N/A.
	//

	void write(unsigned i, ItemType x);

	//
	// insert
	//
	// Purpose: Insert an element into the array.
	// Argument(s):
	//  i : index at which x is to be inserted
	//  x : value to be inserted into array.
	// Precondition: i <= size() && size() < MAX_LENGTH
	// Side Effect: The new element x will be inserted into the
	//              the array at index i.  Array elements will be 
	//              shifted to make room for x.  The array size 
	//              will be increased by one.
	// Return: N/A.
	//

	void insert(unsigned i, ItemType x);

	//
	// remove(i)
	//
	// Purpose: Remove an element from the array.
	// Argument(s):
	//   i : index of the element to be removed.
	// Precondition: i < size() && size() > 0
	// Side Effect: Remove the array element at index i.  Array 
	//              elements will be shifted to fill the "gap" left 
	//              by the removed element.  The array size will be 
	//              decreased by one.
	// Return: N/A.
	//

	void remove(unsigned i);

private:

	unsigned count;
	GuardedArray array;

};

#endif 

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
#include <cassert>
#include "polynomial.h"
#include "guarded_array.h"
#include "managed_array.h"

ManagedArray::ManagedArray() : array() {
	count = 0;
}

ManagedArray::ManagedArray(unsigned N) : array() {
	assert(N <= MAX_LENGTH);
	count = N;
}

ManagedArray::ManagedArray(unsigned N, ItemType x) : array(x) {
	assert(N <= MAX_LENGTH);
	count = N;
}

unsigned ManagedArray::size() const {
	return count;
}

ItemType ManagedArray::read(unsigned i) const {
	assert(i < count);
	return array.read(i);
}

void ManagedArray::write(unsigned i, ItemType x) {
	assert(i < count);
	array.write(i, x);
}

void ManagedArray::insert(unsigned i, ItemType x) {
	assert(i <= count && count < MAX_LENGTH);
	for (unsigned j = count; j > i; j--)
		array.write(j, array.read(j - 1));
	array.write(i, x);
	count++;
}

void ManagedArray::remove(unsigned i) {
	assert(i < count && count > 0);
	for (unsigned j = i; j < count - 1; j++)
		array.write(j, array.read(j + 1));
	count--;
}
Example polynomial:
a*x^4 + b*x^3 + c*x^2 + d*x + e
1
2
3
4
5
6
ItemType foo[5] {};
foo[0] = e;
foo[1] = d;
foo[2] = c;
foo[3] = b;
foo[4] = a;

The foo has 5 elements. The rank of polynomial is thus 4.

It should be relatively easy to evaluate the polynomial with given x:
1
2
3
4
5
6
7
8
9
ItemType bar( ItemType x ) {
  ItemType y = 1;
  ItemType value = 0;
  for ( unsigned c=0; c < 5; ++c ) {
    value += y * foo[c];
    y *= x;
  }
  return value
}

Topic archived. No new replies allowed.