PROGRAMMING
TECHNIQUE
coursework and summary for
Programming Technique subject.
Course Outline
| Chapter | Topic |
|---|---|
| 1 | Programming Problem-Solving. |
| 2 | Elementary Programming. |
| 3 | Control Structures. |
| 4 | Functions. |
| 5 | Array. |
| 6 | Input & Output. |
| 7 | Pointers. |
| 8 | Structured Data. |
Assignment 1
Exercise 2 : Set 1 - Post Test Loop
/*exercise 1 : post-test loop*/
#include <iostream>
using namespace std;
int main()
{
int number, sum = 0, remainder;
cout<<"Enter a positive number : ";
cin>>number;
do {
remainder = number % 10;
sum = sum + remainder;
number = number / 10;
}while (number>0) ;
if (sum % 3 == 0 && sum % 4 == 0 && sum % 5 == 0) {
cout<<"Sum of digits is "<<sum<<" and divisible by 3, 4 & 5.";
}
else if (sum % 3 == 0 && sum % 4 != 0 && sum % 5 != 0) {
cout<<"Sum of digits is "<<sum<<" and divisible by 3.";
}
else if (sum % 3 != 0 && sum % 4 == 0 && sum % 5 != 0) {
cout<<"Sum of digits is "<<sum<<" and divisible by 4.";
}
else if (sum % 3 != 0 && sum % 4 != 0 && sum % 5 == 0) {
cout<<"Sum of digits is "<<sum<<" and divisible by 5.";
}
else if (sum % 3 == 0 && sum % 4 == 0 && sum % 5 != 0) {
cout<<"Sum of digits is "<<sum<<" and divisible by 3 & 4.";
}
else if (sum % 3 == 0 && sum % 4 != 0 && sum % 5 == 0) {
cout<<"Sum of digits is "<<sum<<" and divisible by 3 & 5.";
}
else if (sum % 3 != 0 && sum % 4 == 0 && sum % 5 == 0) {
cout<<"Sum of digits is "<<sum<<" and divisible by 4 & 5.";
}
else {
cout<<"Sum of digits is "<<sum<<" and not divisible by 3, 4 & 5.";
}
return 0;
}
Exercise 2 : Set 1 - Pre Test Loop
/*exercise 1 set 3 : pre-test loop*/
#include <iostream>
using namespace std;
int main()
{
int num1, num2, remainder, product = 1, i;
cout << "Enter a positive number: ";
cin >> num1;
num2 = num1;
for(i=num1;i>0;i=i/10) {
remainder = i % 10;
product = product *remainder;
}
if (product % 4 == 0 && product % 5 == 0 && product % 7 == 0) {
cout<<"Product of digits is "<<product<<" and divisible by 4, 5 & 7.";
}
else if (product % 4 == 0 && product % 5 != 0 && product % 7 != 0) {
cout<<"Product of digits is "<<product<<" and divisible by 4.";
}
else if (product % 4 != 0 && product % 5 == 0 && product % 7 != 0) {
cout<<"Product of digits is "<<product<<" and divisible by 5.";
}
else if (product % 4 != 0 && product % 5 != 0 && product % 7 == 0) {
cout<<"Product of digits is "<<product<<" and divisible by 7.";
}
else if (product % 4 == 0 && product % 5 == 0 && product % 7 != 0) {
cout<<"Product of digits is "<<product<<" and divisible by 4 & 5.";
}
else if (product % 4 == 0 && product % 5 != 0 && product % 7 == 0) {
cout<<"Product of digits is "<<product<<" and divisible by 4 & 7.";
}
else if (product % 4 != 0 && product % 5 == 0 && product % 7 == 0) {
cout<<"Product of digits is "<<product<<" and divisible by 5 & 7.";
}
else {
cout<<"Product of digits is "<<product<<" and not divisible by 4, 5 & 7.";
}
return 0;
}
Assignment 2
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int carprice, downpayment, loanterms, interestrate, region, balance, totalinterest, monthlyinstallment, months, principal, interest;
do {
cout<<"Enter your car price : RM";
cin>>carprice;
} while (carprice < 22000);
do {
cout<<"Enter your car downpayment (%) : ";
cin>>downpayment;
} while (downpayment <= 0);
do {
cout<<"Enter loan terms (in years) : ";
cin>>loanterms;
} while (loanterms <= 3 || loanterms >10);
do {
cout<<"Enter your car interest rate (%) : ";
cin>>interestrate;
} while (interestrate <3 || interestrate >9 );
balance = carprice - downpayment;
months = loanterms * 12;
totalinterest = balance * (interestrate/100) * loanterms;
monthlyinstallment = (balance + totalinterest)/months;
cout<<"Select your region ; \n1 = SM (semenanjung) or 2 = SS (sabah & sarawak) ? ";
cin>>region;
if (region == 1 && region != 2) {
monthlyinstallment = monthlyinstallment + (0.04 * monthlyinstallment);
cout<<"Monthly installment are increased by 4%.";
} else if (region != 1 && region == 2) {
monthlyinstallment = monthlyinstallment + (0.08 * monthlyinstallment);
cout<<"Monthly installment are increased by 8%.";
}
cout<<"\nMonthly installment : RM "<< monthlyinstallment << ".";
return 0;
}
Exercise 3 (Function)
/*pt exercise 3
name : Nurul Adriana Binti Kamal Jefri
matrics number : A23CS0258
date : 17/12/2023*/
#include <iostream>
#include <string>
using namespace std;
void dispStatus (int);
void getInput (int&, int&, int&, int&);
void dispOutput (int);
int calcAverage (int, int);
int main(){
int n, x=0, y=0, totalCase, newCase, totalDeath, totalRecovered, highest=-999;
string state, temp, higheststate;
char result;
do {
cout<<"\n<<<<<<<<<<<<<DATA>>>>>>>>>>>>>\n";
cout<"Enter state : ";
getline(cin, state);
getInput (totalCase, newCase, totalDeath, totalRecovered);
n = totalCase + newCase - totalDeath - totalRecovered;
if (n > highest) {
highest=n;
higheststate=state;
}
cout<<"\n<<<<<<<<<<<<<SUMMARY>>>>>>>>>>>>>\n";
cout<<"Active cases : " <<n<< "\n";
dispOutput (n);
x = x+n;
y = y+1;
cin.ignore();
cout<<"\nPRESS <ENTER< TO CONTINUE...";
} while (cin.get() == '\n');
calcAverage (x,y);
cout<<"<<<<<<<<<<ACTIVE CASES>>>>>>>>>>";
cout<<"\nTotal : "<<x;
cout<<"Highest : "<<highest<<"("<<higheststate<<")";
cout<<"\nAverage for "<<y<<" state(s)"<<calcAverage (x,y);
return 0;
}
void getInput (int &a, int &b, int &c, int &d) {
cout<<"Enter total case : ";
cin>>a;
cout<<"Enter new cases : ";
cin>>b;
cout<<"Enter total death : ";
cin>>c;
cout<<"Enter total recovered : ";
cin>>d;
}
void dispOutput (int n) {
dispStatus (n);
}
void dispStatus (int n) {
if (n > 40) {
cout<<"Status of zone : RED.";
} else if (n >= 21 && n <= 40) {
cout<<"Status of zone : ORANGE.";
} else if (n >= 1 && n <= 20) {
cout<<"Status of zone : YELLOW.";
} else if (n == 0) {
cout<<"Status of zone : GREEN.";
} else {
cout<<"Status of zone : GREEN.";
}
}
int calcAverage (int x, int y) {
int average = x/y;
return average;
}
Exercise for Input Output
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void readFile(ifstream&, float[], int);
void computeC(float[], float[], int);
float average(float[]);
void grade(float[], char[], int &, int &, int &);
void writeFile(ofstream&, float[], float[], char []);
int main () {
ifstream input("input.txt");
ofstream output("output.txt");
float F[8];
float C[8];
int m=0, countH=0, countM=0, countL=0;
char G[8];
readFile (input, F, m);
computeC (F, C, m);
cout << "Average of the temperature in Celcius : ";
cout << fixed << showpoint << setprecision(1) << average (C) << endl;
grade (C, G, countH, countM, countL);
cout << "Number of high temperature :" << countH << endl;
cout << "Number of medium temperature : " << countM << endl;
cout << "Number of low temperature : " << countL << endl;
writeFile (output, F, C, G);
}
void readFile (ifstream& input, float F[], int m) {
float f;
if (!input.is_open()) {
cout<<"Terminating!";
}
while (!input.eof()) {
for (int i=0; i<8; i++) {
input >> f;
F[i]=f;
m = m+1;
}
}
}
void computeC (float F[], float C[], int m) {
for (int i = 0; i<8; i++) {
C[i] = (5.0/9.0) * (F[i] - 32);
}
}
float average (float C[]) {
float sum = 0.0;
for (int i = 0; i<8; i++) {
sum += C[i];
}
float ave = sum / 8.00;
return ave;
}
void grade (float C[], char G[], int &x, int &countM, int &countL) {
for (int i = 0; i < 8; i++) {
if (C[i]>= 35) {
G[i] = 'H' ;
x += 1;
} else if (C[i]<35 && C[i]>=20) {
G[i] = 'M' ;
countM += 1;
} else if (C[i]<20) {
G[i] = 'L' ;
countL += 1;
}
}
}
void writeFile (ofstream &output, float F[], float C[], char G[]) {
if (output.is_open()) {
output << setw(15) << "C(Celcius)" << setw(15) << "F(Farenheit)" << setw(15) << "Description" << endl;
output << setw(15) << "==========" << setw(15) << "============" << setw(15) << "===========" << endl;
for (int i = 0; i < 8; ++i) {
output << fixed << showpoint << setprecision(2);
output << setw(15) << C[i];
output << setw(15) << F[i];
output << setw(15) << G[i] << endl;
} output.close();
} else {
cout << "Unable to open file for writing." << endl;
}
}
Project Case Study
Case study 1
/* Group member :
1. DAMIYA AINA BINTI BASIR ABD SHAMAD (A23CS0220)
2. NURUL ADRIANA BINTI KAMAL JEFRI (A23CS0258)
3. LUBNA AL HAANI BINTI RADZUAN (A23CS0107)
Date :
21 January 2024*/
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void calconvert(float carbo[], float protein[], float fat[]);
void calDV(float carboDV[], float proteinDV[], float fatDV[]);
void category(float carboDV[], float proteinDV[], float fatDV[], string level_carbo[], string level_protein[], string level_fat[]);
string categoryAVG(float);
int main()
{
fstream infile("inputcereal.txt", ios::in );
fstream outfile("outputcereal.txt", ios::out);
string cerealname[10];
char cerealtype[10];
float carbo[10], protein[10], fat[10];
float carboDV[10], proteinDV[10], fatDV[10];
int nutrientchoice,level,num = 1;
float total_carbo = 0, total_protein = 0, total_fat = 0;
float average_carbo, average_protein, average_fat;
string level_carbo[10], level_protein[10], level_fat[10];
char choice;
string level_avgcarb, level_avgpro, level_avgfat;
if(!infile)
{
cout<<"ERROR: Cannot open file\n";
exit(1);
} for(int i=0; i<10; i++) {
infile>>cerealname[i]>>cerealtype[i]>>carbo[i]>>protein[i]>>fat[i];
}
do {
cout<<"1.Carbohydrate\n2.Protein\n3.Fat\n";
cout<<"Please enter your choice>> ";
cin>>nutrientchoice;
cout<<"\n1.Low\n2.Moderate\n3.High\n";
cout<<"Please enter your choice>> ";
cin>>level;
cout<<endl;
calconvert(carbo, protein, fat);
for(int i=0; i<10;i++) {
carboDV[i]=carbo[i];
proteinDV[i]=protein[i];
fatDV[i]=fat[i];
}
calDV( carboDV, proteinDV, fatDV);
category(carboDV, proteinDV, fatDV, level_carbo, level_protein, level_fat);
for(int i=0; i<10;i++) {
total_carbo = total_carbo + carboDV[i];
total_protein = total_protein + proteinDV[i];
total_fat = total_fat + fatDV[i];
}
average_carbo = total_carbo / 10;
average_protein = total_protein / 10;
average_fat = total_fat / 10;
if(nutrientchoice==1) {
if(level == 1)
{
cout<<"List of cereal with low amount of carbohydrate:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "Low") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
if(level == 2) {
cout<<"List of cereal with moderate amount of carbohydrate:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "Moderate") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
if(level == 3) {
cout<<"List of cereal with high amount of carbohydrate:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "High") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
}
if(nutrientchoice==2) {
if(level == 1) {
cout<<"List of cereal with low amount of protein:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "Low") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
if(level == 2) {
cout<<"List of cereal with moderate amount of protein:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "Moderate") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
if(level == 3) {
cout<<"List of cereal with high amount of protein:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "High") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
}
if(nutrientchoice==3) {
if(level == 1) {
cout<<"List of cereal with low amount of fat:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "Low") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
if(level == 2) {
cout<<"List of cereal with moderate amount of fat:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "Moderate") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
if(level == 3) {
cout<<"List of cereal with high amount of fat:\n";
for(int i=0 ; i<10; i++) {
if(level_carbo[i] == "High") {
cout<<num<<". Cereal "<<cerealtype[i]<<endl;
num++;
}
}
}
level_avgcarb = categoryAVG(average_carbo);
level_avgpro = categoryAVG(average_protein);
level_avgfat = categoryAVG(average_fat);
}
cout<<"Choose again? Y/N >> ";
cin>>choice;
} while((choice == 'Y')||(choice == 'y'));
outfile<<"Type\t\tCarbohydrate\t\t Protein\t\t Fat\n";
outfile<<"========\t============\t\t =======\t\t ===\n";
for(int i=0;i<10;i++) {
outfile<<"Cereal "<<cerealtype[i]<<"\t";
if (level_carbo[i] == "Moderate")
outfile<<fixed<<setprecision(2)<<setw(5)<<carboDV[i]<<"%("<<level_carbo[i]<<left<<setw(10)<<")";
else if (level_carbo[i] == "Low")
outfile<<fixed<<setprecision(2)<<setw(5)<<carboDV[i]<<"%("<<level_carbo[i]<<left<<setw(15)<<")";
else
outfile<<fixed<<setprecision(2)<<setw(5)<<carboDV[i]<<"%("<<level_carbo[i]<<left<<setw(14)<<")";
if(level_protein[i] == "Moderate") {
outfile<<setw(5)<<proteinDV[i]<<"%("<<level_protein[i]<<left<<setw(10)<<")";
} else if(level_protein[i] == "Low") {
outfile<<setw(5)<<proteinDV[i]<<"%("<<level_protein[i]<<left<<setw(15)<<")";
} else {
outfile<<setw(5)<<proteinDV[i]<<"%("<<level_protein[i]<<left<<setw(14)<<")";
}
outfile<<fatDV[i]<<"%("<<level_fat[i]<<")\n";
}
outfile<<"The ten cereal types produce an average %DV of:\n";
outfile<<"Carbohydrate: "<<average_carbo<<"%("<<level_avgcarb<<")\n";
outfile<<"Protein: "<<average_protein<<"%("<<level_avgpro<<")\n";
outfile<<"Fat: "<<average_fat<<"%("<<level_avgfat<<")\n";
infile.close();
outfile.close();
return 0;
}
void calconvert(float carbo[], float protein[], float fat[]) {
for(int i=0; i<10; i++) {
carbo[i] = carbo[i]*28.35;
protein[i] = protein[i]*28.35;
fat[i] = fat[i]*28.35;
}
}
void calDV(float carboDV[], float proteinDV[], float fatDV[]) {
for(int i = 0; i < 10; i++) {
carboDV[i] = carboDV[i] / 300 * 100;
proteinDV[i] = proteinDV[i] / 50 * 100;
fatDV[i] = fatDV[i] / 65 * 100;
}
}
void category(float carboDV[], float proteinDV[], float fatDV[], string level_carbo[], string level_protein[], string level_fat[]) {
for(int i=0; i<10; i++) {
if (carboDV[i] <= 5)
level_carbo[i] = "Low";
else if ((carboDV[i] > 5) && (carboDV[i] < 20))
level_carbo[i] = "Moderate";
else
level_carbo[i] = "High";
}
for(int i=0; i<10; i++) {
if (proteinDV[i] <= 5)
level_protein[i] = "Low";
else if ((proteinDV[i] > 5) && (proteinDV[i] < 20))
level_protein[i] = "Moderate";
else
level_protein[i] = "High";
}
for(int i=0; i<10; i++) {
if (fatDV[i] <= 5)
level_fat[i] = "Low";
else if ((fatDV[i] > 5) && (fatDV[i] < 20))
level_fat[i] = "Moderate";
else
level_fat[i] = "High";
}
}
string categoryAVG(float avg) {
string level;
if (avg <= 5)
level= "Low";
else if ((avg > 5) && (avg < 20))
level = "Moderate";
else
level= "High";
return level;
}
Case study 3
/* Group member :
1. DAMIYA AINA BINTI BASIR ABD SHAMAD (A23CS0220)
2. NURUL ADRIANA BINTI KAMAL JEFRI (A23CS0258)
3. LUBNA AL HAANI BINTI RADZUAN (A23CS0107)
Date :
21 January 2024*/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
using namespace std;
//Function prototype list
int readFile(const char*, float[][12], string[], int);
int change(float[][12],int);
float grandTotalSales(float[][12], int);
float highestSale(float[][12], string[], string[], int, int&, int&);
float lowestSale(float[][12], string[], string[], int, int&, int&);
float totalMonthly(float[][12], int, int);
int totalStores(float[][12], float[], int);
int main()
{
const int row = 5, col = 12;
int h1 = 0, h2 = 0, l1 = 0, l2 = 0;
float sales[row][col], storeAnn[5];
string store[row];
string month[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const char* file = "sales2014.dat";
const char* ofile = "Report Sales.txt";
int read = readFile(file, sales, store, row); // Read input from file
int changes = change(sales,row); // Change all input to real value
float total = grandTotalSales(sales, row); // Calculate grand total
float avg = total / 12.0; // Calculate average of company annual sales
float highest = highestSale(sales, store, month, row, h1, h2); // Find the highest monthly sales
float lowest = lowestSale(sales, store, month, row, l1, l2); // Find the lowest monthly sales
int totalS = totalStores(sales, storeAnn, row); // Calculate total sales for each store
fstream outfile(ofile, ios::out); // Opening output file
// Check output file can be opened or not
if (!outfile)
cout << "Error in opening output file" << endl;
else
cout << "Output file is opened successfully" << endl;
// Output for grand total and average
outfile << "Total of sales over all stores : RM ";
outfile << right << setw(10);
outfile << fixed << setprecision(2) << total << endl;
outfile << "Average sale per month : RM ";
outfile << right << setw(10);
outfile << fixed << setprecision(2) << avg << endl;
// Output for the highest monthly sales
outfile << "\nThe highest sales :" << endl;
outfile << "-------------------" << endl;
outfile << "Store : " << store[h1] << endl;
outfile << "Month : " << month[h2] << endl;
outfile << "Sales : RM " << fixed << setprecision(2) << right << setw(10) << highest << endl;
// Output for the lowest monthly sales
outfile << "\nThe lowest sales :" << endl;
outfile << "-------------------" << endl;
outfile << "Store : " << store[l1] << endl;
outfile << "Month : " << month[l2] << endl;
outfile << "Sales : RM " << fixed << setprecision(2) << right << setw(10) << lowest << endl;
// Output for total sales for each month
outfile << "\n\nTotal sales by month :" << endl << "Month" << setw(10) << "Sales" << endl << "-----" << setw(10) << "-----" << endl;
outfile << right;
for (int x = 0; x < 12; x++)
{
outfile << month[x] << " RM " << fixed << setprecision(2) << setw(10) << totalMonthly(sales, x, row) << endl;
}
// Output for the total annual sales for each store
outfile << "\nTotal sales by store :" << endl << "Store" << setw(21) << "Total Sales" << endl << "-----" << setw(21) << "-----------" << endl;
for (int x = 0; x < row; x++)
{
outfile << left;
outfile << setw(14) << store[x] << "RM";
outfile << right;
outfile << setw(10) << fixed << setprecision(2) << storeAnn[x] << endl;
}
// Output for the profitable stores
outfile << "\nProfitable stores :" << endl << "-----------------" << endl;
for (int x = 0; x < row; x++)
{
if (storeAnn[x] >= 600000)
outfile << store[x] << endl;
}
outfile.close(); // Closing the output file
return 0;
}
// Function to read input from input file
int readFile(const char* file, float sales[][12],string store[],int row)
{
fstream infile(file, ios::in);
if (!infile)
cout << "Error in opening input file" << endl;
else
cout << "Input file is opened successfully" << endl;
for (int x = 0; x < row; x++)
{
for (int y = 0; y< 12; y++)
{
infile >> sales[x][y];
}
getline(infile >> ws, store[x]);
}
infile.close();
return row;
}
// Function to change each cell to real value(multiple of 1000)
int change(float sales[][12], int row)
{
for (int x = 0; x < row; x++)
{
for (int y = 0; y < 12; y++)
{
sales[x][y] = sales[x][y] * 1000.0;
}
}
return row;
}
// Function to calculate the grand total
float grandTotalSales(float sales[][12], int row)
{
float total = 0;
for (int x = 0; x < row; x++)
{
for (int y = 0; y < 12; y++)
{
total += sales[x][y];
}
}
return total;
}
// Function to find the highest sales
float highestSale(float sales[][12], string store[], string month[], int row, int &n, int &c)
{
float high = 0;
for (int x = 0; x < row; x++)
{
for (int y = 0; y < 12; y++)
{
if (sales[x][y] >= high)
{
high = sales[x][y];
n = x;
c = y;
}
}
}
return high;
}
// Function to find the lowest sales
float lowestSale(float sales[][12], string store[], string month[], int row, int &i, int &j)
{
float low = 9999999999;
for (int x = 0; x < row; x++)
{
for (int y = 0; y < 12; y++)
{
if (sales[x][y] <= low)
{
low = sales[x][y];
i = x;
j = y;
}
}
}
return low;
}
// Function to calculate total monthly sales for each month
float totalMonthly (float sales[][12], int x, int row)
{
float total = 0;
for (int y = 0; y < row; y++)
{
total += sales[y][x];
}
return total;
}
// Function to calculate total annual sales for each store
int totalStores(float sales[][12], float storeAnn[], int row)
{
for (int x = 0; x < row; x++)
{
for (int y = 0; y < 12; y++)
{
storeAnn[x] += sales[x][y];
}
}
return row;
}
Summary
Dr. Eiliyah @ Wong Yee Leng taught this topic. As a foundational subject, this course provides students with both theory and application in methods for problem-solving using a structured approach. Students are needed to create programming in the C++ programming language to tackle easy to moderate challenges. The course covers the following topics: pre-processor guidance, parameters and elements, data types, input and output statements, control structures which include sequential, selection, and loop, built-in and user-defined functions, single and two-dimensional arrays, file operations, pointers, and structured data types.