Reputation: 123
Thanks for all helped me before. But I still have some questions about the program. How to generate a new random number while the new random number is equal to the previous random number? Also how to transpose the matrix?
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
int _tmain(int argc, _TCHAR* argv[])
{
int num2 = 0;
int num=0, i, j;
int mtx[9][9] = {0};
while (num < 3 || num > 9) {
printf("Enter an integer (3-9): ");
scanf("%d", &num);
}
do
{
srand(time(NULL));
switch (num)
{
case 3: num2 = rand() % 8;
break;
case 4: num2 = rand() % 15;
break;
case 5: num2 = rand() % 24;
break;
case 6: num2 = rand() % 35;
break;
case 7: num2 = rand() % 48;
break;
case 8: num2 = rand() % 63;
break;
case 9: num2 = rand() % 80;
break;
}
for (i=0; i < num; ++i)
for (j=0; j < num; ++j)
mtx[i][j] = num2;
}
while ( num2 == num2);
for (i=0; i < num; ++i) {
for (j=0; j < num; ++j)
printf("%i ", mtx[i][j]);
printf("\n");
}
return 0;
}
Update:
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
int _tmain(int argc, _TCHAR* argv[])
{
int prevNum2 = 0;
int num2 = 0;
int num = 0, i, j; // Added initializers and loop counters
int mtx[9][9] = {0}; // Reserve enough space for the worst-case scenario
while (num < 3 || num > 9) { // Added input validation loop
printf("Enter an integer (3-9): ");
scanf("%d", &num);
}
srand(time(NULL));
do{
prevNum2 =num2;
switch (num)
{
case 3: num2 = rand() % 8;
break;
case 4: num2 = rand() % 15;
break;
case 5: num2 = rand() % 24;
break;
case 6: num2 = rand() % 35;
break;
case 7: num2 = rand() % 48;
break;
case 8: num2 = rand() % 63;
break;
case 9: num2 = rand() % 80;
break;
}
// Loop through the matrix elements we want, filling each with a random number
for (i=0; i < num; ++i)
for (j=0; j < num; ++j)
mtx[i][j] = num2;
}
while (num2 == prevNum2);
/* Do something with the matrix here (display it, etc) */
for (i=0; i < num; ++i) {
for (j=0; j < num; ++j)
printf("%i ", mtx[i][j]);
printf("\n");
}
return 0;
}
Upvotes: 0
Views: 1065
Reputation: 96121
I think you're trying to initialize elements in mtx
with unique random integers from 0 to n-1
, where n
is the number of elements in mtx
(does mtx
represent a sudoku cell?). If this is the case:
Your for
loop sets all the elements of mtx
to the same value. So, if you are trying to do what I think, that wouldn't work in any case.
What you want to do is to shuffle a list of numbers from 0
to n-1
. Fisher-Yates shuffle is pretty simple and efficient.
If you don't want to deal with malloc
and friends, here's one way to do it.
/* maximum possible size, replace ... with actual numbers */
int list[] = {0,1,2,3,4,5,6,7,8,...,80};
/* function to shuffle the first n elements of list */
void shuffle(int *list, size_t n);
and then, when you know the value of num
.
shuffle(list, num*num);
for (i=0; i < num; ++i)
for (j=0; j < num; ++j)
mtx[i][j] = list[i*num+j];
and you can get rid of all code dealing with random numbers, your do...while
loop, etc.
I haven't written shuffle()
for you, becuase I think it will be a fun exercise for you to do. :-)
Finally, your prompt:
printf("Enter an integer (3-9): ");
might not be displayed to the user at the time you call printf()
above, because stdout
is line buffered by default on many systems. To make sure it's displayed before your program waits for input, do:
fflush(stdout);
before you try to read the input. This will make sure the prompt gets displayed properly.
Upvotes: 0
Reputation: 9314
I believe that the easiest way to fix this without radical changes is to eliminate the do-while loop. If you want to generate a matrix (or do anything to each element of a matrix - display, modify, etc...), the simplest approach is to work inside of a double nested for-loop. Your variable 'prevnum2' isn't really needed.
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
switch (num) { // generate a random number
...
}
mtx[i][j] = num2; // store the random number in the matrix
}
}
When you move onward and upward in your programming life, you'll definitely want to look into Alok's excellent suggestions for better random number generation techniques.
Upvotes: 0
Reputation: 50017
The code section which reads
switch (num)
{
case 3: num2 = rand() % 8; break;
case 4: num2 = rand() % 15; break;
case 5: num2 = rand() % 24; break;
case 6: num2 = rand() % 35; break;
case 7: num2 = rand() % 48; break;
case 8: num2 = rand() % 63; break;
case 9: num2 = rand() % 80; break;
}
could be rewritten as
num2 = rand() % ((num * num) - 1);
which is more compact and arguably clearer.
Upvotes: 2
Reputation: 15925
I think you should change this line:
while ( num2 == num2);
As this will go on indefinitely for any value of num2
EDIT:
to make my comment cleaerer here are the program bits to make it work:
do
{
srand(time(NULL));
switch (num)
becomes:
do
{
int prevNum2 =num2;
switch(num)
and
while ( num2 == num2);
becomes:
while( num2 == prevNum2);
FINAL EDIT:
in a response to your edit. This bit of code is clearly not what you intended:
for (i=0; i < num; ++i)
for (j=0; j < num; ++j)
mtx[i][j] = num2;
}
What it does is fill the entire matrix with the same number.
what you actually want (probably) is that every next cell is filled with a different number. For this you need to have a loop construct of some sort.
Something along the lines of:
for(int i; i<num*num; i++)
{
//draw random number bit (shortened for clearity... should be your whole switch bit)
num2 = rand() % 8;
mtx[i%num][i/num] = num2;
}
thats it... This way the entire matrix gets filled with random values. No need to check if the value was already drawn before...since that was not the problem
Upvotes: 1
Reputation: 4130
do { .. } (x == x)
is an infinite loop. You don't have any break statements to exit this.
Upvotes: 0
Reputation: 70819
Like Neil said, srand() needs to be called once, at the beginning of the program. Every time you call srand you're seeding the random number generator, but with this program it'll always be seeded with the same time, so you'll end up with the same random number.
<joke>
Of course, that's perfectly random enough by some definitions:
</joke>
Upvotes: 1