Skip to content

Instantly share code, notes, and snippets.

@ereidland
Created March 11, 2013 20:38
Show Gist options
  • Save ereidland/5137527 to your computer and use it in GitHub Desktop.
Save ereidland/5137527 to your computer and use it in GitHub Desktop.
Assignment 4 at Library
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
//See function bodies for comments on these.
int * createArray(int & size);
void findStats(int * arr, int size, int & min, int & max, double & average);
int * searchElement(int * arr, int size, int * element);
int main()
{
int option = 0, //Selected option.
*arr = 0, //Int pointer to first element in array
*res = 0, //Result of search. Pointer to lone int.
arrSize = 0, //size of array.
min = 0, //Min value in array.
max = 0, //Max value in array.
target = 0; //Target element to find.
double
average = 0; //Average value in array.
//Set all the things to 0.
while(true) //Infinite loop (broken in the case block at option 4).
{
cout << "Enter a choice: ";
cin >> option; //Read in option.
switch(option)
{
case 1:
if (arr) // If arr is not NULL.
delete [] arr; //Delete the arr. Note the [] on the delete. If it is not shown there,
//the program thinks it is only deleting a single pointer to an int.
arr = createArray(arrSize); //Create an array, passing arrSize so we can get the size back.
cout << "Array: ";
for (int i = 0; i < arrSize; i++) //Loop from to to < arrSize
cout
<< setw(4) //Set character width to 4.
<< arr[i]; //Output arr at i
cout << endl;
break;
case 2:
if (!arr) //If array is not set, hate on the user.
{
cout << "Array is not set.";
break; //Break out of case.
}
findStats(arr, arrSize, min, max, average); //Find stats for array and get them back through the reference varibles.
cout << "Array stats: " << endl //Let them know what's coming.
<< "Min: " << setw(10) << min << endl //Print min
<< "Max: " << setw(10) << max << endl //Print max
<< "Average: " << setw(10) << average << endl;//Print average
break;
case 3:
if (!arr) //If array isnot set, hate on the user.
{
cout << "Array is not set.";
break; //Break out of case.
}
cout << "Select a number to find: ";
cin >> target; //Note that this is reading into the dereferenced pointer to the target.
res = searchElement(arr, arrSize, &target);
if (res) //If res is not 0
cout << "Number found: " << *res << endl; //Print dereferenced number.
else
cout << "Number not found. :(" << endl; //Let the user know our lament.
break;
case 4:
cout << "Exiting" << endl;
if (arr) //If array exists
delete[] arr; //Delete it. Note the [].
return 0; //Return - out of main an the program exits.
default:
cout << "Unknown option: " << option << endl;
}
}
return 0; //Result to be sent back to the OS once this is complete.
}
int * createArray(int & size) //Create array.
{
cout << "Enter a size: ";
cin >> size; //Read in size. Note that this goes into the referenced variable.
if (size > 0) //Size must be > 0 for a valid array.
{
int * res = new int[size]; //Declare res as new array of size "size".
for (int i = 0; i < size; i++)
{
res[i] = 1 + rand() % 100; //The rand function returns a positive integer within a very large range.
//Control the range by using the % operator (remainder), so that
//In case (large % small), the result will be up to a maximum of (small - 1)
}
return res; //Return the result.
}
else
{
cout << "Bad size: " << size; //Let the user know they are naughty and entered an invalid size.
return 0; //NULL pointer.
}
}
void findStats(int * arr, int size, int & min, int & max, double & average) //Calculate stats for array.
{
if (size <= 0) //If size is <= 0.
{
cout << "Size must be > 0 to calculate stats." << endl; //Let them know how awful they are.
return; //Return so that we exit the function.
}
average = 0; //Set average to 0.
//min and max don't need to be reset here because they are set to the first element in the loop.
for (int i = 0; i < size; i++) //Loop from 0 to < size.
{
if ((i == 0) || arr[i] < min) //If i is 0 or if arr at i is < min.
min = arr[i]; //Set min to arr[i]
if ((i == 0) || arr[i] > max) //If i is 0 set or if arr at i is > max.
max = arr[i]; //Set max to arr[i].
average += arr[i]; //Increment average by arr at i.
}
average /= size; //Divide average (really just the sum at this point) by the size to get the actual average.
//Note that it will automatically cast up from an int to a double, because the compiler goes with
//The highest precision type.
}
int * searchElement(int * arr, int size, int * element) //Find element with *element value.
{
if (size <= 0) //No good. Size too small.
{
cout << "Size must be > 0 to search." << endl; //Let them know they are horrible.
return 0; //Return 0 (failure) so we exit the function.
}
for (int i = 0; i < size; i++) //Loop from 0 to < size
if (arr[i] == *element) //If array at i is equal to the dereferenced element.
return (arr + i); //Equal to: &arr[i]; Returning the pointer to the element at i.
return 0; //Return 0 (failure).
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment