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 ☺ 5Similarly, 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++.