Question

c++ Please give a line by line explanation fo the design of this code (what is...

c++

Please give a line by line explanation fo the design of this code (what is happening per line to make this program work)

int ways(int amt,int* l,int n,int* res){
if (amt < 0 || (amt > 0 && n <= 0)) return 0;
if (amt == 0){
string s = "";
int j = 0;
if (res[0] > 0){
j = 1;

cout << res[0] << " quarter/s, ";
}
if (res[1] > 0) {
j = 1;
cout << res[1] << " dime/s, ";
}
if (res[2] > 0){
j = 1;
cout << res[2] << " nickel/s, ";
}
if (res[3] > 0){
if (j == 1) cout << "and " <<res[3] << " pennies";
else cout << res[3] << " pennies";
}
cout << endl;
return 1;
}
int* temp = new int[4];
for (int i = 0; i < 4; i++)
temp[i] = res[i];
int x = ways(amt,l,n-1,temp);
temp[n-1] += 1;
int y = ways(amt-l[n-1],l,n,temp);
return x+y;
}

int main(){
int amt;
cout << "Enter the amount of pennies: ";
cin >> amt;
cout << "These are the combinations: " "\n";
int* l = new int[4];
l[0] = 25; l[1] = 10; l[2] = 5; l[3] = 1;

int* res = new int[4];
res[0] = 0; res[1] = 0; res[2] = 0; res[3] = 0;
cout << endl << "The total amount of different combinations: " << ways(amt,l,4,res) << endl;
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: Please up-vote if you find this solution useful in any way.

Info:

1 nickel = 5 pennies

1 dime = 10 pennies

1 quarter = 25 pennies

Description of program:

The program has a recursive function called ways which are calculating the combination in recursive ways. In basic terms, when you enter the amount of pennies as input then the program print the combinations in which the given pennies can be divided into nickel, dime and quarters and also the count of those combinations. (Combinations are the ways to group numbers).

CODE:

#include <iostream>
#include <string>
using namespace std;

int ways(int amt,int* l,int n,int* res){
  
//if the amount is 0 or the size of array l is 0, function will return 0
if (amt < 0 || (amt > 0 && n <= 0))
return 0;
  
// When the amount becomes zero or we can say that this program will enter in this condition for every recursive call
// where the current amount becomes zero.
// Also, when in a recursive call, the current amount becomes 0, the function uses the res value which is the current value
// of current temp pointer array.
// The if condition checks the index of res if any index value of res is > 0 then that index value is printed with a unit string
// After printing, the program returns 1 which is then added to the count of the combination of pennies.
if (amt == 0){
  
   // This string variable is not used anywhere in the program
string s = "";
  
   // This variable is used to use in the last if condition, to check whether above if conditions are executed or not
   // that means, quarter's, dime's or nickel are possible for the given amount.
int j = 0;
  
   //First if condition to check if quarters are possible for the given amount
if (res[0] > 0){
j = 1;
cout << res[0] << " quarter/s, ";
}

   //Second if condition to check if dime's are possible for the given amount
if (res[1] > 0) {
j = 1;
cout << res[1] << " dime/s, ";
}

   //Third if condition to check if nickel's are possible for the given amount
if (res[2] > 0){
j = 1;
cout << res[2] << " nickel/s, ";
}

   //Last if condition to check the remaining pennies
if (res[3] > 0){
  
   //When above if conditions are true, then it will print " and ___ pennies"
if (j == 1)
cout << "and " <<res[3] << " pennies";
   //When above if conditions are true, then it will print " ___ pennies"
else
cout << res[3] << " pennies";
}
  
   //For ending the current line
   cout << endl;

   //return statement
return 1;
}
  
// Creating a pointer of arrays temp
int* temp = new int[4];
  
// Copying the res array to temp array
for (int i = 0; i < 4; i++)
temp[i] = res[i];
  
// This variable runs, for the count of n
int x = ways(amt,l,n-1,temp);
  
// Every time the function is called when amount is non-zero, the value of index of temp is increased by 1
temp[n-1] += 1;
  
// This variable checks whether the current amount can be converted to any unit of a dollar
// amt - l[n-1] subtracts the current amount with the unit amount like a quarter, dime, nickel etc, and checks
// if it current amount is equal to or greater than the unit
int y = ways(amt-l[n-1],l,n,temp);
  
// returning the sum of x+y
return x+y;
}

// Main function
int main(){
  
int amt;

cout << "Enter the amount of pennies: ";
cin >> amt;
  
cout << "These are the combinations: " "\n";
// pointer array which stores the amounts of units of dollar
int* l = new int[4];
l[0] = 25; l[1] = 10; l[2] = 5; l[3] = 1;
  
// Another pointer array which is used in way() to store the count of units in every recursive call.
int* res = new int[4];
res[0] = 0; res[1] = 0; res[2] = 0; res[3] = 0;

cout << endl << "The total amount of different combinations: " << ways(amt,l,4,res) << endl;
  
return 0;
}

// Code ends here!!!!!!!!!!

NOTE: Please up-vote.

Add a comment
Know the answer?
Add Answer to:
c++ Please give a line by line explanation fo the design of this code (what is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • C++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • THE CODE CALCULATES THE MEAN MEDIAN AND MODE OF THE SET OF NUMBERS THE CODE IS...

    THE CODE CALCULATES THE MEAN MEDIAN AND MODE OF THE SET OF NUMBERS THE CODE IS WRITTEN IN C++ PLEASE FIX THE MODE FUNCTION ! SO THAT IT OUTPUTS 0 WHEN THERE ARE NO REPETITIONS IN THE DATASET eg. for 1,2,3,4,5 mode should be zero but it gives 1 ! #include<iostream> #include<math.h> using namespace std; class statistical{ protected: double *dataArray; int size; public: statistical(double a[], int s){ dataArray = new double[s]; for (int i = 0; i<s; i++){ dataArray[i] =...

  • points): Show the output of the code below as it would appear on the monitor. int...

    points): Show the output of the code below as it would appear on the monitor. int main cout <<" <<endl: int wildcat 2: while (wildcat > 5) cout << wildcat <<endl; wildcat++ cout <K*<< endl; int jayhavk 5i do cout << jayhawk <s endl: while (jayhawk0) cout <<*" << endl; int wolverine l: while (wolverine 0 &&wolverine < 10) cout << wolverine <<endl: wolverine2: cout <<" <<endl: for (int zag-4; zag>0; zag_) cout <<****" << endl; for (int i-10; i<-30;...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream>...

    C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream> template <typename T, int N=10> class Array { private:   T array[N+1]; public:   Array() {     for(int i=0; i<N+1; i++) array[i] = 0;   }      T& operator [] (int index) {     if (index>N || index < 0)       return array[N];     else       return array[index];   }   template <typename S>   Array<T,N>& operator= (S &other) {     for(int i=0; i<N+1; i++)       array[i] = other[i];         return *this;   } }; template <typename T, typename...

  • Can I get help with adding the following to this code please? 1. When buying multiple...

    Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT