sorting 2d array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 7
Reputation: ar31an is an unknown quantity at this point 
Solved Threads: 0
ar31an ar31an is offline Offline
Newbie Poster

sorting 2d array

 
0
  #1
Jun 16th, 2008
1 4 15 7
8 10 2 11
14 3 6 13
12 9 5 ☺

As you can see there is a ☺ at bottom right corner. Implement the program so that a box like the above one is displayed (lines may not be displayed). Allow the user to hit any of the arrow keys (up, down, left, or right).

If user hits say, right arrow key then program should give a message that this movement is not possible. If user hits left arrow key then 5 should move in place of ☺ and ☺ should move in place of 5, and box will look like:

1 4 15 7
8 10 2 11
14 3 6 13
12 9 ☺ 5

Similarly, you can experience with all arrow keys and movements of ☺ will take place, if possible. The user would continue hitting the arrow keys till the numbers aren’t sorted in ascending order.


here is my all code just the sorting code not working


  1. #include <iostream>
  2. #include <algorithm> //swap()
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. //A function to display the current array
  7. void displayArray(int array[4][4]);
  8. void sortArray(int array[4][4]);
  9.  
  10. int main()
  11. {
  12. int i,j,a,count=1;
  13. int x[4][4];
  14. cout<<"\nEnter 15 Integers:\n";
  15. for(i=0;i<4;i++)
  16.  
  17. {
  18. for(j=0;j<4;j++)
  19. {
  20. if (j == 3 && i == 3)
  21. x[i][j] = '2';
  22. else
  23. {
  24. cin>>x[i][j];
  25. }
  26.  
  27. }
  28. }
  29.  
  30. x[3][3] = '2';
  31. i = 3; j = 3;
  32. displayArray(x);
  33.  
  34. while (a!='q')
  35. {
  36. a = getch();
  37. if(a == 75)
  38. {
  39. if (j == 0)
  40. {
  41. cout<<"\n\nNo more possible moves towards left" << endl;
  42. }
  43. else
  44. {
  45. j--;
  46. swap(x[i][j], x[i][j+1]);
  47. displayArray(x);
  48. cout<<"\n\ntotal moves: "<<count++;
  49. }
  50. }
  51. else
  52. if (a == 77)
  53. {
  54. if (j == 3)
  55. {
  56. cout<<"\n\nNo more possible moves towards right" << endl;
  57. }
  58. else
  59. {
  60. j++;
  61. swap(x[i][j], x[i][j-1]);
  62. displayArray(x);
  63. cout<<"\n\ntotal moves: "<<count++;
  64. }
  65. }
  66. else
  67. if (a == 72)
  68. {
  69. if (i == 0)
  70. {
  71. cout<<"\n\nNo more possible moves towards up" << endl;
  72. }
  73. else
  74. {
  75. i--;
  76. swap(x[i][j], x[i+1][j]);
  77. displayArray(x);
  78. cout<<"\n\ntotal moves: "<<count++;
  79. }
  80. }
  81. else
  82. if (a == 80)
  83. {
  84. if (i == 3)
  85. {
  86. cout<<"\n\nNo more possible moves towards down" << endl;
  87. }
  88. else
  89. {
  90. i++;
  91. swap(x[i][j], x[i-1][j]);
  92. displayArray(x);
  93. cout<<"\n\ntotal moves: "<<count++;
  94. }
  95. }
  96. else
  97. if (a=='q')
  98. {
  99. a= getch();
  100. x[3][3] = '2';
  101. i = 3; j = 3;
  102. sortArray(x);
  103. break;
  104. }
  105.  
  106. }
  107.  
  108. return 0;
  109. }
  110.  
  111. //This is a function that will display the current array.
  112. // note that the smile's location is denoted by a '2' in the array.
  113. void displayArray(int array[4][4])
  114. {
  115. cout<<"\n\n";
  116. for(int i=0; i<4; i++)
  117. {
  118. for(int j=0; j<4; j++)
  119. {
  120. if (array[i][j] == '2')
  121. {
  122. cout << '\t' << '\2';
  123. }
  124. else
  125. {
  126. cout << '\t' << array[i][j];
  127. }
  128. if (j == 3)
  129. {
  130. cout << endl;
  131. }
  132. }
  133. }
  134. }
  135.  
  136.  
  137. void sortArray(int array[4][4])
  138. {
  139. cout<<"\n\n";
  140. // sorting an array values into ascending order
  141. for(int i = 0; i < 4; i ++)
  142. {
  143. for(int j = 0; j < 4; j ++)
  144. {
  145. if (array[i][j] == '2')
  146. {
  147. cout << '\t' << '\2';
  148. }
  149. else
  150. {
  151. printf("array[%d][%d] = %d ", j, i, array[j][i]);
  152. printf("\n");
  153. }
  154. }
  155.  
  156. }
  157. }


please help me i don't know wheter the sorting code is right or wrong if it is wrong please correct it the program is in c++.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: sorting 2d array

 
0
  #2
Jun 16th, 2008
Hey, this game is cooler than dirt!

I am not sure what your requirements are -- are you supposed to make your program solve the puzzle? or are you just supposed to notice when the user has successfully solved the puzzle?

You have, BTW, a conflict in your data. Both '2' and the smiley are represented by '2'. Make the smiley an invalid number.

Then, if all you have to do is check to see if the user has succeeded in sorting the array, just count through the array with a loop, checking to see that the current index is larger than the last, ignoring the invalid value (which is the smiley).

If you have to solve the puzzle let me know and I (or someone) will give you some hints.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: ar31an is an unknown quantity at this point 
Solved Threads: 0
ar31an ar31an is offline Offline
Newbie Poster

Re: sorting 2d array

 
0
  #3
Jun 16th, 2008
Then, if all you have to do is check to see if the user has succeeded in sorting the array, just count through the array with a loop, checking to see that the current index is larger than the last, ignoring the invalid value (which is the smiley).

did not really understand this can you please provide some code to give an example .

thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
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: sorting 2d array

 
0
  #4
Jun 16th, 2008
Say the box started out like this:

1 3 5
4 2 *

where * is the cute little box. Then it would be sorted if the numbers were like this:

1 2 3
4 5 *

To check if the numbers are sorted in ascending order then use a loop with a variable called priorNumber and a flag initialized to true. Assign element at [0][0] as prior number oustside the loop. Then run the loop:
  1. for this row
  2. for this column
  3. if this element less than priorNumber
  4. change flag to false
  5. break from loop
  6. else
  7. assign this element to priorNumber
Then when loop done evaluate the value of the flag. If the value is true then the array is sorted. If not, then it's not. You'll probably need to account for the value of the * or the cute little box or the smiley or whatever, but that shouldn't be too hard to accomplish.
Last edited by Lerner; Jun 16th, 2008 at 5:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: DigitalPackrat is an unknown quantity at this point 
Solved Threads: 1
DigitalPackrat's Avatar
DigitalPackrat DigitalPackrat is offline Offline
Light Poster

Re: sorting 2d array

 
0
  #5
Jun 17th, 2008
I wrote this code a long time ago. Thought I would share, though, I am sorry I did not review your code.

--Attached Code and EXE--
Attached Files
File Type: zip Console.zip (16.2 KB, 12 views)
[Catchy Signature Here]
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: ar31an is an unknown quantity at this point 
Solved Threads: 0
ar31an ar31an is offline Offline
Newbie Poster

Re: sorting 2d array

 
0
  #6
Jun 17th, 2008
thanks
Reply With Quote Quick reply to this message  
Reply

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




Views: 1182 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC