Starting from:

$24.99

NYU-CS Homework #3 Solution



Submission instructions:
1. For this assignment you should turn in 7 files:
• Six ‘.cpp’ files, one for each question 1 to 6.
Name your files ‘YourNetID_hw3_q1.cpp’, ‘YourNetID_hw3_q2.cpp’, etc.
• A ‘.pdf‘ file with your answers for questions 7-11. Each question should start on a new page!
Name your file ‘YourNetID_hw3_q7to11.pdf’

2. Typing your solutions would grant you 5 extra points.

3. You should submit your homework in the Gradescope system.
Note that when submitting the pdf file, you would be asked to assign the pages from your file to their corresponding questions.

4. You can work and submit in groups of up to 4 people. If submitting as a group, make sure to associate all group members to the submission on gradescope.

5. For the coding questions, pay special attention to the style of your code. Indent your code correctly, choose meaningful names for your variables, define constants where needed, choose the most appropriate control flow statements, etc.

6. For the math questions, you are expected to justify all your answers, not just to give the final answer (unless explicitly asked to).
As a rule of thumb, for questions taken from zyBooks, the format of your answers, should be like the format demonstrated in the sample solutions we exposed. 1:
Write a program that computes how much a customer has to pay after purchasing two items. The price is calculated according to the following rules:
• Buy one get one half off promotion: the lower price item is half price.
• If the customer is club card member, additional 10% off.
• Tax is added.

Inputs to the program include:
• Two items’ prices
• Have club card or not (User enters ‘Y’ or ‘y’ for “yes”; ‘N’ or ‘n’ for “no”)
• Tax rate (User enters the percentage as a number; for example they enter 8.25 if the tax rate is 8.25%)

Program displays:
• Base price - the price before the discounts and taxes
• Price after discounts - the price after the buy one get one half off promotion and the member’s discount, if applicable
• Total price – the amount of money the customer has to pay (after tax).

Your program should interact with the user exactly as it shows in the following example:
Enter price of first item: 10
Enter price of second item: 20
Does customer have a club card? (Y/N): y
Enter tax rate, e.g. 5.5 for 5.5% tax: 8.25
Base price: 30.0
Price after discounts: 22.5
Total price: 24.35625



Question 2:
Write a program that:
• Asks the user for their name.
• Asks the user to input their graduation year.
• Asks the user to input the current year.
Note: If graduation year equals to current year, status is ‘Graduated’; if graduation year is four years after current year, status is ‘Freshman’, etc.
Your program should interact with the user exactly as it shows in the following example:
Please enter your name: Jessica
Please enter current year: 2015
Jessica, you are a Freshman
3:
Write a program that does the following:
• Ask user to input three Real numbers a, b and c. They represent the parameters of a quadratic equation 𝑎𝑥! + 𝑏𝑥 + 𝑐 = 0
• Classify to one of the following:
- ’Infinite number of solutions’ (for example, 0𝑥! + 0𝑥 + 0 = 0 has infinite number of solutions)
- ’No solution’ (for example, 0𝑥! + 0𝑥 + 4 = 0 has no solution)
- ’No real solution’ (for example, 𝑥! + 4 = 0 has no real solutions)
- ’One real solution’
- ’Two real solutions’
• In cases there are 1 or 2 real solutions, also print the solutions.

Notes:
1. If 𝑎 ≠ 0 and there are real solutions to the equation, you can get these solutions using the following formula:
−𝑏 ± √𝑏! − 4𝑎𝑐
𝑥",! = 2𝑎
The number of solutions depends on whether (b is positive, zero, or negative.

2. In order to calculate the square root of a number (of type double), you should call the sqrt function, located in the cmath library.

Follow the syntax as demonstrated in the code below:
#include <iostream> #include <cmath> using namespace std;
int main() { double x = 2.0; double sqrtResult;

sqrtResult = sqrt(x); cout<<sqrtResult<<endl;

return 0;
}
Note that you first need to include the cmath library, and then you can call the sqrt function, passing the argument that you want to calculate the square root of, enclosed in parentheses.

Your program should interact with the user exactly as it shows in the following example:
Please enter value of a: 1
Please enter value of b: 4
Please enter value of c: 4
This equation has a single real solution x=-2.0


4:
Define the following constants:
const int FLOOR_ROUND = 1; const int CEILING_ROUND = 2;
const int ROUND = 3;

Write a program that asks the user to enter a Real number, then it asks the user to enter the method by which they want to round that number (floor, ceiling or to the nearest integer). The program will then print the rounded result.

Your program should interact with the user exactly as it shows in the following example: Please enter a Real number:
4.78
Choose your rounding method:
1. Floor round
2. Ceiling round
3. Round to the nearest whole number
2
5

Implementation requirements:
1. Use a switch statement.
2. You are not allowed to include and use the math library.

Question 5:
Body mass index (BMI) is a number calculated from a person’s weight and height using the following formula: 𝑤𝑒𝑖𝑔ℎ𝑡/ℎ𝑒𝑖𝑔ℎ𝑡!. Where 𝑤𝑒𝑖𝑔ℎ𝑡 is in kilograms and ℎ𝑒𝑖𝑔ℎ𝑡 is in meters.

According to the Centers for Disease Control and Prevention, the BMI is a fairly reliable indicator of body fatness for most people. BMI does not measure body fat directly, but research has shown that BMI correlates to direct measures of body fat, such as underwater weighing and dual-energy X-ray absorptiometry.

The following table gives the weight status in respect to the BMI value:
BMI Range Weight Status
Below 18.5 Underweight
[18.5, 25) Normal
[25, 30) Overweight
30 and above Obese

Write a program that prompts for weight (in pounds) and height (in inches) of a person, and prints the weight status of that person.

Your program should interact with the user exactly as it shows in the following example:
Please enter weight (in pounds): 135
Please enter height (in inches): 71 The weight status is: Normal
Note: 1 pound is 0.453592 kilograms and 1 inch is 0.0254 meters.


Question 6:
The output will be the cost of the call.
Notes:
1. The time is to be input in 24-hour notation, so the time 1:30 P.M. is input as 
13:30
2. The day of the week will be read as one of the following two character string: Mo Tu We Th Fr Sa Su
3. The number of minutes will be input as a positive integer.


Question 7:
Solve the following questions from the Discrete Math zyBook: a) Exercise 3.1.1, sections a-g
b) Exercise 3.1.2, sections a-e
c) Exercise 3.1.5, sections b, d
d) Exercise 3.2.1, sections a-k

Question 8:
Solve Exercise 3.2.4, section b from the Discrete Math zyBook.

Question 9:
Solve the following questions from the Discrete Math zyBook: a) Exercise 3.3.1, sections c-e
b) Exercise 3.3.3, sections a, b, e, f
c) Exercise 3.3.4, sections b, d

Question 10:
Solve the following questions from the Discrete Math zyBook: a) Exercise 3.5.1, sections b, c
b) Exercise 3.5.3, sections b, c, e
c) Exercise 3.5.6, sections d, e
d) Exercise 3.5.7, sections c, f, g

Question 11:
Solve the following questions from the Discrete Math zyBook: a) Exercise 3.6.2, sections b, c
b) Exercise 3.6.3, sections b, d
c) Exercise 3.6.4, sections b, c

More products