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;
}
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.
c++ Please give a line by line explanation fo the design of this code (what is...
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...
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,...
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 <<...
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;...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record { int age; string name; }; int check(record r[], int n, string nm) { for (int i = 0; i < n;...
C++ Check Book Program I need to have a checkbook program that uses the following items. My code that I have appears below. 1) Math operations using built-in math functions 2) Class type in a separate .h file with member functions (accessor functions, get, set, show, display find, etc). There needs to be an overloaded function base/derived classes or a template. 3) Binary File #include <iostream> using namespace std; int main() { double checking,savings; cout<<"Enter the initial checking...
(Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth. **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...
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;...
Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...