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

Programming Laboratory Activities Questions: I

The document contains programming exercises involving the use of arrays, loops, functions, and classes. It includes questions on creating programs to: 1) Display right triangles using asterisks based on user input height 2) Accept and sort integers from the user 3) Reserve seats from a 2D array representation of a theater 4) Perform math operations like division and displaying Fibonacci numbers using functions 5) Create a class to represent properties of legged mammals The concluding remarks note that the exercises help use multiple inputs, arrays, and conditional objects while solving more challenging problems.

Uploaded by

Marvin Torre
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
340 views

Programming Laboratory Activities Questions: I

The document contains programming exercises involving the use of arrays, loops, functions, and classes. It includes questions on creating programs to: 1) Display right triangles using asterisks based on user input height 2) Accept and sort integers from the user 3) Reserve seats from a 2D array representation of a theater 4) Perform math operations like division and displaying Fibonacci numbers using functions 5) Create a class to represent properties of legged mammals The concluding remarks note that the exercises help use multiple inputs, arrays, and conditional objects while solving more challenging problems.

Uploaded by

Marvin Torre
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Programming Laboratory Activities

Questions:

I.
1. Write a program using WHILE statement that displays a right triangle using asterisks where the height is at
the left corner of the application. The height will depend on the user input.

#include <iostream>

using namespace std;

int main()
{
int Height, Width=0, Line;

cout << "Enter Height: ";


cin >> Height;

while(Height > 0){


Line = Width;
while(Line >=0){
cout << "*";
Line--;
}

cout << endl;


Height--;
Width +=2;
}

cin.clear();
cin.ignore();
cin.get();

return 0;
}
2. Write a program using FOR statement that displays a right triangle using asterisks where the height is at the
right corner of the application.The height will depend on the user input.

#include <iostream>

using namespace std;

int main()
{
int Height;

cout << "Enter Height: ";


cin >> Height;

for(int a = 0, b = 0; a < Height; a++, b+=2){


for(int c = 0; c < (Height*2) - b - 2; c++){
cout << " ";
}

for(int c = 0; c <= b; c++){


cout << "*";
}
cout << endl;
}

cin.clear();
cin.ignore();
cin.get();

return 0;
}
II.

3. Write a program that will accept five (5) integers and display them to the users. You are limited to using only
two (2) variables (including the array).

#include <iostream>

using namespace std;

int main(){
int x[5], i;
cout << "Enter 5 numbers: " << endl;

for(i = 0; i < 5; i++)


{
cout << "Number[" << i + 1 << "]: ";
cin >> x[i];
}
cout << "You entered these integers: " << x[0] << ", " << x[1] << ", " << x[2] << ", " << x[3] << ", " << x[4] << endl;
cin.clear();
cin.ignore();
cin.get();

return 0;
}
4. Write a program that will display an equilateral triangle with a height depending on the user. The minimum
height is 1, the maximum height is 10. Use an array to display the specific character on the specific row. The
array will be: { 0 := “A”, 1:= “B”, 2 := “C”, 3 := “D”, 4 := “E”, 5 := “F”, 6 := “G”, 7 := “H”, 8 := “I”, 9 := “J” }. You
are limited to four (4) variables only (including the array).

#include <iostream>
#include <limits>

using namespace std;

int main () {
int intHeight, i, k;

while(true){
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else if (intHeight < 1 || intHeight > 10) {
cout << "Enter height (1 - 10): ";
cin >> intHeight;
} else {
break;
}
}

char arr[10] = {'A','B','C','D','E','F','G','H','I','J'};


for(i = 0; i < intHeight; i++)
{
for(k = 0; k < intHeight - i - 1; k++)
{
cout << " ";
}
for(k = 0; k < (i * 2) + 1; k++)
{
cout << arr[i];
}
cout << endl;
}
cin.clear();
cin.ignore();
cin.get();

return 0;
}
5. Write a program that will ascendingly sort six (6) integers from the user. Use only four (4) variables
(including the array).

#include <iostream>

using namespace std;

