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* );
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);
if ((getInput(&aSeed, &aLb, &aUb, &aNumToGen))== true)
printf("Thank you");
else
printf("\nTry again later\n");
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);
else
return false;
if ((getBounds(pLb, pUb)) == true)
getNumToGen(pNumToGen);
else
return false;
if ((getNumToGen(pNumToGen)) == true)
return true;
else
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;
}
if (seedResult == 1){
if(*pSeed >= 0)
return true;
else{
printf("You have entered an invalid integer");
return false;
}
}
}
// function getBound () prompts the user for a lower bound and an upper bound
// integer. Next, it will test whether the inputs are valid and set the bool
// to true if it is, false if not. The integers are sent as output parameter,
// and the bool value is returned.
bool getBounds(int* pLb, int* pUb)
{
printf("Please enter a lower bound [-25,-1] and an upper bound [1,25]: ");
int boundsResult = scanf("%d %d", pLb, pUb);
if (boundsResult != 2)
printf("You have entered invalid data");
if (boundsResult == 2){
if (!(*pLb >= -25 && *pLb <= -1)){
printf("%d is not in range [-25,-1]", *pLb);
return false;
}
if (!(*pUb >= 1 && *pUb <= 25)){
printf("%d is not in range [1,25]", *pUb);
return false;
}
}
else
return true;
return false;
}
// function getNumToGen() prompts the user for a non-negative integer to set
// as the quanitity of pseudorandom numbers to generate. Next, the function
// tests whether the input is valid or not. If valid, bool value is returned
// true; if invalid, bool set to false. Input sent as output parameter.
bool getNumToGen(int* pNumToGen)
{
printf("Please enter the number of randoms to generate [10,50]:");
int numToGenResult = scanf("%d", pNumToGen);
if (numToGenResult != 1){
printf("You have entered invalid data");
return false;
}
if (numToGenResult == 1){
if (!(*pNumToGen >= 10 && *pNumToGen <= 50)){
printf("%d is not in range [10,50]", *pNumToGen);
return false;
}
}
else
return true;
}