swapping in a matrix

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2004
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

swapping in a matrix

 
0
  #1
Apr 12th, 2006
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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. int a[10][10]={0};
  7. int b[10][10]={0};
  8. void singleplay(int, int);
  9. void mainmenu();
  10. void newgame();
  11. void loadgame();
  12.  
  13. int main()
  14. {
  15. mainmenu();
  16. return 0;
  17. }
  18.  
  19. void mainmenu()
  20. {
  21. int choice;
  22. do
  23. {
  24. system("cls");
  25. printf("\t\tSliding Numbers\n\t\tMenu\n\t\t1. Start a new game\n\t\t2. Load a game\n\t\t3. Exit");
  26. printf("\n\t\t Enter a choice: ");
  27. scanf("%d", &choice);
  28. switch (choice)
  29. {
  30. case 1: newgame(); break;
  31. case 2: loadgame(); break;
  32. case 3: printf("\n\nGoodbye\n"); exit(0);
  33. default: printf("\nInvalid Choice\n"); system("PAUSE"); break;
  34. }
  35. } while (choice<1 || choice>3);
  36. }
  37.  
  38. void newgame()
  39. {
  40. int n, holes, noofelements, rowpos, colpos, choice;
  41. int i,j;
  42. srand(time(NULL));
  43. for(i = 0; i <10; i++)
  44. {
  45. for(j = 0; j <10; j++)
  46. a[i][j] = -1;
  47. }
  48. do
  49. {
  50. printf("Enter the size of the square (+ve integer, max 257): ");
  51. scanf("%d", &n);
  52. if (n<1 || n>256)
  53. printf("Invalid size.");
  54. }
  55. while (n<1 || n>256);
  56. do
  57. {
  58. printf("Enter the number of holes (max %d): ", n/2);
  59. scanf("%d", &holes);
  60. if (holes>(n/2))
  61. printf("Invalid number of holes");
  62. }
  63. while (holes>(n/2));
  64. noofelements=n*n-holes;
  65. for (i=1; i<=noofelements; i++)
  66. {
  67. do {
  68. rowpos=rand() % n;
  69. colpos=rand() % n;
  70. }
  71. while( a[rowpos][colpos] >= 0);
  72. a[rowpos][colpos]=i;
  73. } //do while loop doesnt work here.
  74. for (i=0; i<n; i++) //display
  75. {
  76. for (j=0; j<n; j++)
  77. {
  78. if (a[i][j]==-1)
  79. printf("%c", ' ');
  80. else
  81. printf("%d\t", a[i][j]);
  82. }
  83. printf("\n");
  84. }
  85. singleplay(n, holes);
  86. }
  87.  
  88. void loadgame()
  89. {
  90. int i,j,matricsize,spaces;
  91. char choice;
  92. FILE *infile;
  93. char filename[13];
  94. char quit[]="quit";
  95. do
  96. {
  97. printf("\n\n Please enter a filename (xxxxxxxx.yyy) or 'quit' to exit: ");
  98. scanf("%s", filename);
  99. if (strcmp(filename, quit)==0)
  100. mainmenu();
  101. else
  102. if ((infile = fopen(filename, "r")) == NULL)
  103. printf("\n Filename invalid");
  104. }
  105. while (((infile = fopen(filename, "r")) == NULL));
  106. fscanf(infile,"%d", &matricsize);
  107. fscanf(infile,"%d", &spaces);
  108. for(i=0;i<matricsize;i++)
  109. for(j=0;j<matricsize;j++)
  110. fscanf(infile,"%d", &a[i][j]);
  111. fclose(infile);
  112. for (i=0;i<matricsize;i++) //display
  113. {
  114. printf("\n");
  115. for (j=0;j<matricsize;j++)
  116. {
  117. printf("\t%d",a[i][j]);
  118. }
  119. }
  120. printf("\n\n\n");
  121. do //doesnt work but system("cls") covers it up
  122. {
  123. printf("\nThe above data will be loaded. do u want to continue? (y/n): ");
  124. scanf("%ch",&choice);
  125. switch (choice)
  126. {
  127. case 'y': singleplay(matricsize, spaces); break;
  128. case 'Y': singleplay(matricsize, spaces); break;
  129. case 'n': mainmenu(); break;
  130. case 'N': mainmenu(); break;
  131. default: printf("Invalid choice\n"); break;
  132. }
  133. }
  134. while (choice!='y' && choice!='Y' && choice!='n' && choice!='N');
  135. }
  136.  
  137. void singleplay(int n, int holes)
  138. {
  139. int i, j, k=0, noofelements, correct=0, temp;
  140. int x1, y1, x2, y2;
  141. system("cls");
  142. for (i=0; i<n; i++) //display with coordinates
  143. printf("\t%d", (i));
  144. printf("\n\n");
  145. for (i=0; i<n; i++)
  146. {
  147. printf("%d\t", (i));
  148. for (j=0; j<n; j++)
  149. {
  150. if (a[i][j]==-1)
  151. printf(" ");
  152. else
  153. printf("%d\t", a[i][j]);
  154. }
  155. printf("\n");
  156. }
  157. noofelements=n*n-holes;
  158. for (i=0; i<n; i++)
  159. for(j=0; j<n; j++)
  160. {
  161. if (k<noofelements)
  162. {
  163. k++;
  164. b[i][j]=k;
  165. }
  166. }
  167. while (correct==0) //swapping
  168. {
  169. printf("Enter the x y coordinates of the element to be moved: ");
  170. scanf("%d %d", &x1, &y1);
  171. y1++;
  172. if (x1>=n || y1>=n)
  173. printf("\nInvalid coordinate\n");
  174. printf("Enter the x y coordinates of the empty space: ");
  175. scanf("%d %d", &x2, &y2);
  176. y2+=2;
  177. if (x2>=n || y2>=n)
  178. printf("\nInvalid coordinate\n");
  179. temp=a[x1][y1];
  180. //testing:
  181. printf("\n%d\n", a[x1][y1]);
  182. a[x1][y1]=a[x2][y2];
  183. //testing:
  184. a[x2][y2]=temp;
  185. printf("\n%d\n", temp);
  186.  
  187. for (i=0; i<n; i++) //display with coordinates
  188. printf("\t%d", (i));
  189. printf("\n\n");
  190. for (i=0; i<n; i++)
  191. {
  192. printf("%d\t", (i));
  193. for (j=0; j<n; j++)
  194. {
  195. if (a[i][j]==-1)
  196. printf(" ");
  197. else
  198. printf("%d\t", a[i][j]);
  199. }
  200. printf("\n");
  201. }
  202. for (i=0; i<n; i++) //checking if matrix has been solved
  203. for (j=0; j<n; j++)
  204. {
  205. if (a[i][j]!=b[i][j])
  206. {correct=0; break;}
  207. else
  208. correct=1;
  209. }
  210. }
  211. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: swapping in a matrix

 
0
  #2
Apr 12th, 2006
>>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;
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

Re: swapping in a matrix

 
0
  #3
Apr 12th, 2006
I know that part. I did that. but it apparently does not work.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC