jrbush82
10-05-2003, 04:46 PM
I'm having some problems trying to figure out what exactly the goal of the program is, I guess if I understood the problem better, I would be able to understand how to write the program. Any help is appreciated.
Below is the specification of a "safe array" class, which halts the program if an array index goes out of bounds. (Recall that C++ does not check for out-or-bounds indexes when you use built-in arrays.)
Implement each member function as it would appear in the implementation file. To halt the program use the exit function supplied by the C++ standard library through the header file cstdlib (see Appendix C).
1 *** CODE WITH PROBLEM ***
2
3 const int MAX_SIZE = 200;
4
5 class IntArray
6 {
7 public:
8 int ValueAt ( /* in */ int i) const;
9 //Precondition:
10 // i is assigned
11 //Postdondition:
12 // IF i >= 0 && i < declared size of array
13 // Function value == value of array element
14 // at index i
15 // ELSE
16 // Program has halted with error message
17
18 void Store ( /* in */ int val, /* in */ int i);
19 //Precondition:
20 // val and i are assigned
21
22 //Postcondition:
23 // IF i >= 0 && i < declared size of array
24 // value is stored in array element i
25 // ELSE
26 // Program has halted with error message
27
28 intArray ( /* in */ int arrSize );
29 //Precondition:
30 // arrSize is assigned
31 //Postcondition:
32 // IF arrSize >= 1 && arrSize <= MAX_SIZE
33 // Array created with all array elements == 0
34 // ELSE
35 // Program has halted with error message
36
37 private:
38 int arr[MAX_SIZE];
39 int size;
40 };
41
42
43 *** WHAT I'VE BEEN ABLE TO DO SO FAR ***
44
45 #include <iostream>
46
47 using namespace std;
48
49 const int MAX_SIZE = 200;
50
51 class IntArray
52 {
53 public:
54 const int ValueAt(int i)
55 {
56 //Precondition:
57 // i is assigned
58 //Postdondition:
59 // IF i >= 0 && i < declared size of array
60 // Function value == value of array element
61 // at index i
62
63 if (i >= 0 && i < MAX_SIZE) {
64 int n = arr[i];
65 return n;
66 } else {
67 exit(1);
68 cerr << endl << " Out-of-bounds index...";
69 }
70 }
71
72 void Store(int val, int i){
73 //Precondition:
74 // val and i are assigned
75
76 //Postcondition:
77 // IF i >= 0 && i < declared size of array
78 // value is stored in array element i
79 // ELSE
80 // Program has halted with error message
81
82 if (i >= 0 && i < MAX_SIZE) {
83 arr[i] = val;
84 } else {
85 exit(1);
86 cerr << endl << " Out-of-bounds index...";
87 }
88 }
89
90 intArray(int arrSize ) {
91 //Precondition:
92 // arrSize is assigned
93 //Postcondition:
94 // IF arrSize >= 1 && arrSize <= MAX_SIZE
95 // Array created with all array elements == 0
96 // ELSE
97 // Program has halted with error message
98
99 if (arrSize >= 1 && arrSize <= MAX_SIZE) {
100 for (int i = 0; i < MAX_SIZE; i++) {
101 arr[i] = 0;
102 }
103 } else {
104 exit(1);
105 cerr << endl << " Out-of-bounds index...";
106 }
107 }
108
109 private:
110 int arr[MAX_SIZE];
111 int size;
112 };
113
114 int main() {
115
116
117
118 return 0;
119 }
Below is the specification of a "safe array" class, which halts the program if an array index goes out of bounds. (Recall that C++ does not check for out-or-bounds indexes when you use built-in arrays.)
Implement each member function as it would appear in the implementation file. To halt the program use the exit function supplied by the C++ standard library through the header file cstdlib (see Appendix C).
1 *** CODE WITH PROBLEM ***
2
3 const int MAX_SIZE = 200;
4
5 class IntArray
6 {
7 public:
8 int ValueAt ( /* in */ int i) const;
9 //Precondition:
10 // i is assigned
11 //Postdondition:
12 // IF i >= 0 && i < declared size of array
13 // Function value == value of array element
14 // at index i
15 // ELSE
16 // Program has halted with error message
17
18 void Store ( /* in */ int val, /* in */ int i);
19 //Precondition:
20 // val and i are assigned
21
22 //Postcondition:
23 // IF i >= 0 && i < declared size of array
24 // value is stored in array element i
25 // ELSE
26 // Program has halted with error message
27
28 intArray ( /* in */ int arrSize );
29 //Precondition:
30 // arrSize is assigned
31 //Postcondition:
32 // IF arrSize >= 1 && arrSize <= MAX_SIZE
33 // Array created with all array elements == 0
34 // ELSE
35 // Program has halted with error message
36
37 private:
38 int arr[MAX_SIZE];
39 int size;
40 };
41
42
43 *** WHAT I'VE BEEN ABLE TO DO SO FAR ***
44
45 #include <iostream>
46
47 using namespace std;
48
49 const int MAX_SIZE = 200;
50
51 class IntArray
52 {
53 public:
54 const int ValueAt(int i)
55 {
56 //Precondition:
57 // i is assigned
58 //Postdondition:
59 // IF i >= 0 && i < declared size of array
60 // Function value == value of array element
61 // at index i
62
63 if (i >= 0 && i < MAX_SIZE) {
64 int n = arr[i];
65 return n;
66 } else {
67 exit(1);
68 cerr << endl << " Out-of-bounds index...";
69 }
70 }
71
72 void Store(int val, int i){
73 //Precondition:
74 // val and i are assigned
75
76 //Postcondition:
77 // IF i >= 0 && i < declared size of array
78 // value is stored in array element i
79 // ELSE
80 // Program has halted with error message
81
82 if (i >= 0 && i < MAX_SIZE) {
83 arr[i] = val;
84 } else {
85 exit(1);
86 cerr << endl << " Out-of-bounds index...";
87 }
88 }
89
90 intArray(int arrSize ) {
91 //Precondition:
92 // arrSize is assigned
93 //Postcondition:
94 // IF arrSize >= 1 && arrSize <= MAX_SIZE
95 // Array created with all array elements == 0
96 // ELSE
97 // Program has halted with error message
98
99 if (arrSize >= 1 && arrSize <= MAX_SIZE) {
100 for (int i = 0; i < MAX_SIZE; i++) {
101 arr[i] = 0;
102 }
103 } else {
104 exit(1);
105 cerr << endl << " Out-of-bounds index...";
106 }
107 }
108
109 private:
110 int arr[MAX_SIZE];
111 int size;
112 };
113
114 int main() {
115
116
117
118 return 0;
119 }