int main()
{
int x[6], i, j, temp;
cout << "Enter 6 numbers to Sort:" << endl;

for(i = 0; i < 6; i++)


{
cout << "Number[" << i + 1 << "]: ";
cin >> x[i];
}
for(i = 0; i < 6; i++)
{
j = i + 1;
while(j<6){
while(x[i] > x[j]) {
temp=x[i];
x[i]=x[j];
x[j]=temp;
j++;
}
j++;
}
}
cout << "Integers after sorted: " << x[0] << ", " << x[1] << ", " << x[2] << ", " << x[3] << ", " << x[4] << ", " << x[5] <<
endl;
cin.clear();
cin.ignore();
cin.get();

return 0;
}
6. What can you conclude from this activity?

The exercises are helpful because they allow us to use multiple user input entries using multiple variable.
Also, the use of arrays allows us to declare many other options while also using conditional objects learned from our
previous lessons. Doing this more exciting and challenging as we go along.
III.

Write a C++ program with the following specifications:


1. Use two-dimensional array with size 7 columns and 5 rows.
2. Seat numbers are populated during run-time.
3. User is asked to input a seat number.
4. Seat number chosen is replaced by 0.
5. Program displays a remark “Seat successfully reserved” when reservation is done.
6. User is not allowed to reserve a previously reserved seat. Display “Seat is taken” remarks.
7. User is not allowed to enter an invalid seat number. Display an error message.
8. Program continuously loops.

Sample Output:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35

Enter seat number to reserve : 11

1 2 3 4 5 6 7
8 9 10 0 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35

Enter seat number to reserve :

#include<iostream>

using namespace std;

int main(){
int SeatNumber,i,t,count,ask;
int a[5][7] = {{1,2,3,4,5,6,7},{8,8,10,11,12,13,14},{15,16,17,18,19,20,21},
{22,23,24,25,26,27,28},{29,30,31,32,33,34,35}};
for(i = 0; i < 5; i++){
for( t = 0; t < 7; t++){
cout << a[i][t] << "\t";
}
cout << endl;
}
b:
if(ask ==1){
for(i = 0; i < 5; i++){
for ( t = 0; t < 7; t++){
if(SeatNumber==a[i][t]){
a[i][t] = 0;
count++;
}
cout << a[i][t] << "\t";
}
cout << endl;
}
}
cout << "Enter Seat Number to reserve: ";
cin >> SeatNumber;
count = 0;

for( i =0; i < 5; i++){


for ( t = 0; t < 7; t++){
if(SeatNumber==a[i][t]){
a[i][t] = 0;
count++;
}
cout << a[i][t] << "\t";
}
cout << endl;
}
if (count ==0){
if((SeatNumber >0) && (SeatNumber < 36)){
cout << "\nSeat is Taken\n";
}else{
cout << "\nInvalid Seat number\n";
}
}else{
cout << "\nSeat Successfully reserved\n";
ask = 1;
}
goto b;
return EXIT_SUCCESS;

}
IV.
1. Write a program that can divide six non-zero integers (two integers per division) from the user and display
the result to the user. Create a function that will perform the division operation. Display only the non-decimal
part of the quotient.

#include<cstdlib>
#include<cstring>
#include<iostream>

using namespace std;


int qoutient(int a, int b){
return a/b;
}
int main(){
cout << "Enter the Six None Zero Integer:\n";
int n[6];
for (int i = 0; i < 6; i++){

cout << "Number["<< i+1 <<"]:";


cin >> n[i];
}
cout << "Number[1] devided by Number[2] = "<< qoutient(n[0],n[1]) << "\n";
cout << "Number[3] devided by Number[4] = "<< qoutient(n[2],n[3]) << "\n";
cout << "Number[5] devided by Number[6] = "<< qoutient(n[4],n[5]) << "\n";
cout << endl;

system("pause");
return EXIT_SUCCESS;
}

2. Write a program that will accept a short value from 10 to 99 and display them per digit (separated by a
space).

#include<cstdlib>
#include<iostream>

using namespace std;


