943,587 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1559
  • C++ RSS
Jun 16th, 2008
0

sorting 2d array

Expand Post »
Quote ...
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


C++ Syntax (Toggle Plain Text)
  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++.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ar31an is offline Offline
14 posts
since Apr 2008
Jun 16th, 2008
0

Re: sorting 2d array

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jun 16th, 2008
0

Re: sorting 2d array

Quote ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ar31an is offline Offline
14 posts
since Apr 2008
Jun 16th, 2008
0

Re: sorting 2d array

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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 17th, 2008
0

Re: sorting 2d array

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, 41 views)
Reputation Points: 10
Solved Threads: 1
Light Poster
DigitalPackrat is offline Offline
33 posts
since Jan 2008
Jun 17th, 2008
0

Re: sorting 2d array

thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ar31an is offline Offline
14 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: sudoku program 3x3 only
Next Thread in C++ Forum Timeline: Need general, esoteric advice on C++ in the real world..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC