[ARCHIVED THREAD] - C programming help (Page 1 of 2)
Posted: 3/6/2009 8:34:46 AM EDT
|
http://rafb.net/p/Y8lYQ538.html
Alright this has no syntax errors at all. But I dont think the number generator is working correctly. I dont think the number are being generated therefor not going through the counters. Anyone got any advice on making the number generator work? |
|
#include <stdio.h>
#include <stdbool.h> #include <stdlib.h> //Function Declarations void displayName (void); bool getInput (int *seed, int *lb, int *ub, int *numToGen); bool getSeed (int *seed); bool getBounds (int *lb, int *ub); bool getNumToGen (int *numToGen); bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum int *even, int *odd, int *neg, int *pos, int *zero); bool addToCounts (int randNum, int *even, int *odd, int *neg, int *pos, int *zero); void printResults (int seed, int lb, int ub, int numToGen, int randNum, int even, int odd, int neg, int pos, int zero); int main (void) { //Local Declarations int seed, lb, ub, numToGen, randNum; int even = 0; int odd = 0; int neg = 0; int pos = 0; int zero = 0; //Statements displayName (); getInput (&seed, &lb, &ub, &numToGen); genRandom (seed, lb, ub, numToGen, &randNum, &even, &odd, &neg, &pos, &zero); addToCounts (randNum, *even, *odd, *neg, *pos, *zero); printResults (seed, lb, ub, numToGen, randNum, even, odd, neg, pos, zero); return 0; } void displayName (void) { printf("Name: John Doe"); printf("\n\n"); } bool getInput (int *seed, int *lb, int *ub, int *numToGen) { getSeed(seed); getBounds(lb, ub); getNumToGen(numToGen); } bool getSeed (int *seed) { int input; printf("Please enter a nonnegative seed value: "); scanf("%d", &input); if (input < 0) { printf("%d is not nonnegative."); printf("Please try again another day."); } else { *seed = input; } printf("\n\n"); } bool getBounds (int *lb, int *ub) { int input1, input2; printf("Please enter a lower bound [-25,-1] and an upper bound [1,25]: "); scanf("%d %d", &input1, &input2); if (input1 < -25) { printf("Lower bound %d is not in the range [-25,-1]."); printf("Please try again another day."); } else if (input1 > -1) { printf("Lower bound %d is not in the range [-25,-1]."); printf("Please try again another day."); } else { *lb = input1; } if (input2 < 1) { printf("Upper bound %d is not in the range [1,25]."); printf("Please try again another day."); } else if (input2 > 25) { printf("Upper bound %d is not in the range [1,25]."); printf("Please try again another day."); } else { *ub = input2; } printf("\n\n"); } bool getNumToGen (int *numToGen) { int input; printf("Please enter the number of randoms to generate [10,50]: "); scanf("%d", &input); if (input < 10) { printf("%d is not in the range [10,50]."); printf("Please try again another day."); } else if (input > 50) { printf("%d is not in the range [10,50]."); printf("Please try again another day."); } else { *numToGen = input; } printf("\n\n"); } bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum) { int range; int i; srand(seed); range = (ub - lb) + 1; for (i = 10; i < numToGen; i++) { *randNum = lb + rand() % range; } } bool addToCounts(int randNum, int *even, int *odd, int *neg, int *pos, int *zero) { if (randNum < 0) { *neg++; } else if (randNum > 0) { *pos++; } else { *zero++; } if (randNum != 0) { if (randNum % 2 == 0) { *even++; } } else { *odd++; } } void printResults(int seed, int lb, int ub, int numToGen, int randNum, int even, int odd, int neg, int pos, int zero) { printf("\n%15s%15s%15s%15s", "Input Type", "Value", "Count Type", "Value"); printf("\n%15s%15s%15s%15s", "––––––––––", "––––-", "––––––––––", "––––-"); printf("\n%15s%15d%15s%15d", "Seed", seed, "Evens", even); printf("\n%15s%15d%15s%15d", "Lower Bound", lb, "Odds", odd); printf("\n%15s%15d%15s%15d", "Upper Bound", ub, "Negatives", neg); printf("\n%15s%15d%15s%15d", "Number Generated", numToGen, "Positives", pos); printf("\n%15s%15s%15s%15d", "", "", "Zeros", zero); printf("\n\n"); } |
|
Quoted:
Why does genRandom() have 5 parameters in the function declaration but you pass in 10 parameters in the actual call? Also you don't actually do anything with the random number generated in that function. The integer pointer just gets overwritten 10+ times. no it has 10, it's just continued on the next line. yes, the problem seems to be in the getrand funciton. most likely, as with all C and C++ problems, a pointer or reference issue. man I'm glad modern languages do all of that for you. ETA: to the OP, I don't see in the final printf where you're displaying the randNum value. |
|
Sorry I copied and pasted that off notepad++ without fixing the last thing I changed.
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> //Function Declarations void displayName (void); bool getInput (int *seed, int *lb, int *ub, int *numToGen); bool getSeed (int *seed); bool getBounds (int *lb, int *ub); bool getNumToGen (int *numToGen); bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum); bool addToCounts (int randNum, int *even, int *odd, int *neg, int *pos, int *zero); void printResults (int seed, int lb, int ub, int numToGen, int randNum, int even, int odd, int neg, int pos, int zero); int main (void) { //Local Declarations int seed, lb, ub, numToGen, randNum; int even = 0; int odd = 0; int neg = 0; int pos = 0; int zero = 0; //Statements displayName (); getInput (&seed, &lb, &ub, &numToGen); genRandom (seed, lb, ub, numToGen, &randNum); addToCounts (randNum, *even, *odd, *neg, *pos, *zero); printResults (seed, lb, ub, numToGen, randNum, even, odd, neg, pos, zero); return 0; } void displayName (void) { printf("Name: John Doe"); printf("\n\n"); } bool getInput (int *seed, int *lb, int *ub, int *numToGen) { getSeed(seed); getBounds(lb, ub); getNumToGen(numToGen); } bool getSeed (int *seed) { int input; printf("Please enter a nonnegative seed value: "); scanf("%d", &input); if (input < 0) { printf("%d is not nonnegative."); printf("Please try again another day."); } else { *seed = input; } printf("\n\n"); } bool getBounds (int *lb, int *ub) { int input1, input2; printf("Please enter a lower bound [-25,-1] and an upper bound [1,25]: "); scanf("%d %d", &input1, &input2); if (input1 < -25) { printf("Lower bound %d is not in the range [-25,-1]."); printf("Please try again another day."); } else if (input1 > -1) { printf("Lower bound %d is not in the range [-25,-1]."); printf("Please try again another day."); } else { *lb = input1; } if (input2 < 1) { printf("Upper bound %d is not in the range [1,25]."); printf("Please try again another day."); } else if (input2 > 25) { printf("Upper bound %d is not in the range [1,25]."); printf("Please try again another day."); } else { *ub = input2; } printf("\n\n"); } bool getNumToGen (int *numToGen) { int input; printf("Please enter the number of randoms to generate [10,50]: "); scanf("%d", &input); if (input < 10) { printf("%d is not in the range [10,50]."); printf("Please try again another day."); } else if (input > 50) { printf("%d is not in the range [10,50]."); printf("Please try again another day."); } else { *numToGen = input; } printf("\n\n"); } bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum) { int range; int i; srand(seed); range = (ub - lb) + 1; for (i = 10; i < numToGen; i++) { *randNum = lb + rand() % range; } } bool addToCounts(int randNum, int *even, int *odd, int *neg, int *pos, int *zero) { if (randNum < 0) { *neg++; } else if (randNum > 0) { *pos++; } else { *zero++; } if (randNum != 0) { if (randNum % 2 == 0) { *even++; } } else { *odd++; } } void printResults(int seed, int lb, int ub, int numToGen, int randNum, int even, int odd, int neg, int pos, int zero) { printf("\n%15s%15s%15s%15s", "Input Type", "Value", "Count Type", "Value"); printf("\n%15s%15s%15s%15s", "––––––––––", "––––-", "––––––––––", "––––-"); printf("\n%15s%15d%15s%15d", "Seed", seed, "Evens", even); printf("\n%15s%15d%15s%15d", "Lower Bound", lb, "Odds", odd); printf("\n%15s%15d%15s%15d", "Upper Bound", ub, "Negatives", neg); printf("\n%15s%15d%15s%15d", "Number Generated", numToGen, "Positives", pos); printf("\n%15s%15s%15s%15d", "", "", "Zeros", zero); printf("\n\n"); } |
|
this is the output so far
Please enter a nonnegative seed value: 45 Please enter a lower bound [-25,-1] and an upper bound [1,25]: -5 5 Please enter the number of randoms to generate [10,50]: 34 Input Type______Value____Count Type_____Value ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––- Seed________45_______Evens_________0 Lower Bound______-5_______Odds_________0 Upper Bound_______5________Negatives_____0 Number Generated____34_______Positives_______0 ___________________________Zeros__________0 |
|
int main (void)
{ //Local Declarations int seed, lb, ub, numToGen, randNum; int even = 0; int odd = 0; int neg = 0; int pos = 0; int zero = 0; //Statements displayName (); getInput (&seed, &lb, &ub, &numToGen); genRandom (seed, lb, ub, numToGen, &randNum); addToCounts (randNum, *even, *odd, *neg, *pos, *zero); printResults (seed, lb, ub, numToGen, randNum, even, odd, neg, pos, zero); return 0; } Should that RED line be this instead? addToCounts(randNum, &even, &odd, &neg, &pos, &zero); |
|
Quoted:
Hey man, I'm a bit busy at present with work, but post your most current source using rafb.net. Don't clutter up the thread with huge dumps, and ignore the guys who won't click the damn link, for whatever reason. I'll take a look at it, after you've done that. http://rafb.net/p/XiAXPj25.html Thank you |
|
Quoted:
Quoted:
Shouldn't the call to addToCounts() be inside the for-loop inside genRandom()? Otherwise it's only doing the very last number generated. Oh I see what you mean. It needs to count after every loop of the Number generator? If you are wanting it to increment the positive, negative, evens and odds and zeros counters after each random number is generated, then yes it needs to be in the loop. Otherwise the way it is now, it only does the last number generated. |
|
Your compiler is not very strict, for grins (and speed) I pasted your code into a file on my computer and attempted to compile it. Lots of errors.
I suggest you start with addressing these: if a function is declared to return a value, then it should return a value - otherwise declare it as void you are not passing enough arguments to all of your functions - look at all the printf statements where you have format specifiers for arguments that are never passed |
|
First comment
COMMENT YOUR CODE! Second: This formating is bad; it makes it difficult to read... if (randNum < 0) { *neg++; } Do this if (randNum < 0) { *neg++; } You need \n at the end of your printfs to add a newline character. Please clean it up and add comments. I am not sure what you want the code to do so it is kinda hard to figure out what might be wrong... |
|
Quoted:
this guy has a problem with his code and your complaining about text formatting? I bet your a manager Nope, architect... It was a minor point. The main point (first point) is he needs comments. If your going to ask for help at least give the people trying to help you a clue as to what is going on/what the code is supposed to do (inputs, outputs, desired results, etc) :) Example: bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum){
This loop constantly updates the same variable (randNum) multiple times. It is not readily apparent if the + or % has precedence (% does obviously) IS this code supposed to overwrite the value randNum multiple times without it being used? I would assume not but there might be cases where it might be desirable... If it is desired to keep each of the generated values... then you need to dump them into an array... bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum[]) {
But I am not sure if this is what is desired for the function.... (edited to add code) |
|
Quoted:
I also see a problem with your printf's. this is probably why you are not seeing a output. example: printf("%d is not nonnegative."); use google to find the correct usage, you probably intend something like this printf("%d is not nonnegative.", input); AHHHH that is genius, I was wondering why it would throw up 0's |
|
Quoted:
I also see a problem with your printf's. this is probably why you are not seeing a output. example: printf("%d is not nonnegative."); use google to find the correct usage, you probably intend something like this printf("%d is not nonnegative.", input); Alright Finished this, now it prints wrong numbers too |
|
For each function, please add the following comment structure
// This function does X // Inputted values are: // List them, and their possible rages/values // The result of the function is: // The return value is: With this, a person reading your code (including yourself) knows what the function should do, what the inputs are and what the desired output / end results should be. This will also help you. You can write all your program with just function stubs because you have told yourself exactly how the functions will behave, you just need to add the behavior later. Oh, also, if you want to learn some more :) rand() sucks for its psuedorandomness. http://en.wikipedia.org/wiki/Pseudorandom_number_generator I have always liked the Mersenne twister with some of my own bit shifting mods. |
|
Quoted:
Quoted:
this guy has a problem with his code and your complaining about text formatting? I bet your a manager Nope, architect... It was a minor point. The main point (first point) is he needs comments. If your going to ask for help at least give the people trying to help you a clue as to what is going on/what the code is supposed to do (inputs, outputs, desired results, etc) :) Example: bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum){ int range; int i; srand(seed); range = (ub - lb) + 1; for (i = 10; i < numToGen; i++) { *randNum = lb + rand() % range; }}
This loop constantly updates the same variable (randNum) multiple times. It is not readily apparent if the + or % has precedence (% does obviously) IS this code supposed to overwrite the value randNum multiple times without it being used? I would assume not but there might be cases where it might be desirable... If it is desired to keep each of the generated values... then you need to dump them into an array... bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum[]) { int range; int i; srand(seed); range = (ub - lb) + 1; for (i = 10; i < numToGen; i++) { *randNum[i-10] = (rand() % range) + lb; }}
But I am not sure if this is what is desired for the function.... (edited to add code) Only problem is I cant use arrays. Has to be Boolean loops |
|
Quoted:
Ok, in the function bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum[]) What are you trying to do? Trying to seed a number(seed), have the upper and lower bounds(ub and lb) within which the generator should be generate numbers, and how many randoms to generate(numToGen). And I need to print the randoms in 5 rows and 10 columns |
|
I made some changes for you
look at this, look what i changed. You should be able to fix up whatever else is wrong from here http://rafb.net/p/SSUvOP53.html edited to add output from the modified code D:\temp\test\Debug>test Name: John Doe Please enter a nonnegative seed value: 45 Please enter a lower bound [-25,-1] and an upper bound [1,25]: -15 15 Please enter the number of randoms to generate [10,50]: 10 Input Type Value Count Type Value ûûûûûûûûûû ûûûû- ûûûûûûûûûû ûûûû- Seed 45 Evens 2 Lower Bound -15 Odds 7 Upper Bound 15 Negatives 5 Number Generated 10 Positives 4 Zeros 1 |
|
Quoted:
I made some changes for you look at this, look what i changed. You should be able to fix up whatever else is wrong from here http://rafb.net/p/SSUvOP53.html edited to add output from the modified code D:\temp\test\Debug>test Name: John Doe Please enter a nonnegative seed value: 45 Please enter a lower bound [-25,-1] and an upper bound [1,25]: -15 15 Please enter the number of randoms to generate [10,50]: 10 Input Type Value Count Type Value ûûûûûûûûûû ûûûû- ûûûûûûûûûû ûûûû- Seed 45 Evens 2 Lower Bound -15 Odds 7 Upper Bound 15 Negatives 5 Number Generated 10 Positives 4 Zeros 1 OMG thank you, Is Malloc an Array though? |
|
Quoted:
Quoted:
Ok, in the function bool genRandom (int seed, int lb, int ub, int numToGen, int *randNum[]) What are you trying to do? Trying to seed a number(seed), have the upper and lower bounds(ub and lb) within which the generator should be generate numbers, and how many randoms to generate(numToGen). And I need to print the randoms in 5 rows and 10 columns Ok, if I understand you (and I could easily have made a mistake) , then lets try this.... (You need to add the code :) // This prints the values as it generates them // assuming numToGen is less than 50 // if it is more, only 50 rands will print //seed a number // calculate the bounds differential... // lower - upper is the diff // loop 5 times for 5 rows // loop 10 times for 10 columns // generate a random number, mod it to my range and add the bounds differential value // print random number and a tab // increment a counter tracking the number of randoms printed // if the counter is >= numToGen then print a newline (\n) and break or return // end loop 10 times // print a newline (\n) // end loop 5 times // If we got here then numToGen > 50 // all done There are your comments... Try adding the code :) To be even more precise, you will want to subtract from your mod value the range differential. JUST make sure the resulting value is not negative. Always always bound check your values. |
|
Why are you trying to generate all the random numbers inside the getRandom() function? If you're not supposed to use arrays, I suspect the teacher is steering you towards writing a method that generates a single random number between bounds given in the argument. Then use a for loop to call that function however many times you need.
Write functions that do one thing. |
|
Here you go, man. I got a little break at work, today. I commented the shit out of it (even though the code is obvious ) to indicate what I changed, and why.http://rafb.net/p/LWkIAo43.html I really wish you could use arrays, because it would clean the hell up out of the code. Also, there's really no need to use pointers all over the place in this app like you're doing, but I left them anyway. I also fixed it so that if a user enters something invalid, it quits (making the bool return values useful!), since that's what it looks like you wanted it to do. If you don't understand any of it, just ask. Name: Program n00b |