-
Troubleshooting Please
Just trying to get this code to get a seed, upper and lower bounds, the number of random numbers to generate so far. having a few bugs, any help would be much appreciated.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
void myName(void);
bool getInput(int* jSeed, int* jLowBnd, int* jHiBnd, int* jGenNumb);
bool getSeed(int* jSeed);
bool getBounds(int* jLowBnd, int* jHiBnd);
bool getGenNum(int* jGenNumb);
void genRandom(int seed, int LowBnd, int HiBnd, int GenNumb,
int* jEven, int* jOdd, int* jNeg, int* jPos, int* jZero);
void add2Counts(int randNum, int* jEven, int* jOdd,
int* jNeg, int* jPos, int* jZero);
void printResults(int Seed, int LowBnd, int HiBnd, int GenNumb, int Even,
int Odd, int Neg, int Pos, int Zero);
int main(void)
{
int jSeed, jLowBnd, jHiBnd, jGenNumb;
int jEven,jOdd, jNeg, jPos, jZero;
int Seed, LowBnd, HiBnd, GenNumb, Even, Odd;
int Pos, Neg, Zero, randNum;
myName();
getInput(&jSeed, &jLowBnd, &jHiBnd, &jGenNumb);
genRandom(Seed, LowBnd, HiBnd, GenNumb, &jEven,
&jOdd, &jNeg, &jPos, &jZero);
add2Counts(randNum, &jEven, &jOdd, &jNeg, &jPos, &jZero);
printResults(Seed, LowBnd, HiBnd, GenNumb, Even, Odd, Neg, Pos, Zero);
return 0;
}
void myName(void)
{
printf("\n**************************************** *******************");
printf("\n**************************************** *******************");
printf("\n** Joshua Yount **");
printf("\n** and **");
printf("\n** John King **");
printf("\n**************************************** *******************");
printf("\n**************************************** *******************\n");
printf("\n\n");
return;
}
bool getInput(int* jSeed, int* jLowBnd, int* jHiBnd, int* jGenNumb)
{
if(getSeed(jSeed)) printf("\nSuccess!\n");
else printf("There was an error passing you seed value");
if(getBounds(jLowBnd, jHiBnd)) printf("\nSuccess!\n");
else printf("\nThere was an error passing the upper and lower bounds.\n");
if(getGenNum(jGenNumb)) printf("\nSuccess!\n");
else printf("There was an error passing the number of random numbers.\n");
return;
}
bool getSeed(int* jSeed)
{
int seed;
*jSeed = seed;
printf("\nPlease enter a nonnegative seed value: ");
scanf("%d", jSeed);
if(seed < 0)
printf("%d is a negative number, please only enter positive numbers.", seed);
else;
return;
}
bool getBounds(int* jLowBnd, int* jHiBnd)
{
int lowbnd, hibnd;
*jLowBnd = lowbnd;
*jHiBnd = hibnd;
printf("\nPlease enter a lower bound number inbetween [-25,-1]");
printf("and an upper bound number inbetween [1,25]");
printf("seperated by a space");
printf("\n");
scanf("%d %d", jLowBnd, jHiBnd);
if(-25 < lowbnd < -1 && 1 < hibnd < 25)
{
if(-25 > lowbnd > -1 && 1 < hibnd < 25)
printf("Your lower bound number is outside of range [-25,-1].");
else if(-25 < lowbnd < -1 && 1 > hibnd > 25)
printf("Your upper bound number is outside the range [1,25]");
else if(-25 > lowbnd > -1 && 1 > hibnd > 25)
printf("Both you entered values are outside the range [-25,-1] & [1,25]");
}
else;
return;
}
bool getGenNum(int* jGenNumb)
{
int gennumb;
printf("\nPlease enter a number of random numbers to generate");
printf("inbetween [10,50]:");
printf("\n");
scanf("%d", jGenNumb);
gennumb = *jGenNumb;
if(10 < gennumb < 50)
{
if(10 > gennumb)
printf("The number you entered is less than 10:");
else if(gennumb > 50)
printf("The number you entered is greater than 50:");
}
else;
return ;
}
void genRandom(int seed, int LowBnd, int HiBnd, int GenNumb,
int* pEven, int* pOdd, int* pNeg, int* pPos, int* pZero)
{
int i;
int randNum;
srand((unsigned int)seed);
for(i = 0; i < GenNumb; i++)
{
randNum = rand() % (LowBnd - HiBnd);
printf("\n%d: %d", i, randNum);
}
printf("\n\n");
return;
}
void add2Counts(int randNum, int* pEven, int* pOdd,
int* pNeg, int* pPos, int* pZero)
{
}
void printResults(int seed, int LowBnd, int HiBnd, int GenNumb, int even,
int odd, int neg, int pos, int zero)
{
}
-
It's hard to help if I don't know what errors you're getting.
-
ummm some of the tests aren't correctly working, and it is just saying success for the random number generator. Its not generating the numbers.
-
http://cboard.cprogramming.com/cplus...ead-first.html
Also beware of these kinds of expressions:
if (10 < gennumb < 50)
While it is correct to write so in math, it is not in C.
Basically, it evaluates to
if ((10 < gennumb) < 50)
And (10 < gennumb) will evaluate to a boolean (true or false; basically 1 or 0), and then that will be compared to < 50, so you'll see you will get the wrong result.
You need to be explicit. If you want gennumb to be bigger than 10 and lesser than 50, then you must write
if (gennumb > 10 && gennumb < 50)