# How to connect a pair of number in 2d array?

edited June 2016

I am new to c programming. My aim is to break out the loop and move the number to connect to each other. I am not sure why my code is not working, I tried many times but still no luck. I really need help...please...

Here's my code :

``````  #include <stdio.h>
#include <stdlib.h>

void gameboard(char box)
{

int x,y;

for(x = 0; x < 9; x++)
{
for(y = 0; y  <9; y++)
{
box[x][y] ='_';

}

}

for(x = 0; x < 9; x++)
{

box[x]='#';
box[x]='#';

box[x]='#';
box[x]='#';

}

}

void rnum(char box)
{

int x,r1,r2,r3,r4;
char r='1';

srand( (unsigned) time(NULL));

for(x=0;x<8;x++)
{

r1 = 1 + rand()%7;
r2 = 1 + rand()%7;
r3 = 1 + rand()%7;
r4 = 1 + rand()%7;

box[r1][r2]= r + x;
box[r3][r4]= r + x;
}

}

void playgame(char box)
{
int x, y, choice2,num;
char input;

do{

printf("Please select a number (1-7) : ");
scanf("%i",&num);

if(num==0 ||num > 7)
{

printf("Invalid!\n");

}
else
{

printf("\nNumer %i is currently selected!\n", num);

}
}while(num==0 ||num > 7);

printf("\nMove\nSign out\n");
printf("\nEnter choice: \n");
scanf("%d", &choice2);

switch(choice2)
{
case 1:
{
printf("Press 'e' to go up\n");

}

{
printf("Press 'd' to go right\n");

}

{
printf("Press 's' to go left\n");

}

{
printf("Press 'x' to go down\n");

}

fflush(stdin);
scanf("%c", &input);
break;

case 2: printf("Bye!\n");
}

for(x=0; x<9; x++)

for(y=0; y<9; y++)
{
if(input == 'e')
if(box[x][y]==num)
{
box[x-1][y]=num;
}

else if(input == 'd')
if(box[x][y]==num)
{
box[x][y+1] =num;
}

else if(input == 's')
if(box[x][y]== num)
{
box[x][y-1] = num;
}

else if(input == 'x')
if(box[x][y]==num)
{
box[x+1][y] =num;
}

}

}

int main(void)
{

char box;
char name;
int x,y,choice,num;

printf("************* WELCOME TO NUMBER CONNECT ! ******************\n\n");

scanf("%s", &name);

printf("***Good day %s, let's start the game...all the best!!!***\n\n", name);

gameboard(box);

rnum(box);

for(x = 0; x < 9; x++)
{
for(y = 0; y  < 9; y++)
{
printf(" %c ",box[x][y]);

}

printf("\n\n");
}

playgame(box);

for(x = 0; x < 9; x++)
{
for(y = 0; y  < 9; y++)
{
printf(" %c ",box[x][y]);

}

printf("\n\n");
}

do {

printf(" Game Start\n Logout\n");
scanf("%d", &choice);

switch(choice)
{

case 1: printf("Please select a number (1-7) : ");
scanf("%i",&num);
break;

case 2: printf("Bye!\n");
break;
}
}while(choice!=2);

return 0;
}
``````

For example:
Connect a pair number '2'
(http://i.stack.imgur.com/Q2Kn1.jpg)))