![]() |
|
#1
|
|||
|
|||
Hi I am kind of new at programming and I need some help please.I am working on a project trying to to get an array of 20 numbers. To be filled by the random generator with the range from 1 to 50 , with no duplicates and to be displayed with the first line the first number the second line the firtst and second number etc -----first through twenty. I have been working on for awhile but I just can't seem to get the numbers to come out to be 1 - 50 I end up with something like 0000000000000000204120
Then when I do get some numbers to come up I mess it up again trying to get it to diplay right. Please PLease Help. This is what I've done and it isn't giving me any numbers now. CPP / C++ / C Code:
Last edited by LuciWiz : 03-Apr-2005 at 23:37.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#3
|
|||
|
|||
|
Quote:
Why don't you do things one at a time: Task 1: Generate a random number between 1 and 50. Now you have a good start, I think: use srand() to start the random number generate with something you got from time() so that every run will give dirrerent results. Now, you have an expression that could be used to scale the random numbers, but there are a couple of problems: rand() returns an int that is greater than or equal to zero and less than or equal to some system-defined large int named RAND_MAX. Your expression had rand()/RAND_MAX. The result of this integer division is always zero. How do you get something else? Tell the compiler to convert one (or both) to a floating point number. The result of the floating point division will be a floating point number that is greater than or equal to zero and less than or equal to 1. If you want a number that is greater than or equal to zero and less than 1 (and I think that this is most useful) you can do it by: CPP / C++ / C Code:
Now if you want a number that is greater than or equal to 0.0 and less than 50.0, you could do this: CPP / C++ / C Code:
CPP / C++ / C Code:
Finally, if you want an int that is greater than or equal to 1 and less than or equal to 50 you could simply have this (since converting a floating point number to an int is accomplished by truncating the fractional part): CPP / C++ / C Code:
Try this in a loop by itself and see if the numbers look like they are all between 1 and 50 (inclusive). Now, step back and look at the real task. How would you do it if you had a bowl full of marbles with numbers on it. Every time you pull a ball, you write the number down in your list and put the ball back in the bowl. (This is like the random number generator, since the same number can occur again.) Now how do you get your list with all different numbers? You have a loop, and a counter that keeps track of how many numbers you have so far. (Initially the counter = 0.) You remain in this loop until you have a total of 20 successful draws. Inside the loop you have another loop: Draw number. Check the entire list (entries from 0 to the current value of the counter). If this number is already in the list, then draw another number. Keep doing this until you get a number that isn't already in the list. Put this number in the position indicated by the counter, and increment the counter. After incrementing the counter, if its new value is less than 20, go back and do the inner loop again. You now know how to draw a number between 1 and 50. You need to know how to make a loop. Regards, Dave |
|
#4
|
|||
|
|||
Thanks for the help with random number range Dave.Thanks alolt for your help I did get the random generator to work in the range from 1 50. I took apart the program and started with just that and now I am trying to add the while loop into it for the unique numbers but they won't diplay. I am still working on it though. Thanks again.
I would have posted the code with tags like someone suggested I am just not sure where to go to do that. This is my first day on the site sorry. CPP / C++ / C Code:
Last edited by LuciWiz : 03-Apr-2005 at 23:38.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#5
|
|||
|
|||
|
Quote:
Code tags: Before the first line of code put [ c ] (but don't put spaces between the bracket and the letter. After the last line of code put [ /c ] (without the spaces). Or, you could read the sticky at the top of the forum: GIDForums enables New [ C ] / [ C++ ] bbcode. Now: look at this in your code: CPP / C++ / C Code:
Point number 1. You must read up on how to make a for loop. This is a very good way to make a loop that executes only once. Is this what you had in mind? Why would you make a loop that executes once? Point number 2. A for loop includes only the very next statement. So the only thing in the loop is the statement assigning a value to x. If you want the loop to include more than one statement, you put a block of statements in braces {}. Point number 3. Where is the array of numbers where you will store the 20 distinct values that you are trying to get? Point number 4. Have you really thought about how you would do this without a computer? I tried to give a few hints. I will try some pseudo-code, but not everybody gets much out of pseudo-code. The problem that I have is that I am kind of a bottom-up kind of guy, and I am already thinking in terms of actual code, so it's kind of hard to be abstract in creating a top-down description. However, you might consider something like this for your algorithm: Pseudocode: start with an array of int, say x[20]. Set a counter, say, numberValid, to 0. Here's the iteration: outer loop: Start with numberValid = 0, and repeat the inner loop until numberValid = 20 inner loop: 1. Generate a random number between 1 and 50. 2. See if that number is in the array (look at x[0] up to, but not including x[numberValid]). If it is already in the array, then do number 1 and number 2 again. If it is not in the array, then set x[numberValid] to this number, and increment numberValid. Regards, Dave |
|
#6
|
|||
|
|||
Ok this is what I have so far I am just not getting this.CPP / C++ / C Code:
We are to suppose to modify this program in our book to have a random number generator put the numbers in order with unique numbers and have them display starting with the first number adding one each time a number is added till end. I can get the random generator to work but not when I mess around with it trying to get unique numbers. It is driving me crazy because I have been working on it for days now. |
|
#7
|
|||
|
|||
|
Quote:
Well I have no idea of what exactly what the program in your book does, let alone how it does it and how you would go about 'modifying' it to do something else. I have tried to give you my reasoning about how I would approach the problem, and I guess I haven't been very helpful at getting you into some kind of mindset to make a program. It's tough starting out, when you haven't really got a handle on the language to see how to do something like this. OK, here's a way to implement what I was trying to get at in my previous hints. CPP / C++ / C Code:
This is actual working code, but you have to make a function that I call AlreadyThere(). I could have put the logic in the loop, but maybe having it as a separate function keeps from cluttering up the main loop logic. So, what does it do: There is a loop that is going to fill in MAXNUM (that's 20 for this example) numbers in the int array id[]. All of this is the same that you set up. The loop counter, count points to the next element of id[] that needs a number. Now there is an inner loop that is going to repeatedly get numbers from rand() and see if the current number is already in the array. If it is already there, then the inner loop gets another number and checks again. Eventually there will be a number that is not already in the loop. So we put it there (that is store the new number in id[count] and continue with the outer loop --- the variable count is incremented in the outer loop, and the outer loop continues until we have all 20 required values. What about the function AlreadyThere()? Well it must search the array looking for the new number. It searches from 0 up to (but not including) the value of the index that we pass it (the position where we want to store the next value). The function AlreadyThere returns a boolean value "true" if the number is in the array, and "false" if it is not, so that the inner loop logic can know whether to store the new value or to get another number from rand() to try again. Regards, Dave |
|
#8
|
|||
|
|||
I keep trying to do that function.this is original code from book
CPP / C++ / C Code:
this is code now CPP / C++ / C Code:
I have tried to input function and I have tried to do it with an if statement to check for dups. I am just not sure of what to check against what. I also dont' understand why the bottom of the code isn't sorting anymore. This project has been harder for me than any other project. I don't know if because I had to start off with code to modify or if I am just not getting this project at all. I understand srand a little but trying to find if the number exists already in the array goes from what to what. |
|
#9
|
|||
|
|||
Me againI also wanted to thank you for all of your help. I have been getting so fustrated with this program that I forgot to write it earlier.
|
|
#10
|
|||
|
|||
Everytime I try to add the function I get error.ogram 4 LG.obj : error LNK2001: unresolved external symbol "bool __cdecl AlreadyThere(int * const,int,int)" (?AlreadyThere@@YA_NQAHHH@Z)
Debug/Program 4 LG.exe : fatal error LNK1120: 1 unresolved externals Errors like the ones I pasted above. Sorry to keep bugging you. |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Read/ Write EXCEL files using C/C++ programming | confused_pig | C++ Forum | 4 | 25-Nov-2005 00:27 |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 15:41 |
| which language ? | onauc | C++ Forum | 2 | 19-Nov-2004 02:53 |
| GUI programming | crystalattice | C++ Forum | 5 | 14-Sep-2004 12:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The