Question

In: Computer Science

(C++) In a file called pp7c.cpp, write a function called printMoney that has one parameter, a...

(C++)
In a file called pp7c.cpp, write a function called printMoney that has one parameter, a double, and it prints this parameter out formatted like a dollar amount with $ and exactly 2 digits to the right of the decimal. Write a driver that declares an array of monetary amounts like this:
double amounts[MAX_AMOUNTS];
and uses a while or do while loop to ask the user for monetary amounts with -1 for the amount as a sentinel value to put values in the array, amounts. Do not allow the user to enter more than MAX_AMOUNTS numbers. This loop must count how many numbers are placed in the amounts array as the array may be partially filled. After using a do or do while loop to fill the array, write a for loop to call the printMoney function to print all of the values in the amounts array. The constant and function declarations are shown below.

                const int MAX_AMOUNTS = 10;

void printMoney( double m );

Solutions

Expert Solution

Explanation:

Here is the code below which has the function printMoney and in the main , it keeps asking for the monetory amount until the person enters -1 as the sentinel value or the amounts exceed 10.

Then a for loop is used to call the function printMoney for each amount in the array.

Code:


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

void printMoney(double m)
{
cout<<fixed<<setprecision(2)<<"$"<<m<<endl;
}
int main()
{
const int MAX_AMOUNTS = 10;
  
double amounts[MAX_AMOUNTS];
  
int n = 0;
double amount;
  
  

while(n<10)
{
cout<<"Enter monetory amount: ";
cin>>amount;
  
if(amount==-1)
break;
  
amounts[n++] = amount;
  
}
  
for (int i = 0; i < n; i++) {
  
printMoney(amounts[i]);
}
return 0;
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
write a C++ function called inc whose parameter is an integer reference type and that increases...
write a C++ function called inc whose parameter is an integer reference type and that increases the parameter by one when it is called.
- Write a function with a parameter called A (which is between 1 and 10) and...
- Write a function with a parameter called A (which is between 1 and 10) and prints the multiplication table for 1 to A. • Note: Do not need to draw the horizontal and vertical lines. • Example for A = 10.
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT