Πρόβλημα στη εντολή rand() γλώσσα C
Δημοσιεύτηκε: 28 Απρ 2011, 15:03
Καλησπέρα, θα μπορούσε κάποιος να μου εξηγήσει γιατί η rand() δε λειτουργεί άμα χρησιμοποιήσω τα q και w (q=n+1, w=m+1) ενώ άμα βάλω στη θέση τους n και m λειτουργεί το πρόγραμμα?
Μη δώσετε σημασία στις συναρτήσεις(δεν τισ έβαλα λόγο χώρου) το πρόγραμμα είναι ακομα υπο κατασκευή
Μη δώσετε σημασία στις συναρτήσεις(δεν τισ έβαλα λόγο χώρου) το πρόγραμμα είναι ακομα υπο κατασκευή
- Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
void tamplo(int n, int m, int **pa);
void narkes(int n, int m, int **pa);
int main(int argc, char *argv[])
{
int i,j,n,m,o,p,a,b,r1,r2,q,w;
int **pa=NULL;
printf("Dwste tis diastaseis tou paixnidiou n x m\n");
printf("Prosoxh, to tamplo mporei na exei megethos to poly 24x30\n");
scanf("%d%d", &n,&m);
printf("Epilxte to epipedo duskolias tou paixnidiou\n");
printf("Pathste 1 gia eukolo, 2 gia metro, 3 gia duskolo kai 4 gia akatorthoto\n");
scanf("%d", &o);
if(o==1)
{
p=m*n/8;
}
if(o==2)
{
p=m*n/7;
}
if(o==3)
{
p=m*n/6;
}
if(o==4)
{
p=m*n/5;
}
pa=(int**)calloc(n, sizeof(int*)); //desmeush mnhmhs gia dimiourgia 2d pinaka
for(i=0;i<n;i++)
{
pa[i]=(int*)calloc(m, sizeof(int));
}
tamplo(n,m,pa);
printf("dwste tis syntetagmenes tou tetragwnou pou thelete na anoixete\n");
scanf("%d%d", &a,&b);
pa[a][b]=9;
q=n+1;
w=m+1;
for(i=0;i<p;i++)
{
do
{
r1=rand()%q;
r2=rand()%w;
}while(r1==a && r2==b);
pa[r1][r2]=1;//to 10 antiprosopevei tis narkes
}
tamplo(n,m,pa);
for(i=0;i<n;i++)
{
free(pa[i]);
}
free(pa);
system("PAUSE");
return 0;
}