Programming Technique

Programming Technique

Welcome to Programming World !

                                                                           See the source image

Reflection

Programming Technique is a tough course. I never thought on taking this course and it is still feel like dream. Eventhough I have basic in C++ which I study at matriculation  it is still hard for me to understand the programming language. However, I have to take it as challenges or motivation for me to study hard for this course. Thanks to Sir Fariz who teaching and guiding us throughout this semester. I hope one day my programming skills will improved. 

Assignment

ASG 1

#include<iostream>
#include<cmath>
using namespace std;
#define PI 3.14159

int main()
{
int choice;

float radius, areacircle, length, width,
arearectangle, base, height, areatriangle;

cout<<"This program is a geometry calculator"<<endl;
cout<<"Enter 1 to calculate area of a circle"<<endl;
cout<<"Enter 2 to calculate area of a rectangle"<<endl;
cout<<"Enter 3 to calculate area of triangle "<<endl;
cout<<"Enter 4 to end the program"<<endl<<endl;

A: cout<<"Enter number:";
cin>>choice;
cout<<endl;


switch (choice)
{
case 1 : cout<<"Enter radius of circle:";
cin>>radius;
if(radius>0){
areacircle=PI*pow(radius,2);
cout<<"Area of circle="<<areacircle<<endl<<endl;
goto A;
}
else{
cout<<"You entered negative number"<<endl;
cout<<"Please enter positive number only"<<endl<<endl;
goto A;

}


case 2 : cout<<"Enter length of rectangle:";
cin>>length;
if (length>0){
cout<<"Enter width of rectangle:";
}
else{
cout<<"You entered negative number"<<endl;
cout<<"Please enter positive number only"<<endl<<endl;
goto A;
}


cin>>width;
if (width>0){
arearectangle=length*width;
cout<<"Area of rectangle="<<arearectangle<<endl<<endl;
goto A;
}
else{
cout<<"You entered negative number"<<endl;
cout<<"Please enter positive number only"<<endl<<endl;
goto A;

}



case 3 : cout<<"Enter base of triangle:";
cin>>base;
if(base>0){
cout<<"Enter height of triangle:";
}
else
cout<<"You entered negative number"<<endl;
cout<<"Please enter positive number only"<<endl<<endl;
goto A;

cin>>height;
if(height>0){
areatriangle=base*height*0.5;
cout<<"Area of triangle="<<areatriangle<<endl<<endl;
goto A;
}
else{
cout<<"You entered negative number"<<endl;
cout<<"Please enter positive number only"<<endl<<endl;
goto A;
}



case 4 : cout<<"Program End"<<endl<<endl;
break;

default : cout<<"Entered number out of range"<<endl;
cout<<"Enter numrber 1 till 4 only"<<endl<<endl;
goto A;


}

cout<<"Thank You!"<<endl;



}

ASG 2

include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cctype>
using namespace std;

#define ROWS 15
#define COLS 30
const char TAKEN = '*';
const char EMPTY = '#';
//void displayMenu();
//int getChoice();
//void displaySeats(const char seats[][COLS]);
//void displayPrices(double price[][COLS]);
//void displaySales(double totalSales);
//void purchaseTicket(char seats[][COLS], const double ticketPrice[], double& grandTotal);


void displayMenu()
{

cout << "\n\n\n\t\tC++ Theatre" << endl << endl;
cout << "\n\t1. View Available Seats";
cout << "\n\t2. View Seating Prices";
cout << "\n\t3. View Ticket Sales";
cout << "\n\t4. Purchase a Ticket";
cout << "\n\t5. Exit the Program\n\n";
cout << "\n\tEnter your choice(1-5): ";
}
int getChoice(){
int choice = 0;
while(choice == 0 ){
cin >> choice;
if (choice <= 0 || choice > 5){
cout << "Choice must be between 1 and 5. Please re-enter: ";
choice = 0;
}
}
return choice;
}

void displaySeats(char seats[][COLS])
{

cout << "\n\t\tSeats";
cout << "\n 123456789012345678901234567890" << endl;

for(int r = 0; r < ROWS ; r++){
cout << "row " << setw(2) << r+1 << " ";
for(int c = 0; c < COLS; c++){
cout << seats[r][c];
}

cout << endl;
}

cout << "\n\n\n\tLegend:\t* = Sold";
cout << "\n\t\t# = Available";
cout << "\n\n\nPress the Enter key to continue.";
cin.ignore();
cin.get();

}

void displayPrices(double price[])
{

ifstream inputFile;
inputFile.open("ticket.txt");
double temp;
for(int i=0; i<ROWS; i++)
{
inputFile>>temp;
inputFile>>price[ROWS];

cout << "ROWS " << i+1 << " RM " << price[ROWS]<<endl;
}

inputFile.close();

cout << "\n\n\nPress the Enter key to continue.";
cin.ignore();
cin.get();

}

void purchaseTicket(char seats[][COLS],double ticketPrice[], double& grandTotal)
{
char choice = 'n';
char choice2 = 'n';
int Quit = '1';
int answer;
int numTickets = 0;
int totalTickets = 0;
int r = 0;
int c = 0;
double totPrice = 0;
char map[ROWS][COLS];


cout << "\t\tDo purchase your ticket" << endl << endl;

do
{
cout << "\nPlease enter desired row number (1-" << ROWS << "): ";
cin >> r;

if(r <= 0 || r > ROWS){
cout << "Row must be between 1 and "
<< ROWS << ". Please re-enter: ";
continue;
}
cout << "\nPlease enter desired seat number (1-" << COLS << "): ";
cin >> c;
if(c <= 0 || c > COLS){
cout << "Seat must be between 1 and " << COLS << ". Please re-enter: ";
continue;
}
// if the seat is available
else if(seats[r-1][c-1] == TAKEN){
cout << "\nSorry. That seat has been sold.\n";//**********************
continue;
}

else{ // and if it is - sell the ticket
seats[r-1][c-1] = TAKEN;
totPrice += ticketPrice[r-1];
cout << "\nPurchase confirmed\n";
numTickets++;
totalTickets++;
}

cout << "\n\nYou have purchased a total of " << numTickets << " tickets " << "for a total price of $" << totPrice;
grandTotal += totPrice;


}

while (Quit == 1);

}


void displaySales(double totalSales)
{

cout << "\n\nTotal Sales: $" << totalSales << "\n\n";
}

int main()
{
int choice = 0;
double totalSold = 0;
char map[ROWS][COLS];
double tickets[ROWS];
for(int r = 0; r < ROWS ; r++){
for (int c = 0; c < COLS; c++){
map[r][c] = EMPTY;
}
}

do
{
displayMenu();
choice = getChoice();

if (choice == 1){
displaySeats(map);
}

else if (choice == 2){
displayPrices(tickets);
}

else if (choice == 3)
displaySales(totalSold);

else if (choice == 4){
purchaseTicket(map,tickets, totalSold); // <--------------------------------?
}
}while(choice != 5);

return 0;
}

Lab Exercise

Module 3

Details