Jump to content


Check out our Community Blogs



Recent Status Updates

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Selecting an element on a 2D array

c array nested

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 LonelyGuySylar

LonelyGuySylar

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 18 May 2016 - 07:46 AM

Basically the program will ask the user to select a number from 1-8(all numbers are in pairs), and when the user keyed in the value, the program will select the nearest number it encounters and mark the number.

 

example:before the user enters a value.

# # # # # # # # # # # #
# 1       2           #
#    4                #
# 6        2     8    #
#                     #
#    1     3       5  #
#                     #
#  3           6      #
#      4     5        #
#  7                  #
#      7        8     #
# # # # # # # # # # # #

After the user enters a value, assume the user enter number 1 as the value.

# # # # # # # # # # # #
# [1]       2         #
#    4                #
# 6        2     8    #
#                     #
#    1     3       5  #
#                     #
#  3           6      #
#      4     5        #
#  7                  #
#      7        8     #
# # # # # # # # # # # #

These are my codes, but the selectnumber function is not working, when i runs the program it just crashes.

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



void print_array(int arr[12][12], int ar[144]){

int i,j,m=0;

for(i=0;i<38;i++)
{
	printf("#");
}
printf("\n");
 for(i=0;i<12;i++) 
    {
	        for(j=0;j<14;j++)
	        {
				if(j==0)
				{
					printf("#");
				}
				else if(j==13)
				{
					printf("#");
				}
				else
				{
			
					arr[i][j-1] = ar[m++];
						if(arr[i][j-1] == 0)
						{
							printf("   ");
						}
						else
						{
			            	printf(" %d " , arr[i][j-1]);
						}
				}	 	 	 	 
			 }
		printf("\n");
    }
	for(i=0;i<38;i++)
{
	printf("#");
}
printf("\n");
}



void shuffle(int arr[12][12], int ar[144]){

int t,j,i;

 	srand ( time(NULL) );
    for(i=144-1;i>0;i--)     
    {
        
         j=rand() % (i+1);
         t=ar[j];
         ar[j] = ar[i];
         ar[i] = t;
    }



}



void selectnumber(int arr[12][12], int ar[144], int *num, int *x, int *y)
{
int i, j, test=0;
char temp;
printf("please select a number from 1-8:\n");
fflush(stdin);
scanf("%d", &temp);

arr[*y][*x]=' ';
arr[*y+1][*x]=' ';

for (j=0; j<=12; j++)
{
	for (i=0; i<=12; i++)
	{
		if(arr[j][i]==temp && test==0)
		{
		*y = j;
		*x = i;
		test = 1;
		}
	}
}

arr[*y][*x]='-';
arr[*y+1][*x]='-';

*num=temp;

}


int main()
{
    int array[12][12]={0};
    int randvalues[144]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
	int gx,gy;
	int ginput;
	
  

	shuffle(array,randvalues);
	print_array(array,randvalues);

	selectnumber(array, randvalues, &ginput, &gy, &gx);
	

    return 0;
}