Question

In: Computer Science

Q1 Create a program that asks the user to input their budget. Then, it will ask...

Q1

Create a program that asks the user to input their budget. Then, it will ask the user to input each of their purchase until their entire budget is used up.

What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).

Note: Assume that the user always uses up their budget to the last cent and they don't over spend (is that really possible? :D )

You can use the following screen output to guide your program.

Please enter your budget: $2500.25
Please enter your purchase amount: $1200
Remaining balance: $1300.25
Please enter your purchase amount: $300.25
Remaining balance: $1000.00
Please enter your purchase amount: $1000.00
Remaining balance: $0.00

Skeleton code:

#include <iostream>
#include <iomanip>
int main() {
  double budget = 0, purchase = 0;
  std::cout << std::fixed << std::setprecision(2);
  std::cout << "Please enter your budget: $";
  std::cin >> budget;
  // place code here
  return 0;
}

Q2

Create a program that computes the summation of a number. For example, the summation of 3 would be 1 + 2 + 3.

What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).

You can use the following screen output to guide your program.

summation(n) program
Please enter n: 3
The summation(3) is 6
Skeleton code:
 
 
#include <iostream>
#include <iomanip>
int main() {
  int summation = 0, n = 0;
  std::cout << "summation(n) program";
  std::cout << "Please enter n: ";
  std::cin >> n;
  // provide code here
  std::cout << "summation("<< n << ") is: " << summation << "\n";
  return 0;
}

Q3

Question text

Create a program that asks the user to add all the values that a user inputs until they input -1.

What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).

Note: Be careful not to add the extra -1.

You can use the following screen output to guide your program.

Welcome to Summer-upper!
Please enter a number you want to add (type -1 to exit): 10
Please enter a number you want to add (type -1 to exit): 3
Please enter a number you want to add (type -1 to exit): 11
Please enter a number you want to add (type -1 to exit): -1
Sum is: 24
Skeleton code:
 
#include <iostream>
int main() {
  int sum = 0, input = 0;
  std::cout << "Welcome to Summer-upper!\n";
  // provide code here
  std::cout << "Sum is: " << sum << "\n";
  return 0;
}

Solutions

Expert Solution

q1. I think do-while is more appropriate for solving the given problem statement. The reason is, do-while runs once even if the condition is false, which in this case is required.

Following is the code.

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

int main() {
double budget = 0, purchase = 0;
cout << fixed << setprecision(2);
cout << "Please enter your budget: $";
cin >> budget;
// place code here
do {
cout << "Please enter your purchase amount: $";
cin >> purchase;
double balance = budget - purchase;
budget = balance;
   cout << "Remaining balance: $"<<balance<<"\n";
}while(budget > 0);
return 0;
}
output q1.:

Q2: For this question, I just need to calculate the summation of the numbers till n. For loop is ideal for such scenarios.
code:

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

int main() {
int summation = 0, n = 0;
cout << "summation(n) program";
cout << "Please enter n: ";
cin >> n;
// provide code here
for(int i=0;i<n;i++){
   summation += (i+1);
}

cout << "summation("<< n << ") is: " << summation << "\n";
return 0;
}

output:

q3. For this case both do-while and while loop are ideal. But I prefer, while loop to solve this problem.
#include <iostream>
using namespace std;
int main() {
int sum = 0, input = 0;
std::cout << "Welcome to Summer-upper!\n";
// provide code here
while(input >=0){
   cout << "Please enter a number you want to add (type -1 to exit):";
   cin >> input;
   if(input > -1)
       sum += input;
}
cout << "Sum is: " << sum << "\n";
return 0;
}

output:


Let me know if you need any further help.


Related Solutions

Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
This assignment asks to create an array with 10 integer elements. Ask user to input 10...
This assignment asks to create an array with 10 integer elements. Ask user to input 10 integer values. going through all the array elements and find/print out all the prime numbers. Instructions: Only use for loops for this lab (no need to create methods or classes for this assignment). Put a comment on each line of your codes, explaining what each line of code does. basic java, please
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
Create a program that asks the user for two positive integers. If the user's input for...
Create a program that asks the user for two positive integers. If the user's input for either integer is not positive (less than or equal to 0), re-prompt the user until a positive integer is input. You may assume that all inputs will be integers. Print out a list, ascending from 1, of all divisors common to both integers. Then, print out a message stating whether or not the numbers are "relatively prime" numbers. Two positive integers are considered relatively...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Q1) Create a program that do the following: 1. Asks the user to enter n marks...
Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for a given mark and prints the...
Q1) Please create a C++ program that will ask the user for how many test scores...
Q1) Please create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name, Wayne State Access ID, midterm score and their favorite team (i.e. – Doug, hg1702, 92, Wolverines * - John, hv2201, 99, Warriors). Print out the completed test scores to a file (midTermScores.txt) . Adjust the output as follows: (15 points) a. Scores with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT