0% found this document useful (0 votes)
24 views

Message

This C++ program contains functions for 3 questions that print different patterns of stars. It begins by displaying the author's information and then presents a menu where the user can select which question to run. Based on the selection, it will call the corresponding function that contains the code to output the star pattern. It ends by thanking the user after they select to quit the program.

Uploaded by

Not TGT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Message

This C++ program contains functions for 3 questions that print different patterns of stars. It begins by displaying the author's information and then presents a menu where the user can select which question to run. Based on the selection, it will call the corresponding function that contains the code to output the star pattern. It ends by thanking the user after they select to quit the program.

Uploaded by

Not TGT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// SEHH2042 Assignment 1

// Program template file


// Do not modify the given codes

// =======================================
// Insert more header files when necessary
// =======================================
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;

void showInfo()
{
// Fill in your personal particulars below
cout << "Name : Lam Chun Kit\n";
cout << "Student ID: 22178833A\n";
cout << "Class : 205\n";
}

void Q1()
{
// =====================================
// Insert your codes for Question 1 here
// No need main() and return 0
// =====================================
char mr;
int nop, nosd, nomd, nold;
double disc, alldish;

cin >> nop >> mr >> nosd >> nomd >> nold;
cout << setprecision(2);

switch (mr) {
case 'G': if (nop > 4)
{
nop = nop - 1;
}
alldish = (nop * 4.5) + (nosd * 15) + (nomd * 19) + (nold * 23);
if (nop > 5)
{
disc = alldish * 0.12;
}
else
{
disc = alldish * 0.08;
}
cout << fixed;
cout << (alldish * 1.1) - disc;
break;

case 'S': if (nop > 4)


{
nop = nop - 1;
}
alldish = (nop * 4.5) + (nosd * 15) + (nomd * 19) + (nold * 23);
if (nop > 8)
{
disc = alldish * 0.08;
}
else
{
disc = 0;
}
cout << fixed;
cout << (alldish * 1.1) - disc;
break;

case 'B': alldish = (nop * 4.5) + (nosd * 15) + (nomd * 19) + (nold * 23);
if (nop > 8)
{
disc = alldish * 0.08;
}
else
{
disc = 0;
}
cout << fixed;
cout << (alldish * 1.1) - disc;
break;

case 'N': alldish = (nop * 4.5) + (nosd * 15) + (nomd * 19) + (nold * 23);
cout << fixed;
cout << alldish * 1.1;
break;

default: cout << "";

}
}

void Q2()
{
// =====================================
// Insert your codes for Question 2 here
// No need main() and return 0
// =====================================
char secret = 'O', guess = 'A', head = 'A', end = 'Z';
int i = 0;

do {
if (isupper(guess))
{
if (int(guess) > int(secret))
{
if (int(end) > int(guess))
{
end = guess;
}
cout << head << " - " << end << ": ";
cin >> guess;
i = i + 1;
}
else if (int(guess) < int(secret))
{
if (int(end) > int(guess))
{
head = guess;
}
cout << head << " - " << end << ": ";
cin >> guess;
i = i + 1;
}
else if (int(guess) == int(secret))
{
i = i + 1;
}
}
else
{
cout << head << " - " << end << ": ";
cin >> guess;
i = i + 1;
}
} while (secret != guess);

cout << i;
}

void Q3()
{
// =====================================
// Insert your codes for Question 3 here
// No need main() and return 0
// =====================================
int x, onerowstarscount = 0, allstars, row, column, spaceinput, starcount =
0, spaceneeded = 1;
cin >> x;
allstars = x * 2;
spaceneeded = x;

for (row = 1; row <= allstars;row++) {


for (column = 1; column <= x;column++) {
cout << "*";
onerowstarscount++;
starcount++;
if (onerowstarscount == x) {
for (spaceinput = spaceneeded;spaceinput >= 1;spaceinput--)
{
cout << " ";
}
onerowstarscount = 0;

}
if (starcount == allstars)
{
cout << endl;
starcount = 0;
spaceneeded--;
}

onerowstarscount = 0;
starcount = 0;
for (row = 1; row <= x;row++) {
for (column = 1; column <= allstars;column++) {
cout << "*";
starcount++;

if (starcount == allstars)
{
cout << endl;
starcount = 0;
}
}
}

onerowstarscount = 0;
starcount = 0;
spaceneeded = 1;

for (row = 1; row <= allstars;row++) {


for (column = 1; column <= x;column++) {
cout << "*";
onerowstarscount++;
starcount++;
if (onerowstarscount == x) {
for (spaceinput = 1;spaceinput <= spaceneeded;spaceinput++)
{
cout << " ";
}
onerowstarscount = 0;

}
if (starcount == allstars)
{
cout << endl;
starcount = 0;
spaceneeded++;
}

// IMPORTANT: DO NOT MODIFY main()


int main()
{
char prog_choice;

do {
cout << "\n\n";
cout << "Program Selection Menu" << endl;
cout << "---------------------------------------" << endl;
cout << "Enter question number ('q' to quit): ";
cin >> prog_choice;
cout << "\n\n";

switch (prog_choice) {
case '0': showInfo(); break;
case '1': Q1(); break;
case '2': Q2(); break;
case '3': Q3(); break;
case 'q': break;
default:
cout << "No such question " << prog_choice << endl;
break;
}
} while (prog_choice != 'q');

cout << "Program terminates. Good bye!" << endl;


return 0;
}
// END

You might also like