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

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

ID: 3756776 • 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 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);

}// 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