![]() |
|
[C] A Challenge for the bored 2 - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] A Challenge for the bored 2 (/showthread.php?tid=6068896) Pages: Pages
1
2
|
A Challenge for the bored 2 - saltycracker - Apr 19, 2020 Write a function that will produce the numbers 1 - 10 randomly and not return the same value twice. Example: Say I call my function getNum The first time I call getNum it may produce: 7 The second time I call getNum it may produce: 3 The third time I call getNum it nay produce: 10 The fourth time I call getNum it may produce: 8 The fifth time I call getNum it may produce: 2 The sixth time I call getNum it nay produce: 1 The seventh time I call getNum it nay produce: 4 The eigtht time I call getNum it may produce: 5 The nineth time I call getNum it may produce: 9 The tenth time I call getNum it nay produce: 6 Good luck... C++'ers, try to say away from container classes. Note: What happens at the end of the cycle is up to you. You can reset the function to start again of just do nothing. RE: A Challenge for the bored 2 - FoxBlood - Apr 19, 2020 Am I allowed to use rand() function? RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020 (Apr 19, 2020 05:28 AM)FoxBlood Wrote: Am I allowed to use rand() function? Yes and srand too. I just want people to think about a way to solve this without relying on a container class. RE: A Challenge for the bored 2 - FoxBlood - Apr 19, 2020 Code int arr[11];RE: A Challenge for the bored 2 - HFDone - Apr 19, 2020 very simple with C you can use array for the rand values with C++ you can use std::array RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020 Very important... You must return the values 1 through 10 randomly and no repeats. RE: A Challenge for the bored 2 - FoxBlood - Apr 19, 2020 Code void random_numers()RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020 (Apr 19, 2020 06:30 AM)FoxBlood Wrote:Wow! I mean Wow! Why would you write it like that? For lol's? RE: A Challenge for the bored 2 - akali - Apr 19, 2020 Code int getNum(int* array,int index){RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020 (Apr 19, 2020 06:55 AM)akali Wrote: So far... You are making it way more difficult than it has to be. You don't need a loop to solve the problem. RE: A Challenge for the bored 2 - HFDone - Apr 19, 2020 PHP Code RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020 (Apr 19, 2020 11:06 AM)HFDone Wrote: What is it with the loops? You don't need a loop or recursion to solve this problem. RE: A Challenge for the bored 2 - severos - Apr 19, 2020 PHP Code Is this considered random enough? RE: A Challenge for the bored 2 - saltycracker - Apr 19, 2020 Here's what I did.. Spoiler (Click to View) RE: A Challenge for the bored 2 - 0xDEAD10CC - Apr 21, 2020 (Apr 19, 2020 10:06 PM)saltycracker Wrote: Here's what I did.. Why define a constant ARR_SIZE then not use it to assign your size variable? That makes it nearly useless, you could have just omitted the constant and all usages of it to get the same effect. You also have an extra variable in there that you don't need:
|