Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming: //-------------------------------------------------- // Problem #

ID: 3756683 • Letter: C

Question

C Programming:

//--------------------------------------------------

// Problem #24

// Problem24.c

//--------------------------------------------------

#include

#include

#include

#define SHOWCLIMB

const int LARGEST_STEPSIZE = 2;

//--------------------------------------------------

int main()

//--------------------------------------------------

{

   void ClimbNStairs(int climb[],int steps,const int n,int *ways);

   int n;

   printf("n? ");

   while ( scanf("%d",&n) != EOF )

   {

      int *climb = (int *) malloc(sizeof(int)*(n+1));

      int ways;

      ways = 0;

      ClimbNStairs(climb,0,n,&ways);

#ifndef SHOWCLIMB

      printf("%10d ways ",ways);

#endif

      free(climb);

      printf("n? ");

   }

   system("PAUSE");

   return( 0 );

}

//--------------------------------------------------

void ClimbNStairs(int climb[],int steps,const int n,int *ways)

//--------------------------------------------------

{

   int LengthOfClimb(const int climb[],const int steps);

   if ( LengthOfClimb(climb,steps) == n )

   {

      (*ways)++;

#ifdef SHOWCLIMB

{

      Student provides missing code to display the climb[] using the required format.

}

#endif

   }

   else

   {

      int stepSize;

      Student provides missing code to consider adding/deleting a 1-stair step to climb,

         then adding/deleting a 2-stair step to climb[],..., finally, adding/deleting a

         LARGEST_STEPSIZE-stair step to climb[]. You must "sandwich" a recursive call between each

         add and delete. Hints Consider the state-space tree that describes the solution space

         for this problem. It is only possible to make a stepSize-stair step

         when (LengthOfClimb(climb,steps)+stepSize <= n).

   }

}

//--------------------------------------------------

int LengthOfClimb(const int climb[],const int steps)

//--------------------------------------------------

{

   Student provides missing code to compute the quantification

    steps

      climb[i]

    i = 1

}

PLEASE fill in the missing source code in the THREE designated areas in the source file Problem24.c (located below) that says "Student provides missing code..." below. Please also provide a screenshot(s) of the compiled output once complete. I have provided sample ones which should give an idea on how it should look.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

// Function to create all possible path
void createPath(int n, char* paths, int level)
{
// Checks if n value is zero
if (n == 0)
{
// Assigns null character at level index position of path
paths[level] = '';
// Displays the string
printf("%s ", paths);
}// End of if condition

// Checks if n value is greater then or equals to 1
if(n >= 1)
{
// Assigns character '1' at level index position of path
paths[level] = '1';
// Recursively calls the path with n minus one as first parameter
// level plus one for next level as third parameter
createPath(n - 1, paths, level + 1);
}// End of if condition

// Checks if n value is greater then or equals to 2
if(n >= 2)
{
// Assigns '2' character at level index position of path
paths[level] = '2';
// Recursively calls the path with n minus two as first parameter
// level plus one for next level as third parameter
createPath(n - 2, paths, level + 1);
}// End of if condition
}// End of function

// Function to display the steps
void displayPaths(int n)
{
// Dynamically creates an pointer of size n to store the paths
char* paths = (char *) malloc(sizeof(char) * n);
// Calls the function to create and display path
createPath(n, paths, 0);
printf(" ");
}// End of function

// main function definition
int main()
{
// To store n value entered by the user
int n;

// Loops till n value is not zero
do
{
// Accepts n value from the user
printf(" Enter the N value (Enter 0 to stop): ");
scanf("%d", &n);
// Checks if n value is zero then stop
if(n == 0)
break;
// Calls the function to displays the steps
displayPaths(n);
}while(1); // End of do while loop

return 0;
}// End of main function

Sample Output:

Enter the N value (Enter 0 to stop): 1
1


Enter the N value (Enter 0 to stop): 2
11
2


Enter the N value (Enter 0 to stop): 3
111
12
21


Enter the N value (Enter 0 to stop): 4
1111
112
121
211
22


Enter the N value (Enter 0 to stop): 5
11111
1112
1121
1211
122
2111
212
221


Enter the N value (Enter 0 to stop): 6
111111
11112
11121
11211
1122
12111
1212
1221
21111
2112
2121
2211
222


Enter the N value (Enter 0 to stop): 7
1111111
111112
111121
111211
11122
112111
11212
11221
121111
12112
12121
12211
1222
211111
21112
21121
21211
2122
22111
2212
2221


Enter the N value (Enter 0 to stop): 10
1111111111
111111112
111111121
111111211
11111122
111112111
11111212
11111221
111121111
11112112
11112121
11112211
1111222
111211111
11121112
11121121
11121211
1112122
11122111
1112212
1112221
112111111
11211112
11211121
11211211
1121122
11212111
1121212
1121221
11221111
1122112
1122121
1122211
112222
121111111
12111112
12111121
12111211
1211122
12112111
1211212
1211221
12121111
1212112
1212121
1212211
121222
12211111
1221112
1221121
1221211
122122
1222111
122212
122221
211111111
21111112
21111121
21111211
2111122
21112111
2111212
2111221
21121111
2112112
2112121
2112211
211222
21211111
2121112
2121121
2121211
212122
2122111
212212
212221
22111111
2211112
2211121
2211211
221122
2212111
221212
221221
2221111
222112
222121
222211
22222


Enter the N value (Enter 0 to stop): 0