SECJ1013 - Programming Technique I
Course Information
This subject was taught by Dr Nies Hui Wen. As a fundamental subject, this course equips the students with theory and practice on problem solving techniques by using the structured approach. Students are required to develop programs using C++ programming language, in order to solve simple to moderate problems. The course covers the following: pre-processor directives, constants and variables, data types, input and output statements, control structures: sequential, selection and loop, built-in and user-defined functions, single and two dimensional arrays, file operations, pointers, and structured data types.
Course Outline
- CHAPTER 1: PROGRAMMING PROBLEM-SOLVING
- CHAPTER 2: ELEMENTARY PROGRAMMING
- CHAPTER 3: CONTROL STRUCTURES
- CHAPTER 4: FUNCTIONS
- CHAPTER 5: ARRAY
- CHAPTER 6: INPUT AND OUTPUT
- CHAPTER 7: POINTERS
- CHAPTER 8: STRUCTURED DATA
Coursework
Lab
Assignment
/*
ASSIGNMENT 2 PROGRAMMING TECHNIQUE (SET 1)
TEAM MEMBERS:
YASMIN BATRISYIA BINTI ZAHIRUDDIN (A23CS0201)
NURUL ASYIKIN BINTI KHAIRUL ANUAR (A23CS0162)
NAJMA SHAKIRAH BINTI SHAHRULZAMAN (A23CS0140)
*/
#include <iostream>
using namespace std;
void displayAccountInfo(string, int, double);
void deposit(double&, double);
void withdraw(double&, double);
int main()
{
string acc_holder = "User 1";
int acc_num = 1013202341;
double acc_balance = 200;
char trans;
do
{
cout << "<<<<< My Accounts Overview >>>>>" << endl;
displayAccountInfo(acc_holder, acc_num, acc_balance);
cout << endl;
cout << "<<<<< Deposit Transaction >>>>>" << endl;
deposit(acc_balance, 500);
cout << endl;
cout << "<<<<< Withdrawal Transaction >>>>>" << endl;
withdraw(acc_balance, 200);
cout << endl;
cout << "<<<<< My Accounts Overview >>>>>" << endl;
displayAccountInfo(acc_holder, acc_num, acc_balance);
cout << endl;
cout << "Do you want to perform another transaction? (Y/N) :";
cin >> trans;
cout << endl;
} while ( (trans != 'N' ||trans != 'n') && (trans == 'Y' || trans == 'y') );
system("pause");
return 0;
}
void displayAccountInfo(string accountName, int accountNum, double balance)
{
cout << "Account Holder Name: " << accountName << endl;
cout << "Account Number: " << accountNum << endl;
cout << "Balance: RM " << balance << endl;
}
void deposit(double& balance, double deposit)
{
cout << "Deposit of RM " << deposit << " successful." << endl;
balance = balance + deposit;
}
void withdraw(double& balance, double withdrawal)
{
if (withdrawal <= balance)
{
cout << "Withdrawal of RM " << withdrawal << " successful." << endl;
balance = balance - withdrawal;
}
else
{
cout << "Insufficient funds for withdrawal" << endl;
}
}
/*
ASSIGNMENT 2 PROGRAMMING TECHNIQUE (SET 2)
TEAM MEMBERS:
YASMIN BATRISYIA BINTI ZAHIRUDDIN (A23CS0201)
NURUL ASYIKIN BINTI KHAIRUL ANUAR (A23CS0162)
NAJMA SHAKIRAH BINTI SHAHRULZAMAN (A23CS0140)
*/
#include <iostream>
using namespace std;
int main (){
int number;
cout<<"Welcome to the Food Ordering System"<<endl;
cout<<"1. Pizza - $10"<<endl;
cout<<"2. Burger - $5"<<endl;
cout<<"3. Sandwich - $7"<<endl;
cout<<"Enter the number of the item you want to order: ";
cin>>number;
switch (number){
case 1 : cout<<"Your total bill is: $10"<<endl;
break;
case 2 : cout<<"Your total bill is: $5"<<endl;
break;
case 3 : cout<<"Your total bill is: $7"<<endl;
break;
default : cout<<" "<<endl;
}
system("pause");
return 0;
}
/*
ASSIGNMENT 3 PROGRAMMING TECHNIQUE (SET 1)
TEAM MEMBERS:
YASMIN BATRISYIA BINTI ZAHIRUDDIN (A23CS0201)
NURUL ASYIKIN BINTI KHAIRUL ANUAR (A23CS0162)
NAJMA SHAKIRAH BINTI SHAHRULZAMAN (A23CS0140)
*/
#include<iostream>
using namespace std;
//function prototypes
void displayMainMenu();
void addBook(string[], string[], int[], int&);
void displayLibrary(string[], string[], int[], int);
void searchByTitle(string[], string[], int[], int);
const int MAX_BOOKS = 100;
int main()
{
string bookTitle[MAX_BOOKS], bookAuthor[MAX_BOOKS];//arrays to keep book title,author entered by user
int pubYear[MAX_BOOKS];//array to keep publication year of book entered by user
int choice;//for user to choose between option in menu
int numBooks = 0;//to count number of books entered by user
do
{
displayMainMenu();//function call to display main menu
cout << "Enter your choice: ";
cin >> choice;//user's choice based on option given
cout << endl;
switch (choice)
{
case 1:
if(numBooks < MAX_BOOKS)//only allow user to add books if num of max is not reached
{
addBook(bookTitle, bookAuthor, pubYear, numBooks);//function call to add a book
break;
}
else
{
cout << "Maximum books reached. Cannot add more books\n" << endl;//display message when user can't add any more books
break;
}
case 2:
if(numBooks == 0)//display message if no library in system yet
{
cout << "No books in library." << endl << endl;
break;
}
else
{
displayLibrary(bookTitle, bookAuthor, pubYear, numBooks);//function call to display all books entered by user
break;
}
case 3:
searchByTitle(bookTitle, bookAuthor, pubYear, numBooks);//function call to search book by title
break;
case 4:
cout << "Goodbye!" << endl << endl;
cout << "--------------------------------";
return 0;//end program once user choose option 4. Quit
}
}while(true);
}
//function to display menu with options
void displayMainMenu()
{
cout << "<<<<<Library Management System>>>>>" << endl;
cout << "========================================" << endl;
cout << "1. Add a Book" << endl;
cout << "2. Display Library" << endl;
cout << "3. Search by Title" << endl;
cout << "4. Quit" << endl;
}
//function for user to add and input book details
void addBook(string book[], string author[], int year[], int &numBooks)
{
cin.clear();// Clear any error flags
cin.ignore(); // Ignore newline character after reading the integer
cout << "Enter book title: ";
getline(cin, book[numBooks]);
cout << "Enter author name: ";
getline(cin, author[numBooks]);
cout << "Enter publication year: ";
cin >> year[numBooks];
cout << "\nBook added successfully!";
numBooks++; //counter for each book added by user
cout << "\n\n";
}
void displayLibrary(string book[], string author[], int year[], int numBooks)
{
cout << "Library Contents:" << endl;
cout << "====================" << endl;
for (int i = 0; i < numBooks; i++)
{
cout << "Title: " << book[i] << endl;
cout << "Author: " << author[i] << endl;
cout << "Year: " << year[i] << endl;
cout << endl;
}
cout << "\n\n";
}
void searchByTitle(string book[], string author[], int year[], int numBooks)
{
string search;
int index = numBooks + 1;
cin.ignore(); // Ignore newline character after reading the integer
do
{
cout << "Enter the title to search: ";
getline(cin, search);//enter book title to search
}while(search.empty());//repeat when enter/newline
cout << endl;
for (int i = 0; i < MAX_BOOKS; i++)
{
if (search == book[i])//search all books in array until found the same title
{
index = i; //assign index of book found with same title
break;
}
}
if (index == numBooks + 1) //if no book is found after searching all books in program, display message
{
cout << "Book is not found!";
}
else
{
cout << "Book found:" << endl;
cout << "====================" << endl;
cout << "Title: " << book[index] << endl;
cout << "Author: " << author[index] << endl;
cout << "Year: " << year[index] << endl;
cout << "Index book found: " << index;
}
cout << "\n\n";
}
/*
ASSIGNMENT 3 PROGRAMMING TECHNIQUE (SET 2)
TEAM MEMBERS:
YASMIN BATRISYIA BINTI ZAHIRUDDIN (A23CS0201)
NURUL ASYIKIN BINTI KHAIRUL ANUAR (A23CS0162)
NAJMA SHAKIRAH BINTI SHAHRULZAMAN (A23CS0140)
*/
#include<iostream>
using namespace std;
int multiplyUsingAddition(int , int );
void displayMainMenu();
void performMultiplication(int[], int[], int &);
void displayResults(int[] ,int[], int);
int main()
{
const int MAX_OPERATIONS = 100; //can handle 100 operations
int operands1[MAX_OPERATIONS];// array declaration for number or operands for each operations
int results[MAX_OPERATIONS];// array declaration for the results of each operation
int operationCount = 0;//to count number of operation every time user perform multiplication
int choice;//for user to choose between option in menu
do
{
displayMainMenu();//display options
cout << "Enter your choice: ";//ask user which option they choose
cin >> choice;
cout << endl;
switch(choice)//determine which function to execute based on choice
{
case 1: if(operationCount < MAX_OPERATIONS)//allow user to perform multiplication until num of max operation reached.
{
performMultiplication(operands1,results,operationCount);
break;
}
else
{
cout << "Maximum number of operations reached!"//message if user has reach max operations already
<< "Cannot perform another multiplication" << endl << endl;
break;
}
case 2: if(operationCount == 0)//display message if no operation in system yet
{
cout << "No operation in system." << endl << endl;
break;
}
else
{
displayResults(operands1,results,operationCount);
break;
}
case 3: cout << "Goodbye!" << endl << endl;
cout << "--------------------------------";
return 0;// end program once user chooses option 3 (Quit)
break;
}
}while(true);
}
int multiplyUsingAddition(int a, int b)
{
int result = 0;//declare and initialize result with 0
for(int i = 1; i <= b; i++)//repeat addition b times(if 4*5, then 4+4+4+4+4)
{
result += a;
}
return result;
}
void displayMainMenu()//display options
{
cout << "<<<<<Main Menu>>>>>" << endl;
cout << "=============================" << endl;
cout << "1. Perform Multiplication" << endl;
cout << "2. Display Results" << endl;
cout << "3. Quit" << endl;
}
void performMultiplication(int operands1[], int results[], int &operationCount)
{
int a,b;
cout << "Enter the number of operands for multiplication: ";
cin >> operands1[operationCount];
for(int i = 1; i <= operands1[operationCount]; i++)
{
if(i == 1)//initializing first a with first operand
{
cout << "Enter operand " << i << ": ";
cin >> a;
}
else
{
cout << "Enter operand " << i << ": ";
cin >> b;
a = multiplyUsingAddition(a, b);//repeat calculate until entered num of operands reached
}
}
results[operationCount] = a;//keeping result in array
cout << "\nMultiplication performed successfully!\n" << endl;
operationCount++;//counter for each operation done
}
void displayResults(int operands1[], int results[], int operationCount)
{
cout << "Results of Mathematical Operations: " << endl;
cout << "========================================" << endl;
for(int i = 0; i < operationCount; i++)
{
cout << "Operation " << i+1 << ": " << results[i]
<< " (Operands: " << operands1[i] << ")" << endl;
}
cout << endl << endl;
}
Reflection
We learnt the basics of C++ in this class. C++ is a bit hard complexity, but it is versatile and has a good performance. Learning the basics of programming provides a valuable skill set and knowledge base that can greatly when implemented when creating programs.I could improve by doing more program exercises and eventually try doing bigger projects in order to gain full understanding of the programming language.