Question

In: Computer Science

note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is...

note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is allowed.

The game of “Jump It” consists of a board with n positive integers in a row, except for the first
column, which always contains zero. These numbers represent the cost to enter each column.
Here is a sample game board where n is 6:
0 3 80 6 57 10
The object of the game is to move from the first column to the last column with the lowest total
cost.
The number in each column represents the cost to enter that column. You always start the game
in the first column and have two types of moves. You can either move to the adjacent column or
jump over the adjacent column to land two columns over. The cost of a game is the sum of the
costs of the visited columns.
In the board shown above, there are several ways to get to the end. Starting in the first column,
our cost so far is 0. We could jump to 80, then jump to 57, and then move to 10 for a total cost
of 80 + 57 + 10 = 147. However, a cheaper path would be to move to 3, jump to 6, then jump to
10, for a total cost of 3 + 6 + 10 = 19.

Write a recursive function that solves this problem and returns the lowest cost of a game board
represented and passed as an array.

Note: your function shouldn’t output the actual sequence of jumps, only the lowest cost of this
sequence.

IMPORTANT: Use only user defined functions. No C++ library function is allowed. Please write it in a simple and elementary way.

Solutions

Expert Solution

// you have to pass size because we are not using STL LIBRARAY HERE
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int lowestCost(int board[],int current,int total,int size,int size2) {
total+=board[current];
if(current == size -1)
return total;
else if(current == size -2)
return lowestCost(board,current+1,total,size, size2);
else
{
int path1 = lowestCost(board,current+1,total,size,size2);
int path2 = lowestCost(board,current+2,total,size,size2);
// Use math library to return minimum of the paths
if(path1> path2){
return path2;
}
else {
return path1;
}

}
}
int main()
{
int size=6;
int *board= new int[size];
// SETTING VALUES FOR RUNNING CODE YOU CAN USE LOOP TO INPUT
board[0]=0;
board[1]=3;
board[2]=80;
board[3]=6;
board[4]=59;
board[5]=10;
int size2=8;
int cost = 0;
cost = lowestCost(board,0,cost,size,size2);
// For smaller board
for(int i=0;i < 6;i++)
cout<<board[i]<<" ";
cout<<endl;
cout<<"Least steps: " <<cost;
cout<<endl;

int *board2= new int[size2];
board2[0]=0;
board2[1]=5;
board2[2]=17;
board2[3]=29;
board2[4]=61;
board2[5]=53;
board2[6]=100;
board2[7]=2;
int cost2 = 0;
cost2 = lowestCost(board2,0,cost2,size,size2);
for(int i=0;i < 8;i++)
cout<<board2[i]<<" ";
cout<<endl;
cout<<"Least steps: " <<cost2;
}

OUTPUT:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW

PLEASE GIVE A THUMBS UP


Related Solutions

C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: intQ1_for() intQ1_while() intQ1_do() To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in three different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value on the screen. Return the total sum at the...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character. Example: Input of “Hello,...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the for loop, and selection. Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. If the user enters a 0 or a negative number the program should exit with a message to the user indicating they chose to exit. If...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT