Created
July 21, 2014 14:15
-
-
Save rnelson/e1349851e08aa1e66daf to your computer and use it in GitHub Desktop.
CSC 250 Assignment 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ross Nelson | |
// Assignment 4 | |
// nelsonr_assgn4.cpp | |
// Assigned: 11 Feb 2004 | |
// Due: 18 Feb 2004 | |
// | |
// assgn4 dynamically allocates an array, reads values from | |
// a file, and finds the mode and median of the values | |
// | |
// Return Values: | |
// 0 Successful Execution | |
// -1 Can't Open File | |
// includes: iostream gives cout; cstddef gives NULL; | |
// and fstream gives file i/o. the namespace being | |
// used is std (standard) | |
#include <iostream> | |
#include <cstddef> | |
#include <fstream> | |
#include <iomanip> | |
using namespace std; | |
// debugging is a great way to see what's going on. however, | |
// I don't remember everything I've learned about gdb and the | |
// one in Xcode just slows me down. therefore, I like to use | |
// #ifdef's and do some nice cout statements and resort to gdb | |
// only when I get a crash | |
#ifndef RN_ASSGN4_DEBUG | |
//#define RN_ASSGN4_DEBUG | |
#endif | |
// the description and basic usage information is described | |
// in comments around each function | |
void sortList(int *array, const int *length); | |
void swap(int *nOne, int *nTwo); | |
double findAverage(int *array, const int *length); | |
int findMode(int *array, const int *length); | |
double findMedian(int *array, const int *length); | |
//****************************************************************************** | |
int main() | |
// main() is the main part of the program | |
// Precondition: binary application must be called | |
// Postcondition: program returns 0 upon successful execution, other value if | |
// execution failed | |
{ | |
// before I even start, I want to print out a warning about leakage | |
cout << "It is suggested that you reboot the system you run" << endl | |
<< "this on after running it. The assignment was to" << endl | |
<< "everything a pointer; aside from what couldn't be" << endl | |
<< "a pointer (functions, RN_ASSGN4_DEBUG) and what I" << endl | |
<< "didn't work work to be used as a pointer (fin)," << endl | |
<< "everything is a pointer. Some things are left out" << endl | |
<< "there after running because the program got to the" << endl | |
<< "return statement before the delete statement.\n " << endl; | |
// return value | |
int *returnPtr; | |
returnPtr = new int; | |
*returnPtr = 0; | |
// ifstream is an input file stream, suitable for reading | |
// the contents of files; "fin" is often used to note that | |
// usage of ifstream is similar to that of cin (iostream) | |
ifstream fin; | |
// ints are integers; inputPtr is a pointer which points to | |
// an unknown (to the user) integer, one which we do not | |
// explicitly name. this value will be used to temporarily | |
// store values in as read from fin | |
int *inputPtr; inputPtr = new int; *inputPtr = 0; | |
// I'd dynamically create temp, but the memory size isn't | |
// worth it | |
int *temp; | |
temp = new int; | |
*temp = 0; | |
// cStyleArray is my character array of size 5, enough for | |
// the filename ("data") and the null terminator ('\0') | |
char *cStyleArray; cStyleArray = new char[5]; | |
// this block of code will specify the filename, putting it | |
// into cStyleArray[] in a boring, long way | |
{ | |
*(cStyleArray + 0) = 'd'; | |
*(cStyleArray + 1) = 'a'; | |
*(cStyleArray + 2) = 't'; | |
*(cStyleArray + 3) = 'a'; | |
*(cStyleArray + 4) = '\0'; | |
} | |
// open() is a function of ifstream which reads a file; in | |
// this program, we're specifying the filename as a C-Style | |
// string (a null-terminated character array) | |
fin.open(cStyleArray); | |
// now that the file's been opened, it's time to free some | |
// memory back up using delete | |
delete [] cStyleArray; | |
// after a ifstream is told to open a file, one should check | |
// to make sure it was actually opened; !fin means there | |
// was an error opening it | |
if (!fin) | |
{ | |
cout << "Error opening file. Program terminating." << endl; | |
return -1; | |
} | |
// now that the file's (hopefully) open and readable, it's time | |
// to see how many items we want to read in | |
fin >> *temp; | |
// as the length is passed to every function, we should have a | |
// variable that does nothing but hold the length of the array | |
// (currently *temp) as the value at &temp will be overwritten | |
int *len; | |
len = new int; | |
*len = *temp; | |
// now that we have the length of the array to be read, we need | |
// an array to put them into. | |
int *list; list = new int[*len]; | |
int *sorted; sorted = new int[*len]; | |
// time to read values! weeee. I was going to put it in a | |
// separate function, but it's just as easy to do it here | |
// than looking up how to best pass an input stream byref | |
// just to run a simple for loop | |
int *counter; counter = new int; *counter = 0; | |
for (*counter = 0; *counter < *len; *counter = *counter + 1) | |
{ | |
fin >> *inputPtr; | |
*(list + *counter) = *inputPtr; | |
#ifdef RN_ASSGN4_DEBUG | |
cout << "list[" << *counter << "] = " << *inputPtr << endl; | |
#endif | |
} | |
// since all data has been read, we can safely close the file | |
// now; it's best to do it now so it's not forgotten | |
fin.close(); | |
// now that that's done, time to copy list[] into sorted[] | |
// for no apparant reason. maybe list[] would have to be | |
// used if this program were ever modified? | |
for (*counter = 0; *counter < *len; *counter = *counter + 1) | |
{ | |
*(sorted + *counter) = *(list + *counter); | |
} | |
// and then sort that list | |
sortList(sorted, len); | |
#ifdef RN_ASSGN4_DEBUG | |
cout << endl; | |
cout << "list sorted" << endl; | |
cout << "---- ------" << endl; | |
for (*temp= 0; *temp < *len; *temp += 1) | |
{ | |
cout << list[*temp]; | |
if (*(list + *temp) < 10) | |
cout << " "; | |
else if (*(list + *temp) < 100) | |
cout << " "; | |
else | |
cout << " "; | |
cout << sorted[*temp] << endl; | |
} | |
#endif | |
// these variables are to store the average, | |
// median, and mode of the values of sorted[] | |
int *mode; mode = new int; *mode = 0; | |
double *median; median = new double; *median = 0; | |
double *average; average = new double; *average = 0; | |
// find values, spit them out | |
*average = findAverage(sorted, len); | |
*median = findMedian(sorted, len); | |
*mode = findMode(sorted, len); | |
cout << "Average: " << setprecision(3) << *average << endl; | |
cout << "Median: " << *median << endl; | |
if (*mode == -1) | |
cout << "No mode." << endl; | |
else | |
cout << setprecision(3) << "Mode: " << *mode << endl; | |
// CLEAN UP YOUR MESS! | |
delete inputPtr; delete temp; delete len; delete [] list; | |
delete [] sorted; delete mode; delete median; delete average; | |
// return the value of returnPtr, which is a constant 0 | |
return *returnPtr; | |
} | |
//****************************************************************************** | |
void sortList(int *array, const int *length) | |
// sortList() bubble sorts array[] | |
// Precondition: array[] is of size *length | |
// Postcondition: array[] is sorted in not-descending order | |
{ | |
// bSwitched is used to let the do{} know if there were | |
// any swaps done; if so, run again | |
bool *bSwitched; bSwitched = new bool; | |
// the do{} is what actually goes through array[] checking | |
// if two elements need to be swapped | |
do | |
{ | |
// i is the counter; bSwitched should be reset to false on | |
// every pass otherwise we'd have a fun infinite loop | |
int *i; i = new int; *i = 0; | |
*bSwitched = false; | |
// this monsterous looking thing runs through the loop from | |
// 0 to (length - 1) times | |
for (*i = 0; *i < *length - 1; *i = *i + 1) | |
{ | |
// if the array[i] is greater than array[i+1], the two | |
// values need to be swapped. swap() does this and then | |
// bSwitched is set to true so the do{} knows that it | |
// should run again | |
if (*(array + *i) > *(array + *i + 1)) | |
{ | |
swap((array + *i), (array + *i + 1)); | |
*bSwitched = true; | |
} | |
} | |
delete i; | |
} while (*bSwitched); | |
// CLEAN UP YOUR MESS! | |
delete bSwitched; | |
// I was going to return a NULL, but I thought there was a problem with my | |
// declaring functions as pointers. thus, I just return nothing. | |
return; | |
} | |
//****************************************************************************** | |
void swap(int *nOne, int *nTwo) | |
// swap() swaps the values of nOne and nTwo | |
// Precondition: nOne, nTwo contain values | |
// Postcondition: nOne = nTwo, nTwo = nOne | |
{ | |
// nTmp is a temporary integer which initially holds the value of nOne. nOne | |
// is then assigned the value of nTwo, and nTwo gets what's in nTmp | |
// (previously nOne's content). the values are thus swapped | |
int nTmp = *nOne; | |
*nOne = *nTwo; | |
*nTwo = nTmp; | |
// I was going to return a NULL, but I thought there was a problem with my | |
// declaring functions as pointers. thus, I just return nothing. | |
return; | |
} | |
//****************************************************************************** | |
double findAverage(int *array, const int *length) | |
// findAverage() finds the average of the the elemnts of array[] | |
// Precondition: array[] is of size *length | |
// Postcondition: function value == average of elements of array[] | |
{ | |
// time for yet another dynamic temp variable! weeeee! | |
double *temp; temp = new double; *temp = 0; | |
int *i; i = new int; *i = 0; | |
// to get the average, we need to loop through the array adding up the values | |
// as we go, then divide by the length | |
for (*i = 0; *i < *length; *i = *i + 1) | |
{ | |
*temp += *(array + *i); | |
} | |
*temp = *temp / *length; | |
// CLEAN UP YOUR MESS! | |
delete i; | |
// now we need to return the "temporary" int that contains our average | |
return *temp; | |
// someone leave the water running? | |
delete temp; | |
} | |
//****************************************************************************** | |
int findMode(int *array, const int *length) | |
// findMode() finds the mode (most occurring) value of array[] | |
// Precondition: array[] is of size *length, array[] is sorted (not-descending) | |
// Postcondition: function value == mode of array[] | |
{ | |
// I'm too tired to document this function (and findMedian() very well, so | |
// when I'm doing new variables, figure it out. it's all the same. | |
int *i; i = new int; *i = 0; int *j; j = new int; *j = 0; | |
// second array counting the number of times each occurrs | |
// loop through to find largest; is answer | |
int *barray; barray = new int[*length]; | |
int *tmp; tmp = new int; *tmp = 0; | |
int *k; k = new int; | |
for (*i = 0; *i < *length; *i += 1) | |
{ | |
*k = *(array + *i); | |
for (*j = 0; *j < *length; *j += 1) | |
{ | |
if (*(array + *j) == *k) | |
*tmp += 1; | |
} | |
*(barray + *i) = *tmp; | |
*tmp = 0; | |
} | |
#ifdef RN_ASSGN4_DEBUG | |
for (*i = 0; *i < *length; *i += 1) | |
cout << "*(barray + " << *i << "): " << *(barray + *i) << endl; | |
#endif | |
int *max; max = new int; *max = *barray; | |
int *maxloc; maxloc = new int; | |
for (*i = 0; *i < *length; *i += 1) | |
{ | |
if (*(barray + *i) > *max) | |
{ | |
*max = *(barray + *i + 1); | |
*maxloc = *i; | |
} | |
} | |
bool *unique; unique = new bool; *unique = true; | |
for (*i = 0; *i < *length; *i += 1) | |
{ | |
if ((*(barray + *i) == *max) && (*(array + *i) != *(array + *maxloc))) | |
*unique = false; | |
} | |
#ifdef RN_ASSGN4_DEBUG | |
cout << "Maximum: " << *max << " at array[" << *maxloc << "] (" | |
<< *(array + *maxloc) << ")" << endl; | |
#endif | |
// CLEAN UP YOUR MESS! | |
delete i; delete [] barray; delete j;delete k; delete tmp; delete max; | |
delete maxloc; delete unique; | |
// now, return the value if it was found more than once; if not, return -1 | |
if (*unique) | |
return *max; | |
else | |
return -1; | |
} | |
//****************************************************************************** | |
double findMedian(int *array, const int *length) | |
// findMedian() finds the median of the the elemnts of array[] | |
// Precondition: array[] is of size *length, array[] is sorted (not-descending) | |
// Postcondition: function value == median of elements of array[] | |
{ | |
// middle is going to point to whatever the median is | |
double *middle; middle = new double; *middle = 0; | |
// is the length an even number? | |
if (*length % 2 == 0) | |
{ | |
double *temp; temp = new double; *temp = 0; | |
*temp = *(array + (*length / 2)); | |
*temp += *(array + (*length / 2 - 1)); | |
*temp /= 2; | |
middle = temp; | |
delete temp; | |
} | |
else | |
{ | |
*middle = *(array + (*length / 2)); | |
} | |
// return whatever the middle values location is | |
return *middle; | |
// hooray for more memory leaks! | |
delete middle; // pointless :/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment