Code:
/* This program will prompt the user for a seed value, a lower bound value,
an upper bound value, and a quantity of pseudorandom numbers to generate.
If an input doesn't follow the criteria specified, an error message appears.
While generating the pseudorandom numbers it counts the number of even
numbers, the number of odd numbers, the number of negatives, the number of
positives, and the number of times 0 occurs. Finally it displays the
original user inputs and the counts.
******************************************************************************/
/* preprocessing directives */
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
// Function Declarations
void displayName();
bool getInput(int* pSeed, int* pLb, int* pUb, int* pNumToGen);
bool getSeed(int* pSeed);
bool getBounds(int* pLb, int* pUb);
bool getNumToGen(int* pNumToGen);
void genRandom(int seed, int lb, int ub, int numToGen,
int* pEven, int* pOdd, int* pNeg, int* pPos, int* pZero);
void addToCounts(int randNum, int* pEven, int* pOdd,
int* pNeg, int* pPos, int* pZero);
void printResults(int seed, int lb, int ub, int numToGen,
int even, int odd, int neg, int pos, int zero);
// function main () calls displayName() then getInput(). Depending on the
// values inputted by the user, main() will decide whether to end the program
// or continue with the other functions.
int main (void)
{
bool testInput;
int aSeed, aLb, aUb, aNumToGen;
int aEven, aOdd, aNeg, aPos, aZero;
displayName ();
getInput(&aSeed, &aLb, &aUb, &aNumToGen);
return 0;
}
// function displayName() displays the programmer's name
void displayName()
{
printf("\nThomas Ciocco\n");
return;
}
bool getInput(int* pSeed, int* pLb, int* pUb, int* pNumToGen)
{
getSeed(pSeed);
if ((getSeed(pSeed)) == true){
getBounds(pLb, pUb);
return true;
}
getNumToGen(pNumToGen);
return false;
}
// function getSeed() prompts the user for a nonnegative integer
// and tests whether the input is valid. If it is valid, the bool is set
// to true, if not, bool is set to false. Finally it returns the bool value
// and sends the input as an output parameter.
bool getSeed(int* pSeed)
{
printf("Please enter a non-negative integer value: \n");
int seedResult = scanf("%d", &pSeed);
if(seedResult != 1){
printf("You have entered invalid data");
return false;
}
while (seedResult == 1){
if(pSeed >= 0){
return true;
}
else{
printf("You have entered an invalid integer");
return false;
}
}
}