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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/*
In this program, your task is to comp1ete
all of the function definitions.
Do NOT change any of the given code.
You will find errors which point to the main().
when that happens, do NOT change the main function code.
The errors are in your function code.
If you have any questions, let me know through emails.
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
using namespace std;
void openFile (ifstream &inputStream);
int initArray (int *a, int size);
void testArray (const char *name, const int *array, int SIZE,
const int *x, const int *y, int count);
int LoadData (istream &infile, int array[], int arraySize, int &count);
void List (const int array[], int count);
void CopyArray (int destArray[], const int srcArray[], int count);
void RotateLeft (int array[], int count);
void RotateRight (int array[], int count);
void Reverse (int array[], int count);
const int SEED = -42;
int main() {
const int ARRAY_SIZE = 1000, X_SIZE = 20;
int array[ARRAY_SIZE];
int *x = array + 100, *y = array + 300, count, status;
cout << "PGM803_YOUR LAST NAME" << endl;
ifstream fin;
openFile (fin);
int seed = initArray (array, ARRAY_SIZE);
status = LoadData (fin, x, X_SIZE, count);
cout << "\nstatus = " << status << " count = " << count << "\n\n";
cout << "load ";
List (x, count);
if (count > X_SIZE) {
cout << "\n\a***** ERROR: "
<< "loadData() returned a count greater than the array size."
<< " *****\n";
return 1;
}
//testArray ("loadData() ", array, ARRAY_SIZE, x, y, count);
CopyArray (y, x, count);
cout << "copyArray: ";
List (y, count);
//testArray ("copyArray() ", array, ARRAY_SIZE, x, y, count);
RotateLeft (x, count);
cout << "left: ";
List (x, count );
//testArray ("rotateLeft()", array, ARRAY_SIZE, x, y, count);
CopyArray (x, y, count);
RotateRight (x, count);
cout << "right: ";
List (x, count);
//testArray ("rotateRight()", array, ARRAY_SIZE, x, y, count);
CopyArray (x, y, count);
Reverse (x, count);
cout << "reverse: ";
List (x, count);
// testArray ("reverse() ", array, ARRAY_SIZE, x, y, count);
return 0;
}
int initArray (int *a, int size) {
for (int *limit = a + size; a < limit; a++)
*a = SEED;
return SEED;
}
void skipSegment (const int *array, const int *limit) {
for (; array < limit; array++)
rand();
}
void testSegment (const char *name, const int *array, const int *limit,
const int *x ) {
int error = 0;
for (; array < limit; array++) {
if (*array != -rand() ) {
error++;
cout << "\aERROR: " << name << " function overflowed array"
<< " subscript = " << (array - x)
<< " contents = " << *array << '\n';
}
if( error >= 5 ) exit( 1 );
}
if( error > 0 ) exit( 1 );
}
void testArray (const char *name, const int *array, int SIZE,
const int *x, const int *y, int count) {
testSegment (name, array, x, x);
skipSegment (x, x + count);
testSegment (name, x+count, y, x);
skipSegment (y, y + count);
testSegment (name, y+count, array+SIZE, y);
}
void openFile (ifstream &inputStream) {
char filename[_MAX_PATH];
cout << "\nEnter name of disk input file: ";
cin >> filename;
inputStream.open( filename );
if( !inputStream ) {
cerr << "\nCould not open input file " << filename << '\n' << endl;
exit (EXIT_FAILURE);
}
}
//################
int LoadData (istream &infile, int array[],
int arraySize, int &count)
{
int index = 0;
int status;
while (index < arraySize && infile >> array[index] )
{
index++;
if ( infile.eof() )
{
status = 0;
break;
}
if( index == arraySize && infile >> ws && infile.good() )
{ //Too many data on the input file
status = 1;
break;
}
if( !infile.eof() && infile.fail() )
{ //Bad input data
status = -1;
break;
}
infile >> array[index];
}
count = index;
return status;
}
void List (const int array[], int count)
{
for ( int i = 0; i < 20; i++){
while(i < count){
cout << setw(3) << array[i];
if( i + 1 == 20)
cout << "\n";
}
}
cout << "\n";
}
void CopyArray (int destArray[], const int srcArray[], int count)
{
for (int index = 0; index < count; index++)
{
destArray[index] = srcArray[index];
cout << setw(3) << destArray[index];
if( index + 1 == 20)
cout << "\n";
}
cout << "\n";
}
void RotateLeft (int array[], int count)
{
array[0] = array[count];
cout << setw(3) << array[0];
for (int i = 1; i < count; i++)
{
array[i] = array[i-1];
cout << setw(3) << array[i];
if( i + 1 == 20)
cout << "\n";
}
cout << "\n";
}
void RotateRight (int array[], int count)
{
array[count] = array[0];
for (int i = 1; i < count; i++)
{
array[i] = array[i+1];
}
for (int i = 0; i < count; i++)
{
cout << setw(3) << array[i];
if( i + 1 == 20)
cout << "\n";
}
cout << "\n";
}
void Reverse (int array[], int count)
{
for (int i = 0; i < 9; i++)
{
int k = 8;
array[i] = array[i + k];
k = k -2;
cout << setw(3) << array[i];
if( i + 1 == 20)
cout << "\n";
}
cout << "\n";
}
|