In: Computer Science
Language is C++
NOTE:
No arrays should be used to solve problems and No non-constants
global variables should be used.
PART A: (Minusculo Inn)
Minusculo Inn has only eight guest rooms:
Room Number | Amenities |
---|---|
101 | 1 king size bed |
102, 103,104 | 2 double beds |
201, 202 | 1 queen size bed |
203, 204 | 1 double bed & sofa bed |
Write a program that asks for the room number and displays the amenities. If the user enters a room number that does not exist, the program should display “Input error”.
You must use a switch statement in your program to solve
this problem.
No Repetition Statements is needed in this problem. A User is only
making one selection.
The Minusculo Inn Restaurant has 4 lunch combos for customers to choose:
Combo A : Fried chicken with slaw [price:
$4.25]
Combo B : roast beef with mashed potato [price:
$5.75]
Combo C : Fish and chips [price: $5.25]
Combo D : soup and salad [price: $3.75]
Write a program to calculate how much a party of customers should pay. Suppose the party orders 2 B’s, 1 A and 3 D’s. The casher will enter B, B, A, D, D, D and T into the program. The T means “calculating total of the order”. It is a signal telling the computer that the whole order has been entered and it is time to calculate the total amount the party should pay. Assume there is no sales tax. The following is an example:
Enter item ordered [A/B/C/D] or T to calculate
total: B
Enter item ordered [A/B/C/D] or T to calculate total:
B
Enter item ordered [A/B/C/D] or T to calculate total:
A
Enter item ordered [A/B/C/D] or T to calculate total:
D
Enter item ordered [A/B/C/D] or T to calculate total:
D
Enter item ordered [A/B/C/D] or T to calculate total:
D
Enter item ordered [A/B/C/D] or T to calculate total:
T
Please pay this amount: $ 27
PART B: (Statistics program)
Write a program that reads the integers in the text file (user must enter name of file) and:
Displays the number of integers in the file (5 %)
Displays the number of even and odd integers (5%)
Displays the sum of the even integers and the sum of the odd
integers as well as the sum of all the integers (5%)
Displays the largest integer and the smallest integer (5 %)
Computes the average of the largest and smallest integer (10
%)
Compute the sum of ALL the digits in the input file (i.e. 123 475 –
Sum is 1+2+3+4+7+5 = 22) (15 %)
Assume the contents of the file are as follows:
123 475 61 77 910
234 138 134 95 674
345 31 211 952 873
22 7 876 347 450
The following is a sample output: User input is the 'integers.dat'
What is name of the input file?
integers.dat
The number of integers in the file is 20
There are 11 odd integers and 9 even integers
The sum of the odd integers is 2645
The sum of the even integers is 4390
The sum of all the integers is 7035
The largest integer is 952
The smallest integer is 7
The average of 952 and 7 is 479.5
The sum of all the digits is 212
NOTE:
Use two decimal point precision for the average
To check your code, adjust one of the integers in the file, recompile your code and run again (suggest changing the largest and smallest integers )
Even though there are 20 integers in the file, the program should NOT presume there are 20 integers. In other words, you should not solve this problem by declaring 20 integer variables. If I choose to use another integers.dat file other than the one provided with the assignment, your program should still work correctly with that file as well. All you can assume is that a series of integers will be read in from the file. Therefore, you need to implement a solution that uses the repetition control structures (for, while, do…while) in the reading and processing of these values to produce your output
Part A: (Minusculo Inn)
Write a program that asks for the room number and displays the amenities
main.cpp
//========================================================
#include<iostream>
using namespace std;
int main()
{
int room_number;
cout << "Please choose one room number between
101 to 104 and 201 to 204 : ";
cin >> room_number; // read
user input
switch (room_number) // switch
statement on room_number
{
case 101: {cout << "Amenities: 1 king size bed"
<< endl; break; }
case 102: {cout << "Amenities: 2 double beds"
<< endl; break; }
case 103: {cout << "Amenities: 2 double beds"
<< endl; break; }
case 104: {cout << "Amenities: 2 double beds"
<< endl; break; }
case 201: {cout << "Amenities: 1 queen size bed"
<< endl; break; }
case 202: {cout << "Amenities: 1 queen size bed"
<< endl; break; }
case 203: {cout << "Amenities: 1 double bed
& sofa bed" << endl; break; }
case 204: {cout << "Amenities: 1 double bed
& sofa bed" << endl; break; }
default: {cout << "Input error" << endl;
break; }
}
system("pause");
}
//========================================================
Sample Output 1:
Sample Output 2:
Sample Output 3:
//---------------------------------------------------------------------------------------------------------------------------------------
Write a program to calculate how much a party of customers should pay
main.cpp
//===========================================================
#include<iostream>
using namespace std;
int main()
{
char item;
float total =0.0;
while (1) // read until user don't want total(T)
{
cout << "Enter item ordered
[A/B/C/D] or T to calculate total: ";
cin >> item; // read item
provided by customer
if (item == 'T') break;
// Customer wants total
else
{
switch
(item)
{
case 'A': {
total += 4.25; break; } // A is order amount is added in
total
case 'B': {
total += 5.75; break; } // B is order amount is added in
total
case 'C': {
total += 5.25; break; } // C is order amount is added in
total
case 'D': {
total += 3.75; break; } // D is order amount is added in
total
default:
break;
}
}
}
cout << "Please pay this amount: $ " <<
total << endl;
system("pause");
}
//===========================================================
Sample input/output 1:
Sample input/output 2:
Part B: (Statistics program)
main.cpp
//======================================================================
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string file_name;
cout << "What is name of the input file?
";
cin >>
file_name; // read file
name
ifstream in(file_name); // open user file
int count_num = 0;
// count total number
int count_even = 0; //
count even numbers
int count_odd = 0;
// count odd numbers
int sum_even =
0; // sum of even
numbers;
int sum_odd =
0; // sum of odd
numbers;
int sum_num =
0; // total
sum;
int max = INT_MIN;
// maximum number assigned minimum possible integer
int min = INT_MAX;
// minimum number assigned maximum possible integer
int digit_sum = 0;
// sum of all digit
int number;
if (in.fail())
{
cout << "File is not opened
succesfully" << endl;
}
while
(!in.eof()) //
while loop till eof doesn't hit
{
in >>
number; // reading
from file
count_num++;
sum_num +=
number; // total sum calculation
//even number
if (number % 2 == 0) // checking
even number
{
count_even++;
sum_even +=
number;
}
else
{
count_odd++;
sum_odd +=
number;
}
if (max < number) max =
number; // maximum number finding
if (min > number) min =
number; // minimum number finding
if (number < 0) number =
-number; // negative to positive for digit sum
while (number
!=0)
// digit sum calculation
{
digit_sum +=
number % 10;
number /=
10;
}
}
cout << "The number of integers in the file is "
<< count_num << endl;
cout << "There are " << count_odd <<
" odd integers and " << count_even << " even integers"
<< endl;
cout << "The sum of the odd integers is "
<< sum_odd << endl;
cout << "The sum of the even integers is "
<< sum_even << endl;
cout << "The sum of all the integers is "
<< sum_num << endl;
cout << "The largest integer is " << max
<< endl;
cout << "The smallest integer is " << min
<< endl;
cout << "The average of " << max <<
" and " << min << " is " << (max + min) / 2.0
<< endl;
cout << "The sum of all the digits is " <<
digit_sum << endl;
system("pause");
}
//======================================================================
Sample input 1: 1.dat
123 475 61 77 910
234 138 134 95 674
345 31 211 952 873
22 7 876 347 450
Sample output 1:
Sample input 2: 1.dat
123 475 61 77 910
234 138 134 95 674
345 31 211 952 873
22 7 876 347 450
982 1
Sample output 2:
// tested with negative integer case
Sample input 3: 1.dat
32 -45 65 451 256
-451 412 475 -1 -899
-213 -214 542 12 3
Sample output 3: