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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
int maxSubsequenceSumSlow (int [], int, int &, int &);
int maxSubsequenceSumFast (int [], int, int &, int &);
// ****************************************************************
int main(int argc, char *argv[])
{
enum algorithmOption {SLOW, FAST, PRINT, TEST, NONE};
string stars;
stars.append(60,'*');
stringstream ss;
int ans1, start1, stop1;
int ans2, start2, stop2;
int length = 0;
int arrTest[] = {-2, 11, -4, 13, -5, 2};
int lenTest = sizeof(arrTest) / sizeof(arrTest[0]);
algorithmOption userChoice = NONE;
// ------------------------------
// Verify command line arguments.
// Error out if bad, select user selection if good.
if (argc == 1) {
cout << "Usage: maxSeq <-t|-s|-f|-p> -l <length>" << endl;
return 0;
}
if (argc < 2 || argc > 4 || argc == 3) {
cout << "Error, invalid command line options." << endl;
return 0;
}
if (string(argv[1]) == "-t")
userChoice = TEST;
if (string(argv[1]) == "-s")
userChoice = SLOW;
if (string(argv[1]) == "-f")
userChoice = FAST;
if (string(argv[1]) == "-p")
userChoice = PRINT;
if (userChoice == NONE) {
cout << "Error, invalid command line options." << endl;
return 0;
}
if (userChoice != TEST) {
if (argc == 2) {
cout << "Error, must provide length." << endl;
return 0;
}
if (string(argv[2]) != "-l") {
cout << "Error, invalid length specifier." << endl;
return 0;
}
if (string(argv[3]) != "") {
ss << argv[3];
ss >> length;
if (length < 5 || length > 1000000) {
cout << "Error, invalid length." << endl;
return 0;
}
}
}
// ------------------------------
// Command line args ok, display initial headers.
cout << stars << endl;
cout << "CS 302 - Assignment #1" << endl;
cout << "Maximum Contiguous Subsequence Sum Problem." << endl;
cout << endl << endl;
// ------------------------------
// Generate random values.
// dynamically creates array of required size.
int * myArr = new int[length];
for (int i=0; i<length; i++)
myArr[i] = (rand() % 1000) - 500;
// ------------------------------
// Select appropriate option.
switch (userChoice) {
case PRINT:
if (length < 1000) {
for (int i=0; i<length; i++)
cout << myArr[i] << " ";
cout << endl;
cout << endl;
} else {
cout << "Umm, I don't think so..." << endl;
}
break;
case TEST:
ans1 = maxSubsequenceSumSlow(arrTest, lenTest, start1, stop1);
cout << endl;
cout << "Max Sum 1 = " << ans1 << endl;
cout << "Start 1 = " << start1 <<
" Stop 1 = " << stop1 << endl;
cout << "Sequence 1 = ";
for (int i=start1; i<=stop1; i++)
cout << arrTest[i] << " ";
cout << endl;
ans2 = maxSubsequenceSumFast(arrTest, lenTest, start2, stop2);
cout << endl;
cout << "Max Sum 2 = " << ans2 << endl;
cout << "Start 2 = " << start2 <<
" Stop 2 = " << stop2 << endl;
cout << "Sequence 2 = ";
for (int i=start2; i<=stop2; i++)
cout << arrTest[i] << " ";
cout << endl;
break;
case SLOW:
ans1 = maxSubsequenceSumSlow(myArr, length,
start1, stop1);
cout << endl;
cout << "Max Sum = " << ans1 << endl;
cout << "Start = " << start1 <<
" Stop = " << stop1 << endl;
cout << "Sequence = ";
for (int i=start1; i<=stop1; i++)
cout << myArr[i] << " ";
cout << endl;
break;
case FAST:
ans1 = maxSubsequenceSumFast(myArr, length,
start1, stop1);
cout << endl;
cout << "Max Sum = " << ans1 << endl;
cout << "Start = " << start1 <<
" Stop = " << stop1 << endl;
cout << "Sequence = ";
for (int i=start1; i<=stop1; i++)
cout << myArr[i] << " ";
cout << endl;
break;
case NONE:
break;
}
// ------------------------------
// Done, terminate program.
cout << endl;
cout << "Game over, thanks for playing." << endl;
}
// ****************************************************************
// Cubic maximum contiguous subsequence sum algorithm.
// Assignment algorithm 1
// seqstart and seqEnd represent the actual best sequence.
int maxSubsequenceSumFast(int myArr[], int length, int& start, int& stop)
{
int sum = 0;
int max = 0;
for(int i=0; i < length; i++)
for(int j=i; j < length; j++)
{
sum = 0;
for(int k=i; k <= j; k++)
sum += myArr[k];
if(sum > max)
{
max = sum;
start = i;
stop = j;
}
}
return max;
}
// ****************************************************************
// Linear maximum contiguous subsequence sum algorithm.
// Assignment algorithm 2
// seqStart and seqEnd represent the actual best sequence.
int maxSubsequenceSumSlow(int myArr[], int length, int& start, int& stop)
{
int max = 0;
int sum = 0;
int temp = 0;
for(int j=0; j < length; j++)
{
sum += myArr[j];
if(sum < 0)
{
sum = 0;
temp = j + 1;
}
if(max < sum)
{
max = sum;
start = temp;
stop = j;
}
else if(max < 0)
max = 0;
}
return max;
}
|