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
|
#include <fstream>
using std::ofstream;
using std::endl;
#include <iostream>
using std::cout;
#include <stdlib.h> //for rand()
#include "maxsubarray.h"
using namespace myName;
void randomFill(long* A, unsigned long size);
void printArray(long* A, unsigned long size);
//const bool smalltest = true;
const bool smalltest = false;
int main(void)
{
srand(1); //after you think things are working, you can change this to run different tests.
unsigned long n;
n = 10;
long* A = new long[n];
msaAnswer rslt;
//,rslttest,rsltlinear;
unsigned long i;
for(i=0; i<3; i++)
{
::randomFill(A,n);
myName::findMaxSubarray(A,0,n-1,rslt);
//myName::findMaxSubarray_linear(A,0,n-1,rsltlinear);
//myName::findMaxSubarraySlow(A,0,n-1,rslttest);
printArray(A,n);
cout << rslt << endl;
//rsltlinear << rslttest << endl <<
}
if(smalltest) return 0;
//else, run bigger tests and write results to a file.
std::ofstream fout;
fout.open("output"); //should overwrite whatever file is currently there.
delete[] A;
n = 100;
A = new long[n];
for(i=0; i<10; i++)
{
::randomFill(A,n);
myName::findMaxSubarray(A,0,n-1,rslt);
//myName::findMaxSubarray_linear(A,0,n-1,rsltlinear);
//myName::findMaxSubarraySlow(A,0,n-1,rslttest);
fout << rslt << " @\n";
//<< rsltlinear << rslttest
}
fout.close();
return 0;
}
void randomFill(long* A, unsigned long size)
{
for(unsigned long i=0; i<size; i++)
A[i] = (::rand()%myName::maxrand) - (myName::maxrand / 2);
}
void printArray(long* A, unsigned long size)
{
for(unsigned long i=0; i<size; i++)
cout << A[i] << " ";
cout << endl;
}
|