// MA 4990-01 Special Topics in Probability // Instructor: Dr. Zhijun Liu // Program Author: Reggie Warren #include #include #include #include #include // function prototype // int getVals(int numVals); int main(int argc, char* argv[]) { int *sum = new int[atoi(argv[1])]; int input; int max = 0; int min = 0; int count = 0; ofstream outFile("output.txt"); ifstream inFile; // Make sure that the number of trials and the sequence size are passed // as command line arguments // if(argc != 3) { cerr << "Invalid number of arguments--please enter at least two" << endl; exit(1); } // Loop through calling the procedure that will generate the sum of the // random numbers a number of times equal to the command-line specified // value. Write the sums to a file. // for(int i = 0; i < atoi(argv[1]); i++) { sum[i] = getVals(atoi(argv[2])); outFile << sum[i] << endl; } outFile << endl; for(int i = 0; i < atoi(argv[1]); i++) { // figure out range of values--expensive for large 'sum' arrays // if(sum[i] > max) max = sum[i]; if(sum[i] < min) min = sum[i]; // find out number of times a particular sum occurs // if(i > 0) { // check for matching value--rather expensive for large 'sum' arrays // for(int j = atoi(argv[1]); j > 0; j--) { if(sum[i] == sum[j]) count++; } outFile << "Value " << sum[i] << " occurs " << count << " times." << endl; // reset the count variable // count = 0; } // end if statement } // end for loop outFile << endl << "Range: [" << min << ',' << max << ']' << endl; // Take care of possible memory leaks and dangling pointers // delete [] sum; sum = NULL; } // end main int getVals(int numVals) { int* intArray = new int[numVals]; int sum = 0; // fake randomness--rand() generates numbers between 0 and 2^15 - 1, // if rand() returns a value greater than the value corresponding to // half this range, the corresponding array element equals 1; else, // it equals zero // for(int i = 0; i < numVals; i++) { if(rand() > 16384) intArray[i] = 1; else intArray[i] = -1; // calculate sum // sum += intArray[i]; // cout << sum << " " << intArray[i] << endl; } return sum; }