For the ideas time ONLY (dont bid)
1
ECE 175: Computer Programming for Engineering Applications Homework Assignment 2
Conventions: Name your C programs as hwxpy.c where x corresponds to the homework number and y corresponds to the problem number. For example, the C program for homework 2, problem 1 should be named as hw2p1.c.
Write comments to your programs. Programs with no comments will receive PARTIAL credit. For each program that you turn in, at least the following information should be included at the top of the C file:
- Author: - Date created: - Brief description of the program: - input(s): - output(s): - brief description or relationship between inputs and outputs
Submission Instructions: Use the designated Dropbox on D2L to submit your homework. Submit only the .c files.
Problem 1 (35 points): You are working at the music store whose owner wants you to write a C program to implement a discount policy and display the receipt in a certain format. Below is the table containing the customer type and percentage discount.
Customer Total purchase Discount percentage
Music Teacher ≤ $ 100.0 10 > $ 100.0 12
Not Teacher
6
The discount calculation is performed before the addition of the 5% sales tax. The C program should let a user enter whether he/she is a music teacher and the total purchase and then display the receipt as shown in the sample code execution below. Sample Code Execution #1 (Y, 100 are entered by a user) Are you a music teacher (Y or N)?: Y Enter total purchase ($): 100
Total purchase $100.00 Teacher discount (10%) 10.00 Total after discount 90.00 Sales tax (5%) 4.50 Total $94.50 Sample Code Execution #2 (Y, 145.56 are entered by a user) Are you a music teacher (Y or N)?: Y Enter total purchase ($): 145.56
Total purchase $145.56 Teacher discount (12%) 17.47 Total after discount 128.09 Sales tax (5%) 6.40 Total $134.50
2
Sample Code Execution #3 (N, 24.90 are entered by a user) Are you a music teacher (Y or N)?: N Enter total purchase ($): 24.90
Total purchase $24.90 Non-Teacher discount (6%) 1.49 Total after discount 23.41 Sales tax (5%) 1.17 Total $24.58
Test cases: (Teacher, total purchase) Below are answers that your program should give Discount total after discount sales tax total (Y, 54.43) 5.44 48.99 2.45 51.44 (N, 54.43) 3.27 51.16 2.56 53.72 (Y, 175.39) 21.05 154.34 7.72 162.06 (N, 175.39) 10.52 164.87 8.24 173.11 Problem 2 (35 points): Write a program that computes the roots of a quadratic equation ax2 + bx + c = 0, which are given by the following formulas:
𝑥𝑥1 = −𝑏𝑏+√𝑏𝑏2−4𝑎𝑎𝑎𝑎
2𝑎𝑎 and 𝑥𝑥2 =
−𝑏𝑏−√𝑏𝑏2−4𝑎𝑎𝑎𝑎 2𝑎𝑎
These formulas cannot be used if the value of a = 0, or if the value 𝑏𝑏2 − 4𝑎𝑎𝑎𝑎 < 0 (complex roots).
For the case a = 0 and b ≠ 0, there is only one root which is 𝑥𝑥 = − 𝑎𝑎 𝑏𝑏
For the case a = 0 and b = 0, there is no root. For the case 𝑏𝑏2 − 4𝑎𝑎𝑎𝑎 < 0, there is no real root.
Your program should ask the user to input the coefficients a, b, c of the quadratic equation and display the corresponding solution or appropriate messages. You can use the math library function sqrt(x) to compute the square root of a number x. Examples of Code Execution (user’s entries in red): Enter the coefficients a, b, c of ax^2 + bx + c: 0 0 -1.25 your equation has no root. Enter the coefficients a, b, c of ax^2 + bx + c: 0 -2.5 7.8 One root of -2.50x + 7.80 is 3.120. Enter the coefficients a, b, c of ax^2 + bx + c: 0 4.25 154 One root of 4.25x + 154.00 is -36.235. Enter the coefficients a, b, c of ax^2 + bx + c: 3.5 6 2.4 Roots of 3.50x^2 + 6.00x + 2.40 are -0.636 and -1.078. Enter the coefficients a, b, c of ax^2 + bx + c: 2 5 4 There are no real roots. Enter the coefficients a, b, c of ax^2 + bx + c: 10.75 4.25 0 Roots of 10.75x^2 + 4.25x + 0.00 are 0.000 and -0.395 Lab 2 (30 points): Complete this assignment when you attend your lab session after HW 2 is due.