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

#include <iostream>
#include <algorithm> //swap()
#include <conio.h>
using namespace std;

//A function to display the current array
void displayArray(int array[4][4]);
void sortArray(int array[4][4]); 

int main()
{
	int i,j,a,count=1;
	int x[4][4];
	cout<<"\nEnter 15 Integers:\n";
	for(i=0;i<4;i++)

	{
		for(j=0;j<4;j++)
		{
            if (j == 3 && i == 3)
                x[i][j] = '2';
            else
            {
                cin>>x[i][j];                
            }

		}
	}
    
    x[3][3] = '2';
    i = 3; j = 3; 
    displayArray(x);

	while (a!='q')
   {
	   a = getch();
	   if(a == 75)
	   {
            if (j == 0)
			{
	            cout<<"\n\nNo more possible moves towards left" << endl;
            } 
			else 
			{
               j--;
               swap(x[i][j], x[i][j+1]);
               displayArray(x);
			   cout<<"\n\ntotal moves: "<<count++;
            }
       } 
		   else 
		   if (a == 77) 
		   {
				if (j == 3)
				{
					cout<<"\n\nNo more possible moves towards right" << endl;
				} 
				else 
				{
				    j++;
					swap(x[i][j], x[i][j-1]);
					displayArray(x);
					cout<<"\n\ntotal moves: "<<count++;
				}
		   }
			else 
			if (a == 72) 
			{
				 if (i == 0)
				 {
					 cout<<"\n\nNo more possible moves towards up" << endl;
				 } 
			     else 
				 {
					i--;
					swap(x[i][j], x[i+1][j]);
					displayArray(x);
					cout<<"\n\ntotal moves: "<<count++;
				 }
			   } 
			   else 
			   if (a == 80) 
			   {
					if (i == 3)
						{
							cout<<"\n\nNo more possible moves towards down" << endl;
						} 
					else 
					{
						i++;
						swap(x[i][j], x[i-1][j]);
						displayArray(x);
						cout<<"\n\ntotal moves: "<<count++;
					}
				}
				else
				if (a=='q')
				{
					a= getch();
					x[3][3] = '2';
					i = 3; j = 3;
					sortArray(x);
					break;
				}
			    					
   }
				
	return 0;
}

//This is a function that will display the current array.  
// note that the smile's location is denoted by a '2' in the array. 
void displayArray(int array[4][4])
{
	cout<<"\n\n";
	for(int i=0; i<4; i++)
	{
		for(int j=0; j<4; j++)
		{
           if (array[i][j] == '2')
		   {
               cout << '\t' << '\2';
           } 
		   else 
		   {
               cout << '\t' << array[i][j];
           }
           if (j == 3)
		   {
			   cout << endl;
           }
       }
	}
}


void sortArray(int array[4][4])
{
	cout<<"\n\n";
	// sorting an array values into ascending order
	for(int i = 0; i < 4; i ++)
	{
		for(int j = 0; j < 4; j ++)
		{
			if (array[i][j] == '2')
			{
				cout << '\t' << '\2';
			}
			else
			{
				printf("array[%d][%d] = %d  ", j, i, array[j][i]);
				printf("\n");
			}
		}
					
	}
}

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++.

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.

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

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:

for this row
  for this column
    if this element less than priorNumber
       change flag to false
       break from loop 
    else 
       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.

This article has been dead for over six months. Start a new discussion instead.