int sum(int a, int b){
return a+b;
}
int main(){
for (int i = 9; i < 99; i++){

cout << sum(i, 1) << "\t";


}
cout << endl;

system("pause");
return EXIT_SUCCESS;
}
3. Write a program that will display the nth Fibonacci number. Create a function that will generate the nth
Fibonacci number. Fibonacci numbers are numbers from the Fibonacci sequence which follows the pattern
of 1, 1, 2, 3, 5, 8, 13, 21, 33, 54…

#include<cstdlib>
#include<iostream>

using namespace std;


int sum(int a, int b){
return a+b;
}
int main(){
int First = 0,Second = 1,Third;
int n;
cout << "Enter the nth Fibonacci number: ";
cin >> n;
cout << Second << ", ";
for (int i = 0; i < n-1; i++){

Third = sum(First, Second);


First = Second;
Second = Third;
cout << Third << ", ";
}
cout << endl;

system("pause");
return EXIT_SUCCESS;
}

4. What can you conclude from this activity?

In this lesson I finished the activities that are really fun and very fun to study. Here I learned how to
operate the seating reservation, Fibonacci and more.
V.

1. Write a class that will represent a LeggedMammal. Consider the number of legs, kind of fur, presence of tail.

main.cpp

#include <cstdlib>
#include <iostream>
#include "LeggedMammal.h"

using namespace std;

int main()
{
LeggedMammal animal("Cat", "Fluffy", 4, true);

cout << "**********Mammal Details********** \n\n" << animal.getLeggedMammal() << endl;

cout << endl;


return EXIT_SUCCESS;
}

LeggedMammal.cpp

#include <string>
#include <sstream>
using namespace std;

class LeggedMammal {
private:
string sName, sFur;
short nLegs;
bool bTail;

public:
LeggedMammal(string name, string fur, short legs, bool hasTail) {
sName = name;
sFur = fur;
nLegs = legs;
bTail = hasTail;
}

string getLeggedMammal() {
ostringstream s;
s << "Name of Mammal : " << sName << endl;
s << "Kind of Fur : " << sFur << endl;
s << "Number of Legs : " << nLegs << endl;
s << "Presence of Tail : " << bTail << endl;
return s.str();
}
};

2. Write a class that will represent a Person. Consider the name, address, gender, age and occupation.

main.cpp

#include <cstdlib>
#include <iostream>
#include "Person.h"

using namespace std;

int main()
{
Person pip("Carl Valiant", "Caloocan City", "Female", 37, "Senior Programmer");

cout << "**********Personal Details********** \n\n" << pip.getInformation() << endl;

cout << endl;


return EXIT_SUCCESS;
}

Person.h

#include <string>
#include <sstream>
using namespace std;

class Person {
public:
string Name, Address, Gender, Occupation;
short Age;
Person(string name, string address, string gender, short age, string occupation);
string getInformation();
};
Person::Person(string name, string address, string gender, short age, string occupation){
Name = name;
Address = address;
Gender = gender;
Age = age;
Occupation = occupation;
}
string Person::getInformation(){
ostringstream s;
s << "Name: " << Name << endl;
s << "Address: " << Address << endl;
s << "Gender: " << Gender << endl;
s << "Age: " << Age << endl;
s << "Occupation: " << Occupation << endl;
return s.str();
}

3. Write a class that will represent Polygon. Consider the name, number of sides and color.

main.cpp

#include <cstdlib>
#include <iostream>
#include "Polygon.h"

using namespace std;

int main()
{
Polygon shape("Triangle", "Red", 3);

cout << "**********Polygon Details**********\n\n" << shape.getInformation() << endl;

cout << endl;


return EXIT_SUCCESS;
}

Polygon.h

#include <string>
#include <sstream>
using namespace std;

class Polygon {

public:
string Name, Color;
short Sides;
Polygon(string name, string color, short sides);
string getInformation();
};

Polygon::Polygon(string name, string color, short sides){


Name = name;
Color = color;
Sides = sides;
}

string Polygon::getInformation(){
ostringstream s;
s << "Name: " << Name << endl;
s << "Color: " << Color << endl;
s << "Number of Sides: " << Sides << endl;
return s.str();
}

