152 lines
3.7 KiB
C
152 lines
3.7 KiB
C
// header files
|
|
#include <time.h> // for seeding srand
|
|
#include <stdlib.h> // for srand, rand
|
|
#include "Console_IO_Utility.h"
|
|
#include "File_Output_Utility.h"
|
|
#include "File_Input_Utility.h"
|
|
|
|
// global constants
|
|
const int MIN_RAND_LIMIT = 0;
|
|
const int MAX_RAND_LIMIT = 100;
|
|
const int MIN_ARRAY_SIZE = 1;
|
|
const int MAX_ARRAY_SIZE = 10;
|
|
|
|
// function prototypes
|
|
void displayResults(int arraySize, int *arrayVal);
|
|
int getRandBetween(int lowLimitValue, int highLimitValue);
|
|
void printProgramTitle();
|
|
|
|
int main( int argv, char *argc[] )
|
|
{
|
|
// initialize variables
|
|
int arraySizeValue, arrayVal[MAX_ARRAY_SIZE];
|
|
int iteration = 0;
|
|
// Display title
|
|
//function: printProgramTitle
|
|
printProgramTitle();
|
|
// get user input
|
|
// functions: scanf
|
|
// ask for array size
|
|
arraySizeValue = promptForInteger("Enter array size to fill in with random values: \n");
|
|
|
|
|
|
// do calculations
|
|
srand(time(NULL));
|
|
// need to check if the array size is less than 0, default to min size.
|
|
if (arraySizeValue <= 0)
|
|
{
|
|
int arrayVal[MIN_RAND_LIMIT];
|
|
arraySizeValue = 1;
|
|
}
|
|
else if (arraySizeValue > 10)
|
|
{
|
|
int arrayVal[arraySizeValue];
|
|
arraySizeValue = 10;
|
|
}
|
|
|
|
//fill array with random values
|
|
|
|
for (; iteration <= arraySizeValue; iteration++ )
|
|
{
|
|
arrayVal[iteration] = getRandBetween(MIN_RAND_LIMIT, MAX_RAND_LIMIT);
|
|
}
|
|
|
|
//display displayResults
|
|
//function: displayResults
|
|
displayResults(arraySizeValue, arrayVal);
|
|
|
|
// return results/end program
|
|
printf("\n\nEnd Program\n");
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
Name: displayResults
|
|
Process: displays an array of randomly generated values
|
|
uses pipes to distinguish each array cell
|
|
Function Input/Parameters: array size (int)
|
|
array of values (int *)
|
|
Function Output/Parameters: none
|
|
Function Output/Returned: none
|
|
Device Input/Keyboard: none
|
|
Device Output/Monitor: displays items as specified
|
|
Dependencies: printf
|
|
*/
|
|
void displayResults(int arraySize, int *arrayVal)
|
|
{
|
|
// initialize variables
|
|
//int iteration = 0;
|
|
//display array info
|
|
//function: printString
|
|
printString("Randomly generated array: \n");
|
|
|
|
|
|
printf("|");
|
|
// do calculations
|
|
for (int iteration = 0 ; iteration < arraySize; iteration++ )
|
|
{
|
|
printf(" %d |", arrayVal[iteration]);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Name: getRandBetween
|
|
Process: finds random value between two given parameters, inclusive,
|
|
and returns
|
|
Function Input/Parameters: low limit value (int), high limit value (int)
|
|
Function Output/Parameters: none
|
|
Function Output/Returned: random value as specified (int)
|
|
Device Input/Keyboard: none
|
|
Device Output/Monitor: none
|
|
Dependencies: rand
|
|
*/
|
|
int getRandBetween(int lowLimitValue, int highLimitValue)
|
|
{
|
|
// initialize variables
|
|
int range;
|
|
lowLimitValue = MIN_RAND_LIMIT;
|
|
highLimitValue = MAX_RAND_LIMIT;
|
|
|
|
//Calculate range
|
|
range = highLimitValue - lowLimitValue + 1;
|
|
|
|
// do calculations
|
|
return rand() % range + lowLimitValue;
|
|
|
|
|
|
// display results
|
|
|
|
// return value
|
|
}
|
|
|
|
/*
|
|
Name: printProgramTitle
|
|
Process: displays program title
|
|
Function Input/Parameters: none
|
|
Function Output/Parameters: none
|
|
Function Output/Returned: none
|
|
Device Input/Keyboard: none
|
|
Device Output/Monitor: title displayed as specified
|
|
Dependencies: Console IO Utilities tools
|
|
*/
|
|
void printProgramTitle()
|
|
{
|
|
// initialize variables
|
|
// none
|
|
|
|
// show title
|
|
// functions: printString
|
|
printString("Generate Random Array\n");
|
|
// print divider line
|
|
printCharacters(23, '=');
|
|
// print endlines
|
|
printEndlines(2);
|
|
|
|
// get user input
|
|
// none
|
|
// do calculations
|
|
// none
|
|
// display results
|
|
// none
|
|
}
|