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.
May you be given directions by software engineer document writers.
Am I allowed to use rand() function?
(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.
May you be given directions by software engineer document writers.
Code int arr[11];
int random_numer()
{
int number = rand() % 10 + 1;
if(arr[number] == 1)
return random_numer();
else
{
arr[number] = 1;
return number;
}
}
Something like this?
very simple
with C you can use array for the rand values
with C++ you can use std::array
Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
Very important... You must return the values 1 through 10 randomly and no repeats.
May you be given directions by software engineer document writers.
Code void random_numers()
{
srand (time(NULL));
int arr[11] = {0};
for(int i = 1; i<=10;i++)
{
int number = rand() % 10 + 1;
if(arr[number] == 1)
i--;
else
{
cout<<number<<" ";
arr[number] = 1;
}
}
}
My best code I ever write. Isn't it?
(Apr 19, 2020 06:30 AM)FoxBlood Wrote: Code void random_numers()
{
srand (time(NULL));
int arr[11] = {0};
for(int i = 1; i<=10;i++)
{
int number = rand() % 10 + 1;
if(arr[number] == 1)
i--;
else
{
cout<<number<<" ";
arr[number] = 1;
}
}
}
My best code I ever write. Isn't it? Wow! I mean Wow! Why would you write it like that? For lol's?
May you be given directions by software engineer document writers.
DO NOT DEAL WITH THIS MEMBER
Code int getNum(int* array,int index){
int r = 0;
do{
r = rand() % 11 ;
for(int i = 0; i < index;++i){
if(r == array[i]){
r = 0;
}
}
}while(r == 0);
return r;
}
int main(){
int array[10] = {0};
for(int i = 0; i < 10; ++i){
array[i] = getNum(array,i);
printf("%i\n",array[i]);
}
}
(Apr 19, 2020 06:55 AM)akali Wrote: Code int getNum(int* array,int index){
int r = 0;
do{
r = rand() % 11 ;
for(int i = 0; i < index;++i){
if(r == array[i]){
r = 0;
}
}
}while(r == 0);
return r;
}
int main(){
int array[10] = {0};
for(int i = 0; i < 10; ++i){
array[i] = getNum(array,i);
printf("%i\n",array[i]);
}
}
So far... You are making it way more difficult than it has to be. You don't need a loop to solve the problem.
May you be given directions by software engineer document writers.
PHP Code
<?php int getNum(int* array,int index){ int r = 0; r = rand() % 10 + 1; for(int i = 0; i < index; ++i){ if(r == array[i]){ r = getNum(array, i + 1); } } return r; }
int main(){ int array[10] = {0}; for(int i = 0; i < 10; ++i){ array[i] = getNum(array, i); printf("%i\n", array[i]); } }
Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
Apr 19, 2020 01:14 PM
(This post was last modified: Apr 19, 2020 01:15 PM by saltycracker.)
(Apr 19, 2020 11:06 AM)HFDone Wrote: PHP Code
<?php int getNum(int* array,int index){ int r = 0; r = rand() % 10 + 1; for(int i = 0; i < index; ++i){ if(r == array[i]){ r = getNum(array, i + 1); } } return r; }
int main(){ int array[10] = {0}; for(int i = 0; i < 10; ++i){ array[i] = getNum(array, i); printf("%i\n", array[i]); } }
What is it with the loops? You don't need a loop or recursion to solve this problem.
May you be given directions by software engineer document writers.
PHP Code
<?php int getNum(){ static int[] arr = {8,5,1,9,7,6,3,4,2,10} static idx = 0; if(idx > 9) return 0; // already done all numbers return arr[idx++]; }
Is this considered random enough?
Have a cool signature? hit me up and I might use it.
Apr 19, 2020 10:06 PM
(This post was last modified: Apr 19, 2020 10:07 PM by saltycracker.)
Here's what I did..
Code #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 10
int getNum() {
static size_t size = 10;
static int arr[ARR_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int pos = rand() % size;
int ans = arr[pos];
int temp = arr[size - 1];
arr[size - 1] = ans;
arr[pos] = temp;
size--;//can reset size here when it reaches 0
return ans;
}
int main(int argc, char ** argv) {
srand((unsigned) time(NULL));
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
return 0;
}
May you be given directions by software engineer document writers.
(Apr 19, 2020 10:06 PM)saltycracker Wrote: Here's what I did..
Code #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 10
int getNum() {
static size_t size = 10;
static int arr[ARR_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int pos = rand() % size;
int ans = arr[pos];
int temp = arr[size - 1];
arr[size - 1] = ans;
arr[pos] = temp;
size--;//can reset size here when it reaches 0
return ans;
}
int main(int argc, char ** argv) {
srand((unsigned) time(NULL));
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
fprintf(stdout, "%d\n", getNum());
return 0;
}
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:
|