Thursday, July 9, 2009

C program inside?

10 points to whoever can help me fix this.bear in mind I've absolutely no idea how rand works(that's the reason I'm here)





#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#define num 4


int main()


{


int a,b,c;


int vect[num],i;


int x,y,n;





printf(" Introduzca numero de jugadas ");


scanf("%d",%26amp;x);


printf(" Introduzca monto del cual dispone ");


scanf("%d",%26amp;y);





do{ y--; printf(" \nLe quedan %d jugadas\n",y);


for(i=0;i%26lt;num;i++){


vect[i]=rand() % 10;


a=vect[i];b=vect[i];c=vect[i];


}


if(a==b%26amp;%26amp;a==c){


x=2*x; printf(" Ganaste el triple\n ");


}


else if(a==b%26amp;%26amp;a!=c){


x=(3/2)*x; printf(" Ganaste el doble\n ");


}


else if(a=!b%26amp;%26amp;a==c){


x=(3/2)*x; printf(" Ganaste el doble\n ");


}


else if(b==c%26amp;%26amp;b!=a){


x=(3/2)*x; printf(" Ganaste el doble\n ");


}


else if(b!=c%26amp;%26amp;b!=a){


x=(x/2); printf(" Perdiste\n ");


}


while(y%26gt;0%26amp;%26amp;x%26gt;1);


system("pause");





}

C program inside?
I made some changes, and tried to add comments to explain what I did. Hopefully this helps some.








#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;





//needed for the line below where we seed


//the random number generator


#include %26lt;time.h%26gt;





#define num 4


int main()


{


int a,b,c;


int vect[num],i;


int x,y,n;





//otherwise it will generate exactly the


//same "random" numbers every time


srand(time(0));





printf(" Introduzca numero de jugadas ");


scanf("%d",%26amp;x);


printf(" Introduzca monto del cual dispone ");


scanf("%d",%26amp;y);





do{ y--; printf(" \nLe quedan %d jugadas\n",y);


for(i=0;i%26lt;num;i++){


vect[i]=rand() % 10;


//a, b, and c all ended up with the same value,


//the last random number you generated


}





//moved this outside the loop


//so they don't all have the same value


a = vect[0];


b = vect[1];


c = vect[2];





if(a==b%26amp;%26amp;a==c){


x=2*x; printf(" Ganaste el triple\n ");


}


else if(a==b%26amp;%26amp;a!=c){


x=(3/2)*x; printf(" Ganaste el doble\n ");


}


else if(a=!b%26amp;%26amp;a==c){


x=(3/2)*x; printf(" Ganaste el doble\n ");


}


else if(b==c%26amp;%26amp;b!=a){


x=(3/2)*x; printf(" Ganaste el doble\n ");


}


else if(b!=c%26amp;%26amp;b!=a){


x=(x/2); printf(" Perdiste\n ");


}


}//added a } that was missing


while(y%26gt;0%26amp;%26amp;x%26gt;1);


system("pause");





}
Reply:I am guessing you are asking about this line...





vect[i]=rand() % 10;





rand() returns a random number 0 to 2147483647





% 10 says divide by 10 and give me the remainder, so vect[i] value will get a value 0-9





you do have an error further down also...





3/2 is 1 in integer math. so x is just going to be x after each time you do this (3/2)*x, x is always same as when you started.





also your if statement is over complex...





if(a==b%26amp;%26amp;a==c){ /* if all are the same */


x=2*x; printf(" Ganaste el triple\n ");


}


else if(a==b || b==c || a==c){ /* else if 2 are the same */


x=(3*x)/2; printf(" Ganaste el doble\n ");


}


else { /* else none are the same */


x=(x/2); printf(" Perdiste\n ");


}





the else if you know all three are not equal already because they would have been found in the if...
Reply:rand() is a random number generator function. To make it really random, you need to call the srand function before.

pink

No comments:

Post a Comment