Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
Please help explain why my code is not working.
things I added from my previous question was the count[x] array. i want all of the arrays from count[0] to count[39] to all be equal to 0. I need to check what position my token mostly landed on the Monopoly table. I cant seem to find the error. Please help thank you. :)

C#
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int dice1(int iroll1);
int dice2(int iroll2);
main(){
    srand(time(NULL));
        int iDiceValue1, iDiceValue2,isumdice;
        int iVal1=0, iVal2=0, again=0;
        int ilocate=0;
        int x=0;
        int count[x];

while(x!=40){
count[x]=0;
x++;
}



do
{
    iDiceValue1 = dice1(iVal1);
    iDiceValue2 = dice2(iVal2);
        isumdice = iDiceValue1 + iDiceValue2;

    printf("\n%d \t ",isumdice);
    ilocate = ilocate + isumdice;

if(ilocate>39){
    ilocate = ilocate % 10;
}


if(ilocate==0){
    printf("GO");
    count[0]++;
}

else if(ilocate==1){
    printf("HILLSIDE AVENUE");
    count[1]++;
}

else if(ilocate==2){
    printf("COMMUNITY CHEST");
    count[2]++;
}

else if(ilocate==3){
    printf("HILLSIDE CRESCENT");
    count[3]++;
}

else if(ilocate==4){
    printf("INCOME TAX");
    count[4]++;
}

else if(ilocate==5)
{
printf("TOWN HALL");
count[5]++;
}

else if(ilocate==6)
{
printf("WELTON VALE");
count[6]++;
}

else if(ilocate==7)
{
printf("CHANCE?");
count[7]++;
}

else if(ilocate==8)
{
printf("BELLE VIEW");
count[8]++;
}

else if(ilocate==9)
{
printf("VALLEY WALK");
count[9]++;
}


else if(ilocate==10)
{
printf("JAIL");
count[10]++;
}

else if(ilocate==11)
{
printf("ROCKY ROAD");
count[11]++;

}
else if(ilocate==12)
{
printf("TESCO");
count[12]++;

}
else if(ilocate==13)
{
printf("GULLOCK TYNING");
count[13]++;

}
else if(ilocate==14)
{
printf("RACKVERNAL ROAD");
count[14]++;

}

else if(ilocate==15)
{
printf("WESTFIELD CHAPEL");
count[15]++;

}

else if(ilocate==16)
{
printf("WATERFORD PARK");
count[16]++;

}
else if(ilocate==17)
{
printf("COMMUNITY CHEST");
count[17]++;

}
else if(ilocate==18)
{
printf("ALDER TERRACE");
count[18]++;

}
else if(ilocate==19)
{
printf("WESTFIELD TERRACE");
count[19]++;

}
else if(ilocate==20){
printf("FREE PARKING");
count[20]++;

}

else if(ilocate==21){
printf("SOUTH ROAD");
count[21]++;

}

else if(ilocate==22){
printf("CHANCE");
count[22]++;

}

else if(ilocate==23){
printf("SILVER STREET");
count[23]++;

}

else if(ilocate==24){
printf("ST CHADS AVENUE");
count[24]++;
}

else if(ilocate==25){
printf("SOMERVALE SCHOOL");
count[25]++;
}

else if(ilocate==26){
printf("LONG BARNABY");
count[26]++;
}

else if(ilocate==27){
printf("BLACKBERRY WAY");
count[27]++;
}

else if(ilocate==28){
printf("SANSIBURYS");
count[28]++;
}

else if(ilocate==29){
printf("CLAPTON ROAD");
count[29]++;
}

else if(ilocate==30){
printf("GO TO JAIL");
count[30]++;
ilocate = 10;
}

else if(ilocate==31){
printf("BLUEBELL RISE");
count[31]++;
}

else if(ilocate==32){
    printf("MONGER LANE");
count[32]++;

}

else if(ilocate==33){
    printf("COMMUNITY CHEST");
count[33]++;
}

else if(ilocate==34){
    printf("GREENFIELD WALK");
count[34]++;

}

else if(ilocate==35){
printf("NORTON HILL SCHOOL");
count[35]++;
}

else if(ilocate==36){
printf("NORTH ROAD");
count[36]++;
}

else if(ilocate==37){
printf("CHANCE");
count[37]++;
}

else if(ilocate==38){

printf("LUXURY TAX");
count[38]++;
}

else{
printf("THE DYMBORO");
count[39]++;
}

again++;
getch();

}
while(again!=100);
}


int dice1(int iroll1){

 int irollvalue1 = rand();
 irollvalue1= (irollvalue1 % 6) + 1;
return irollvalue1;
}

int dice2(int iroll2){

 int irollvalue2 = rand();
 irollvalue2= (irollvalue2 % 6) + 1;
 return irollvalue2;
}
Posted
Comments
Frankie-C 31-Jan-15 10:36am    
If you want 40 elements in the array why declare it as int count[x]; where x=0?
Improve your coding: choose a more appropriate loop type (i.e.for instead of while), condense your code (consider creating a table for locations accessed with dice sum), Consider efficiency of your subs (what's the difference between dice1 and dice2? use the same and call twice).
You can consider a good result reducing all your code in less than 20 lines of code...
Reuben Cabrera 1-Feb-15 5:51am    
what do you mean by table or how do you create that? anyway thanks for the tips. ill try to improve the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900