4. What can you conclude from this activity?

These trainings are helpful because they allow us to take classes in our programs. Doing so was more exciting and
challenging at the time we were together.
VI.

1. Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will
represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in
the new constructor. This time, promote the use of accessors and mutators for the new properties.
Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object
using the mutators. Display all the properties of the Dog object using the accessors.

main.cpp

#include <cstdlib>
#include <iostream>
#include "Dog.h"

using namespace std;

int main()
{
Dog pet("Dog", "Wavy", 4, true);
pet.setBreed("Chihuahua");
pet.setSize("Small");
pet.setIsRegister(false);
cout << "**********Dog Details**********\n\n" << pet.getLeggedMammal();
cout << "Breed: " << pet.getBreed() << endl;
cout << "Size: " << pet.getSize() << endl;
cout << "Is Registered: " << pet.getIsRegister() << endl;
cout << endl;
return EXIT_SUCCESS;
}

Dog.h

#include <string>
#include <sstream>
#include "LeggedMammal.h"

using namespace std;

class Dog : public LeggedMammal {


private:
string _breed, _size;
bool _isRegister;

public:
Dog(string name, string fur, short legs, bool hasTail)
:LeggedMammal(name,fur,legs, hasTail) {
}

void setBreed(string breed) {


this->_breed = breed;
}

string getBreed() {
return _breed;
}

void setSize(string size) {


this->_size = size;
}

string getSize() {
return _size;
}

void setIsRegister(bool isregister) {


this->_isRegister = isregister;
}

bool getIsRegister() {
return _isRegister;
}

string getInformation() {
ostringstream s;
s << this->getLeggedMammal();
s << "Breed: " << getBreed() << endl;
s << "Size: " << getSize() << endl;
s << "Is Registered: " << getIsRegister() << endl;
return s.str();
}
};
2. Write a class that extends the Person class from the previous laboratory exercise. The class will represent a
Student. Consider the academic program, year in college and enrolled university. Initialize all the properties
of the parent class in the new constructor. This time, promote the use of accessors and mutators for the
new properties. Instantiate a Student object in the main function and be able to set the values of the
properties of the Student object using the mutators. Display all the properties of the Student object using the
accessors.

main.cpp

#include <cstdlib>
#include <iostream>
#include "Student.h"

using namespace std;

int main()
{
Student student("Carl Valiant", "Caloocan City", "Female", 22, "Senior Programmer");
student.setProgram("BSIT");
student.setYearsInCollege(1);
student.setUniversity("University of Manila");
cout << "**********Student Details**********\n\n" << student.getInformation();
cout << "Academic Program: " << student.getProgram() << endl;
cout << "Years in College: " << student.getYearsInCollege() << endl;
cout << "University: " << student.getUniversity() << endl;

cout << endl;


return EXIT_SUCCESS;
}

Student.h

#include <string>
#include <sstream>
#include "Person.h"

using namespace std;

