Reputation: 107
What is the problem with this code?
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
void createNumb(int RandomNumber) {
srand(time(NULL));
int a, b, c, d;
a = rand() % 10 + 0;
b = rand() % 10 + 0;
c = rand() % 10 + 0;
d = rand() % 10 + 0;
while (b == a) {
b = rand() % 10 + 0;
}
while (c == b || c == a) {
c = rand() % 10 + 0;
}
while (d == c || d == b || d == a) {
d = rand() % 10 + 0;
}
RandomNumber = ("%d%d%d%d", a * 1000 + b * 100 + c * 10 + d * 1);
}
int main() {
int x;
createNumb(x);
printf("%d", x);
}
When I run this program, it always give me the result 0
.
I want it to print a random 4-digit number (each digit must be unique). What is the problem?
Upvotes: 2
Views: 113
Reputation: 144715
There are some problems in your C code, coming from a different language:
return
to return a value from a function and specify the return type before the function name`.RandomNumber = ("%d%d%d%d", a * 1000 + b * 100 + c * 10 + d * 1);
uses the operator ,
that ignores its left operand and evaluates to the right operand, whose value is stored into a local variable with automatic storage duration, and is lost upon function exit. Just use return a * 1000 + b * 100 + c * 10 + d * 1;
.rand() % 10000
, but an approach such as your may be needed for more digits or for specific constraints such as unique digits.srand()
such as clock()
and initialize the pseudo-random number generator just once int the main()
function.printf
format "%04d"
to make display an initial 0
if the result is less than 1000.Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d;
a = rand() % 10;
b = rand() % 10;
c = rand() % 10;
d = rand() % 10;
while (b == a) {
b = rand() % 10;
}
while (c == b || c == a) {
c = rand() % 10;
}
while (d == c || d == b || d == a) {
d = rand() % 10;
}
return a * 1000 + b * 100 + c * 10 + d;
}
int main() {
int x;
srand(clock());
x = createNumb();
printf("%04d\n", x);
return 0;
}
Note that you can avoid the loops in creatNumb()
. Here is a modified version that takes an argument from the command line to produce multiple results.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
return a * 1000 + (b + bb) * 100 + (c + cc) * 10 + (d + dd);
}
int main(int argc, char *argv[]) {
int x, n;
srand(clock());
n = (argc > 1) ? strtol(argv[1], NULL, 0) : 1;
while (n-- > 0) {
x = createNumb();
printf("%04d\n", x);
}
return 0;
}
Boolean operators evaluate to 1
when they are true and 0
otherwise. bb = (b >= a);
is a short notation for:
bb = 0;
if (b >= a)
bb = 1;
If you want to compute an array of 4 numbers with the given constraint, you should pass a pointer to the destination array as an argument to creatNumb()
. Here is a modified version with this approach:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void createNumb(int *dest) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
dest[0] = a;
dest[1] = b + bb;
dest[2] = c + cc;
dest[3] = d + dd;
}
int main(int argc, char *argv[]) {
int array[4];
srand(clock());
createNumb(array);
printf("%d%d%d%d\n", array[0], array[1], array[2], array[3]);
return 0;
}
Note that passing an array as a argument to a function actually passes a pointer to its first element: createNumb(array);
is equivalent to createNumb(&array[0]);
. This process is referred to as arrays decay into pointers in most expression contexts, it is somewhat confusing for beginners but allows for efficient argument passing.
Upvotes: 2
Reputation: 10138
createNumb
function returns void
instead of an int
.srand()
only once, so it’s better move it to your main
.Your createNumb
function should return a random integer, like this:
int createNumb(void) {
/*
** Simplified for readability.
** Here you would use your function that generates 4 different digits
*/
return rand();
}
int main(void) {
srand(time(NULL))(
int x = createNumb();
printf("%d" , x);
}
Or, if you want createNumb
to fill an existing integer, use a pointer:
void createNumb(int *randomNumber) {
/*
** Simplified for readability.
** Here you would use your function that generates 4 different digits
*/
*randomNumber = rand();
}
int main(void) {
int x;
srand(time(NULL))
createNumb(&x); // Passing a pointer to x
printf("%d" , x);
}
Upvotes: 3