Please Help Write a C program Question There are 120 permuta
Please Help!!!
Write a C++ program: Question:- There are 120 permutations of the numbers 1, 2, 3, 4, and 5 (5! or 5 factorial). Generate and classify each permutation by its sky number and display all the permutations whose sky number is 4. The sky number is a number from 1 to 5 that represents the number of array elements in the permutation that are greater than the greatest previous element. Let {2, 3, 1, 4, 5} be a permutation. Its sky number is 4 because, from the left to the right:
2 is greater than any previous number
3 is greater than any previous number
4 is greater than any previous number
5 is greater than any previous number
Any permutation beginning with the number 5 has a sky number of 1. The name comes from the idea that the numbers represent the stories in a multistory building. For the permutation {2, 3, 1, 4, 5}, this can be pictured as
so looking from the left to the right, you see the tops of 4 buildings.
Define a two-dimensional array permute[120][5] with 120 rows and 5 columns. Generate each permutation into a row of the array. Since there are 120 permutations, this will fill the array. Then make a pass through the rows of the array calculating each sky number. Display each permutation that has a sky number of 4. Your output should look like:
--The coding that I have so far ---
static int permute()
{
const int MAX = 4;
int ix, jx, kx, lx;
for (ix = 1; ix <= MAX; ++ix)
{
for (jx = 1; jx <= MAX; ++jx)
{
if (jx == ix)
continue;
for (kx = 1; kx <= MAX; ++kx)
{
if (kx == ix || kx == jx)
continue;
for (lx = 1; lx <= MAX; ++lx)
{
if (lx == ix || lx == jx || lx == kx)
continue;
std::cout << ix << \", \" << jx << \", \" << kx << \", \" << lx << \", \" << std::endl;
}
}
}
}
}
Solution
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int my[] = {1,2,3,4,5};
int arr[120][5];
for(int i=0;i<120;i++)
{
for(int j=0;j<5;j++)
{
arr[i][j] = my[j];
}
next_permutation(my,my+5);
}
int temp=1,hat=0;
for(int i=0;i<120;i++)
{
temp = 0;
for(int j=0;j<5;j++)
{
hat = 1;
for(int k=j-1;k>=0;k--)
{
if(arr[i][j]<arr[i][k])
{
hat=0;
break;
}
}
temp = temp+hat;
}
if(temp==4)
{
cout << arr[i][0] << \',\' << arr[i][1] << \',\' << arr[i][2] << \',\' << arr[i][3] << \',\' << arr[i][4];
cout<<\" has a sky number of 4\"<<endl;
}
}
return 0;
}