class Student : public Person{

private:
string _program, _university;
short _years;
public:
Student(string name, string address, string gender, short age, string occupation)
:Person(name, address, gender, age, occupation) {

void setProgram(string program) {


this->_program = program;
}

string getProgram() {
return _program;
}

void setUniversity(string university) {


this->_university = university;
}

string getUniversity() {
return _university;
}

void setYearsInCollege(short years) {


this->_years = years;
}

short getYearsInCollege() {
return _years;
}

string getInformation(){
ostringstream s;
s << this->Person::getInformation();
s << "Program: " << getProgram() << endl;
s << "Years in College: " << getYearsInCollege() << endl;
s << "University: " << getUniversity() << endl;
return s.str();
}
};
3. What can you conclude from this activity?

In this lesson I learned a lot. I learned how to create a class and connect to another class. It's very fun and
exciting to code especially when you follow all the lessons. I can say I learned a lot.
VII.

Write a C++ program with the following specifications:


1. Create a class and name it Payslip. This class should have the following attributes or properties: name, pay
grade, basic salary, overtime hours, overtime pay, gross pay, net pay and withholding tax.
2. Define the accessors and mutators of the Payslip class based on its attributes or properties. This class
should also have the following methods: determinePayGradeAndTaxRate and computePay.
3. Create another class and name it Employee. This class will contain the main method.
4. In the main method, instantiate an object of the Payslip class. Input the employee name, basic salary, and
number of overtime (OT) hours. These values must be set to the object of the Payslip class. Apply
validations for input as follows:
a. Basic salary should not be less than 10,000.
b. Minimum overtime hours is 1 hour.
5. Basic Salary Details:
Pay Grade A Pay Grade B Tax Rate
10,000 15,000 10%
20,000 25,000 15%
30,000 35,000 20%
40,000 45,000 25%
50,000 55,000 30%

6. The computation is as follows:


Gross pay = basic salary + OT pay
OT pay = no. of OT hours * 1% of basic salary
Net pay = gross pay – withholding tax – fixed values
Withholding tax = gross pay * tax rate
Note: Basic salary greater than or equal to 55,000 will have a pay grade of B and a tax rate of 30%.

7. The following are fixed values:


SSS = 500.00
Pag-ibig = 200.00
Philhealth = 100.00

8. Output should contain the following:

Employee Name :
Basic Salary :
Pay Grade :
No. of OT Hours :
OT Pay :
Gross Pay :
Withholding Tax :
Net Pay :

Note: Input of data and display of results should be defined on the main method. All monetary display
should be formatted with comma separators, 2 decimal places, and “Php” (Ex: Php 32,546.95).

Payslip Class

#include <string>
#include <iostream>

using namespace std;

class payslip{
private:
string mName, mPayGrade;
float mGrossPay, mOTHours, mTaxRate, mSalary, mOTPay, mNetPay, mWithholdingTax;
float mFixedValuesTotal, SSS = 500.00, Pagibig = 200.00, Philhealth = 100.00;

public:
void setPayslip(string Name, float BasicPay, float OTHour){
mName = Name;
mSalary = BasicPay;
mOTHours = OTHour;
}

void DeterminePayGradeandTaxRate(){
if(mSalary == 10000){
mPayGrade = "A";
mTaxRate = 0.10;
}
else if(mSalary == 15000){
mPayGrade = "B";
mTaxRate = 0.10;
}
else if(mSalary == 20000){
mPayGrade = "A";
mTaxRate = 0.15;
}
else if(mSalary == 25000){
mPayGrade = "B";
mTaxRate = 0.15;
}
else if(mSalary == 30000){
mPayGrade = "A";
mTaxRate = 0.20;
}
else if(mSalary == 35000){
mPayGrade = "B";
mTaxRate = 0.20;
}
else if(mSalary == 40000){
mPayGrade = "B";
mTaxRate = 0.25;
}
else if(mSalary == 45000){
mPayGrade = "B";
mTaxRate = 0.25;
}
else if(mSalary == 50000){
mPayGrade = "A";
mTaxRate = 0.30;
}
else if(mSalary == 55000){
mPayGrade = "B";
mTaxRate = 0.30;
}
}

void ComputePay(){
mGrossPay = mSalary + mOTPay;
mOTPay = mOTHours * (mSalary * 0.01);
mNetPay = mGrossPay - mWithholdingTax - mFixedValuesTotal;
mWithholdingTax = mGrossPay * mTaxRate;
mFixedValuesTotal = SSS + Pagibig + Philhealth;
}

double getNetPay(){
return mNetPay = mGrossPay - mWithholdingTax - mFixedValuesTotal;
}

string getName(){
return mName;
}

string getPayGrade(){
return mPayGrade;
}

float getOTHours(){
return mOTHours;
}

float getOTPay(){
return mOTPay = mOTHours * (mSalary * 0.01);
}

float getGrossPay(){
return mGrossPay = mSalary + mOTPay;
}

float getWithTax(){
return mWithholdingTax = mGrossPay * mTaxRate;
}

float getBasicSalary(){
return mSalary;
};
};

Employee Class

#include <iostream>
#include "Payslip.h"

using namespace std;

int main()
{
string name;
float salary, hours;
bool x,y;

cout << "Enter Employee Name: ";


getline(cin, name);

do{
cout << "Enter Salary: ";
cin >> salary;
if(salary < 10000){
cout << "Basic salary should not be less than 10,000";
}
else
break;
}

while(x=true);
do{
cout << "Enter OT Hours: ";
cin >> hours;
if(hours < 1){
cout << "Minimum overtime hours is 1 hour";
}
else
break;
}

while(y=true);
payslip employee;
employee.setPayslip(name, salary, hours);
employee.DeterminePayGradeandTaxRate();
employee.ComputePay();

cout << "" << endl;


cout << "Employee Name: " << employee.getName() << endl;
cout << "Basic Salary: " << employee.getBasicSalary() << endl;
cout << "Pay Grade: " << employee.getPayGrade() << endl;
cout << "No. of OT Hours: " << employee.getOTHours() << endl;
cout << "OT Pay: " << employee.getOTPay() << endl;
cout << "Gross Pay: " << employee.getGrossPay() << endl;
cout << "Withholding Tax: " << employee.getWithTax() << endl;
cout << "Net Pay: " << employee.getNetPay() << endl;

system("pause");
return 0;
}

VIII.

1. Write a program that will display the value and logical address of an integer variable with an initial value of
900.

#include <iostream>

using namespace std;

int main()
{
int a = 900;

cout << "The value of a is: " << a << endl;


cout << "The logical value of a is: " << &a << endl;

system("pause");
return 0;
}
2. Write a program that will display the value and logical address of an uninitialized character array with size
ten (10) and a pointer pointing to the array. (Hint: you may need to perform some casting.)

#include <iostream>
#include <string>

using namespace std;

int main()
{
char a[10];
char *p;
p = (char*) &a;

cout << "The value of a is: " << a << endl;


cout << "The logical value of a is: " << &a << endl;
cout << "The value of pointer of a is: " << p << endl;

system("pause");
return 0;
}
3. Write a program that will display the value and logical address of an uninitialized float array with size twenty
(20) and a reference pointing to the array.

#include <iostream>
#include <string>

using namespace std;

int main()
{
float a[20];
float (&r)[20] = a;

cout << "The value of a is: " << a << endl;


cout << "The logical value of a is: " << &a << endl;
cout << "The value of reference is: " << r << endl;

system("pause");
return 0;
}
4. What can you conclude from this activity?

In this activity I learn how to use pointers and reference variables and how to use them
temporary store within memory
IX.
1. Write a program that will change the value of an integer variable with initial value of 654,321 to 27,946
without directly assigning a value to the variable. You cannot create any pointersor references in the main
function.

#include <iostream>

using namespace std;

void setValue(int& b){


b = 27946;
}

int main()
{
int a = 654321;

cout << "The value of a is: " << a << endl;

setValue(a);
cout << "The value of a after setValue is: " << a << endl;

return EXIT_SUCCESS;
}
2. Write a program that will display the address of a float variable and another variable that shares the same
address and value as the first variable. Do not initialize the first variable.

#include <iostream>

using namespace std;

void pause(){
cout << endl << "Press any key to continue...";
}

int main()
{
int a = 100;
int* b = &a;

cout << "The value of a is: " << a << endl;


cout << "The value of b is: " << b << endl;
cout << "The value of b is: " << *b << endl;
cout << endl;

a = 200;
cout << "The value of a is: " << a << endl;
cout << "The value of b is: " << *b << endl;

pause();
return EXIT_SUCCESS;
}

3. Write a program that will display the words “This is it!” from a variablewithout assigning any characters to the
variable. You cannot use cout << “This is it!” << endl; or any variants of it.

#include <iostream>
#include <stdio.h>

using namespace std;

void setRef(string& b){


b = "This is it!";
}

int main()
{
string a;

setRef(a);
printf("%s\n", a.c_str());

return EXIT_SUCCESS;

}
4. What can you conclude from this activity?

Points and references are useful in storing and pointing to other components.

You might also like