C++ coding hw Part A. Code a for loop to print the Celsius temperatures for Fahr
ID: 3890030 • Letter: C
Question
C++ coding hw
Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32). Output should be in a table format:
Fahrenheit Celsius
25 degrees
. .
. . . .
. .
Turn in the source and the output from Part A.
Part B. Code a while loop to read in and total any number of positive integers terminated with a -1 as a sentinel. Print the average of the values after the loop is finished. Run with at least 4 or 5 numbers – make some of them negative numbers which should not be included in the total or average. The approach should be:
1. Define total and count and set them to 0
2. Read in a number
3. While number != -1
3.1 If number >0
3.1.1 Total += number
3.1.2 Count ++
3.2 Read in another number
4. Print the average of the positive numbers
5. Stop
Turn in the source and the output from Part B.
Part C. Code a do while loop to make change for a customer. Input the amount of money put into a vending machine. Change that value to cents. Input the amount of the purchase. Change that value to cents. Subtract the two values. Determine the customer’s change in number of quarters, dimes, nickels, and pennies, using integer arithmetic. Hint:
cents / 25 = number of quarters
cents % 25 = remaining cents to change into dimes, nickels, etc.
Keep making change for different purchases until the user tells you to stop. Make change for 3 or 4 purchases. Turn in the source and 3 or 4 outputs from Part C.
Will rate (:
Explanation / Answer
Given below is the code for the different parts. Please do rate the answer if it helped. Thank you.
partA.cpp
#include <iostream>
using namespace std;
int main()
{
double C;
cout << "Farenheit Celsius" << endl;
for(double F = 25; F <= 125; F += 1) //change F from 25 to 125 in steps of 1
{
C = 5.0 / 9 * (F - 32); //calculate C based on value of F, use 5.0 instead of 5, to perform double division
cout << F << " " << C << endl; //display F and C
}
}
output
Farenheit Celsius
25 -3.88889
26 -3.33333
27 -2.77778
28 -2.22222
29 -1.66667
30 -1.11111
31 -0.555556
32 0
33 0.555556
34 1.11111
35 1.66667
36 2.22222
37 2.77778
38 3.33333
39 3.88889
40 4.44444
41 5
42 5.55556
43 6.11111
44 6.66667
45 7.22222
46 7.77778
47 8.33333
48 8.88889
49 9.44444
50 10
51 10.5556
52 11.1111
53 11.6667
54 12.2222
55 12.7778
56 13.3333
57 13.8889
58 14.4444
59 15
60 15.5556
61 16.1111
62 16.6667
63 17.2222
64 17.7778
65 18.3333
66 18.8889
67 19.4444
68 20
69 20.5556
70 21.1111
71 21.6667
72 22.2222
73 22.7778
74 23.3333
75 23.8889
76 24.4444
77 25
78 25.5556
79 26.1111
80 26.6667
81 27.2222
82 27.7778
83 28.3333
84 28.8889
85 29.4444
86 30
87 30.5556
88 31.1111
89 31.6667
90 32.2222
91 32.7778
92 33.3333
93 33.8889
94 34.4444
95 35
96 35.5556
97 36.1111
98 36.6667
99 37.2222
100 37.7778
101 38.3333
102 38.8889
103 39.4444
104 40
105 40.5556
106 41.1111
107 41.6667
108 42.2222
109 42.7778
110 43.3333
111 43.8889
112 44.4444
113 45
114 45.5556
115 46.1111
116 46.6667
117 47.2222
118 47.7778
119 48.3333
120 48.8889
121 49.4444
122 50
123 50.5556
124 51.1111
125 51.6667
partB.cpp
#include <iostream>
using namespace std;
int main()
{
double total = 0;
int count = 0;
double num;
cout << "Enter a number(-1 to stop): " ;
cin >> num;
while(num != -1) //check if number entered is -1, if it is stop
{
if(num > 0) //check if its a +Ve number
{
total += num;
count++;
}
cout << "Enter a number(-1 to stop): " ;
cin >> num;
}
double avg = total / count;
cout << "Average = " << avg << endl;
}
output
Enter a number(-1 to stop): 4
Enter a number(-1 to stop): 3
Enter a number(-1 to stop): 0
Enter a number(-1 to stop): -2
Enter a number(-1 to stop): -4
Enter a number(-1 to stop): 5
Enter a number(-1 to stop): -1
Average = 4
partC.cpp
#include <iostream>
using namespace std;
int main()
{
double inputAmount, purchaseAmount;
int inputCents, purchaseCents, returnChange;
int quarters, dimes, nickles, pennies;
char ans;
do
{
cout << "Enter amount input into vending machine: ";
cin >> inputAmount;
inputCents = inputAmount * 100; //convert to cents
cout << "Enter purchase amount: ";
cin >> purchaseAmount;
purchaseCents = purchaseAmount * 100; //convert into cents
returnChange = inputCents - purchaseCents;
quarters = returnChange / 25; //calculate no. of quarters
returnChange = returnChange % 25; //calculate the remain amount after quarters is removed
dimes = returnChange / 10; //calculate no. of dimes
returnChange = returnChange % 10; //calculate how much is remaining after removing dimes
nickles = returnChange / 5; //calculate number of nickles
pennies = returnChange % 5; //calculate no. of pennies, i.e after removing nickles
cout << "Change: " << quarters << " quarters, " << dimes << " dimes, ";
cout << nickles << " nickles, " << pennies << " pennies" << endl;
cout << "Calculate another? y/n: ";
cin >> ans;
}while(ans == 'y' || ans == 'Y');
cout << "Good Bye" << endl;
}
output
Enter amount input into vending machine: 5
Enter purchase amount: 1.99
Change: 12 quarters, 0 dimes, 0 nickles, 1 pennies
Calculate another? y/n: y
Enter amount input into vending machine: 3
Enter purchase amount: 2.45
Change: 2 quarters, 0 dimes, 1 nickles, 0 pennies
Calculate another? y/n: n
Good Bye
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.