Programming Laboratory Activities Questions: I
Programming Laboratory Activities Questions: I
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>
int main()
{
int Height, Width=0, Line;
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>
int main()
{
int Height;
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>
int main(){
int x[5], i;
cout << "Enter 5 numbers: " << endl;
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>
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;
}
}
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>
int main()
{
int x[6], i, j, temp;
cout << "Enter 6 numbers to Sort:" << endl;
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.
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
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
#include<iostream>
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;
}
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>
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>
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>
system("pause");
return EXIT_SUCCESS;
}
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"
int main()
{
LeggedMammal animal("Cat", "Fluffy", 4, true);
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"
int main()
{
Person pip("Carl Valiant", "Caloocan City", "Female", 37, "Senior Programmer");
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"
int main()
{
Polygon shape("Triangle", "Red", 3);
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();
};
string Polygon::getInformation(){
ostringstream s;
s << "Name: " << Name << endl;
s << "Color: " << Color << endl;
s << "Number of Sides: " << Sides << endl;
return s.str();
}
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"
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"
public:
Dog(string name, string fur, short legs, bool hasTail)
:LeggedMammal(name,fur,legs, hasTail) {
}
string getBreed() {
return _breed;
}
string getSize() {
return _size;
}
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"
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;
Student.h
#include <string>
#include <sstream>
#include "Person.h"
private:
string _program, _university;
short _years;
public:
Student(string name, string address, string gender, short age, string occupation)
:Person(name, address, gender, age, occupation) {
string getProgram() {
return _program;
}
string getUniversity() {
return _university;
}
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.
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>
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"
int main()
{
string name;
float salary, hours;
bool x,y;
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();
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>
int main()
{
int a = 900;
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>
int main()
{
char a[10];
char *p;
p = (char*) &a;
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>
int main()
{
float a[20];
float (&r)[20] = a;
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>
int main()
{
int a = 654321;
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>
void pause(){
cout << endl << "Press any key to continue...";
}
int main()
{
int a = 100;
int* b = &a;
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>
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.