0

I have a code in which I want to swap an element in a matrix with another. User inputs the row, column no of the elements to be swapped. But the code doesn't seem to work. Can somebody help?
PS: The swapping starts in the snigleplayer() function.

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

int a[10][10]={0};
int b[10][10]={0};
void singleplay(int, int);
void mainmenu();
void newgame();
void loadgame();

int main()
{
 mainmenu();
 return 0;
}

void mainmenu()
{
	int choice;
	do
	{
    system("cls");
	printf("\t\tSliding Numbers\n\t\tMenu\n\t\t1. Start a new game\n\t\t2. Load a game\n\t\t3. Exit");
    printf("\n\t\t Enter a choice: ");
	scanf("%d", &choice);
	switch (choice)
	{
	 case 1: newgame(); break;
	 case 2: loadgame(); break;
	 case 3: printf("\n\nGoodbye\n"); exit(0);
	 default: printf("\nInvalid Choice\n"); system("PAUSE"); break;
	}
	} while (choice<1 || choice>3);
}

void newgame()
{
 int n, holes, noofelements, rowpos, colpos, choice; 
 int i,j;
 srand(time(NULL));
 for(i = 0; i <10; i++)
 {
 	 for(j = 0; j <10; j++)
		 a[i][j] = -1;
 }
 do
 {
  printf("Enter the size of the square (+ve integer, max 257): ");
  scanf("%d", &n);
  if (n<1 || n>256)
	 printf("Invalid size.");
 }
 while (n<1 || n>256);
 do
 {
  printf("Enter the number of holes (max %d): ", n/2);
  scanf("%d", &holes);
  if (holes>(n/2))
 	 printf("Invalid number of holes");
 }
 while (holes>(n/2));
 noofelements=n*n-holes;
   for (i=1; i<=noofelements; i++)
	{
	 do {
	    	rowpos=rand() % n;
			colpos=rand() % n;
		} 
	 while( a[rowpos][colpos] >= 0);
		a[rowpos][colpos]=i;
	}										//do while loop doesnt work here.
	for (i=0; i<n; i++) //display
	{
		for (j=0; j<n; j++)
		{
		    if (a[i][j]==-1)
			 printf("%c", ' ');
			else
			printf("%d\t", a[i][j]);
		}
		printf("\n");
	}
 singleplay(n, holes);
}

void loadgame()
{
 int i,j,matricsize,spaces;
 char choice; 
 FILE *infile;
 char filename[13];
 char quit[]="quit";
 do
 {
  printf("\n\n Please enter a filename (xxxxxxxx.yyy) or 'quit' to exit: ");
  scanf("%s", filename);
  if (strcmp(filename, quit)==0) 
	 mainmenu();
  else
  if ((infile = fopen(filename, "r")) == NULL)  
     printf("\n Filename invalid");
 }
 while (((infile = fopen(filename, "r")) == NULL));  
 fscanf(infile,"%d", &matricsize);
 fscanf(infile,"%d", &spaces);
 for(i=0;i<matricsize;i++)
  for(j=0;j<matricsize;j++)
   fscanf(infile,"%d", &a[i][j]);
 fclose(infile);
 for (i=0;i<matricsize;i++)		//display
 {
  printf("\n");
  for (j=0;j<matricsize;j++)
  {
   printf("\t%d",a[i][j]);
  }
 }
 printf("\n\n\n");
 do					//doesnt work but system("cls") covers it up
 {
  printf("\nThe above data will be loaded. do u want to continue? (y/n): ");
  scanf("%ch",&choice);
  switch (choice)
  {
   case 'y': singleplay(matricsize, spaces); break;
   case 'Y': singleplay(matricsize, spaces); break;
   case 'n': mainmenu(); break;
   case 'N': mainmenu(); break;
   default: printf("Invalid choice\n"); break;
  }
 }
 while (choice!='y' && choice!='Y' && choice!='n' && choice!='N');
}

void singleplay(int n, int holes)
{
 int i, j, k=0, noofelements, correct=0, temp;
 int x1, y1, x2, y2;
 system("cls");
 for (i=0; i<n; i++)  //display with coordinates
	 printf("\t%d", (i));
 printf("\n\n");
 for (i=0; i<n; i++)
 {
  printf("%d\t", (i));
  for (j=0; j<n; j++)
  {
   if (a[i][j]==-1)
   printf(" ");
   else
   printf("%d\t", a[i][j]);
  }
  printf("\n");
 }
 noofelements=n*n-holes;
 for (i=0; i<n; i++)
	 for(j=0; j<n; j++)
	 {
		 if (k<noofelements)
		 {
		 k++;
		 b[i][j]=k;
		 }
	 }
 while (correct==0) //swapping
 {
  printf("Enter the x y coordinates of the element to be moved: ");
  scanf("%d %d", &x1, &y1);
  y1++;
  if (x1>=n || y1>=n)
	  printf("\nInvalid coordinate\n");
  printf("Enter the x y coordinates of the empty space: ");
  scanf("%d %d", &x2, &y2);
  y2+=2;
  if (x2>=n || y2>=n)
	  printf("\nInvalid coordinate\n");
  temp=a[x1][y1];
  //testing:
  printf("\n%d\n", a[x1][y1]);
  a[x1][y1]=a[x2][y2];
  //testing:
  a[x2][y2]=temp;
  printf("\n%d\n", temp);
  
  for (i=0; i<n; i++) //display with coordinates
	 printf("\t%d", (i));
     printf("\n\n");
  for (i=0; i<n; i++)
  {
   printf("%d\t", (i));
   for (j=0; j<n; j++)
   {
    if (a[i][j]==-1)
    printf(" ");
    else
    printf("%d\t", a[i][j]);
   }
   printf("\n");
  }
  for (i=0; i<n; i++)  //checking if matrix has been solved
	 for (j=0; j<n; j++)
	 {
	  if (a[i][j]!=b[i][j])
	  {correct=0; break;}
	  else
	  correct=1;
	 }
 }
}
2
Contributors
2
Replies
4
Views
11 Years
Discussion Span
Last Post by iamboredguy
0

>>swap an element in a matrix with another

As an example, if you had a matrix like this:
int matrix[3][3];

and you wanted to swap the element in the third row and second column with the element in the first row and first column then you could do something like this:

int temp;
temp = matrix[2][1];
matrix[2][1] = matrix[0][0];
matrix[0][0] = temp;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.