Universiti Malaysia Sarawak: Object Oriented Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

UNIVERSITI MALAYSIA SARAWAK

FACULTY OF ENGINEERING ELECTRICAL AND


ELECTRONIC ENGINEERING DEPARTMENT

KNR1743

Object Oriented Programming

ASSIGNMENT 1
Lecturer`s Name: Ts. Dr Maimun binti Huja Husin
Group Members : GROUP3_4: G2
1. YOHANES HANYIE (86100)

2. WONG HAI HUI (86063)

3. SAVIO GINO AK MACGRIGO (85610)


4. WASZEL DAHING ANAK KONZOE (86041)
5. SEBASTIAN ALDRIN NOEL (85617)
Assignment 1

Part 1

*1. Assuming there are 7.481 gallons in a cubic foot, write a program that asks
the user to enter a number of gallons, then displays the equivalent in cubic
feet.
//ASSIGNMENT 1 PART 1 NO.1//
#include <iostream>

using namespace std;


int main()
{
float gallons, cufeet;
cout << "\nEnter quantity in gallons : ";
cin >> gallons;
cufeet = gallons / 7.481;
cout << "Equivalent in cublic feet is " << cufeet << endl;
return 0;

OUTPUT

*2. Write a program that generates the following table:


1990 135
1991 7290
1992 11300
1993 16200
Use a single cout statement for all output.
//ASSIGNMENT 1 PART 1 NO.2//
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << 1990 << setw(8) << 135 << endl;
cout << 1991 << setw(8) << 7290 << endl;
cout << 1992 << setw(8) << 11300 << endl;
cout << 1993 << setw(8) << 16200 << endl;
return 0;
}
OUTPUT

*3. Write a program that generates the following output:


10
20
19
Use an integer constant for the 10, ab arithmetic assignment operator to
generate the 20, and a decrement operator to generate the 19.
//ASSIGNMENT 1 PART 1 NO.3//
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
const int a =10;
cout<<a<<endl;
int b=a+10;
cout<<b<<endl;
int c=--b;
cout<<c;

return 0;
}

OUTPUT
4. On a certain day the British pound was equivalent to $1.487 U.S., the French
franc was $0.172, the German deutschemark was $0.584, and the Japanese
yen was $0.00955. Write a program that allows the user to enter an amount
in dollars, and then displays this value converted to these four other monetary
units.
#include <iostream>
using namespace std;
int main()
{
float ammount=0, british = 1.487, franc = 0.172, deutsche = 0.584, yen =
0.00955;
cout << "Enter the ammount in US Dollar == ";
cin >> ammount;
cout << "British == " << ammount*british << endl;
cout << "French == " << ammount*franc << endl;
cout << "German == " << deutsche*ammount << endl;
cout << "Japanese == " << ammount*yen << endl;
return 0;

OUTPUT
5. You can convert temperature from degrees Celsius to degrees Fahrenheit by
multiplying by 9/5 and adding 32. Write a program that allows the user to
enter a floating-point number representing degrees Celsius, and then displays
the corresponding degrees Fahrenheit.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float Cel, Faren;
cout<<"Enter temperature in degree celsius:";
cin>>Cel;
Faren=(Cel*9/5)+32;
cout<<"Temperature in Farenheit: "<<Faren;

return 0;
}

OUTPUT
6. If you have two fractions, a/b and c/d, their sum can be obtained from the
formula
𝑎 𝑐 𝑎∗𝑑+𝑏∗𝑐
+ =
𝑏 𝑑 𝑏∗𝑑
For example, 1/4 + 2/3 is
1 2 1∗3+4∗2 3+8 11
+ = = =
4 3 4∗3 12 12
Write a program that encourages the user to enter two fractions, and then
displays their sum in fractional form. (You don’t need to reduce it to lowest
terms.) The interaction with the user might look like this:
Enter first fraction: 1/2
Enter second fraction: 2/5
Sum = 9/10
You can take advantage of the fact that the extraction operator (>>) can be
chained to read in more than one quantity at once:
cin>>a>>dummychar>>b;
#include <iostream>

using namespace std;

int main()
{
int a,b,c,d,e,f;
char ch;
cout<<"Enter first fraction in the form of a/b : ";
cin>>a>>ch>>b;
cout<<"Enter second fraction in the form of c/d:";
cin>>c>>ch>>d;
e=a*d+b*c;
f=b*d;
cout<<endl;
cout<<a<<"/"<<b<<"+"<<c<<"/"<<d<<"="<<e<<"/"<<f;
return 0;
}

OUTPUT
7. By default, output is right-justified in its field. You can left-justify text output
using the manipulator setiosflags(ios::left). Use this manipulator, along with
setw(), to help generate the following output:
Last name First name Street address Town State
Jones Bernard 109 Pine Lane Littletown MI
O’Brian Coleen 42 E. 99th Ave. Bigcity NY
Wong Harry 121-A Allabama St. Lakeville IL
#include<iostream>
#include<conio.h>
#include <iomanip>
using namespace std;
int main()
{
cout<<setiosflags(ios::left)<<setw(12)<<"Last Name"<<setw(13)<<"First
Name"<<setw(18) <<"Street
address"<<setw(12)<<"Town"<<setw(5)<<"State"<<endl;

cout<<setiosflags(ios::left)<<setfill('-
')<<setw(12)<<""<<setw(13)<<""<<setw(18)<<""<<setw(12)<<""<<setw(5)<<""<<
endl;

cout<<setiosflags(ios::left)<<setfill('
')<<setw(12)<<"Jones"<<setw(13)<<"Bernard"<<setw(18)<<"109 Pine
Lane"<<setw(12)<<"Littletown"<<setw(5)<<"MI"<<endl;

cout<<setiosflags(ios::left)<<setw(12)<<"O’Brian"<<setw(13)<<"Coleen"<<setw(18
)<<"42 E. 99th Ave."<<setw(12)<<"Bigcity"<<setw(5)<<"NY"<<endl;

cout<<setiosflags(ios::left)<<setw(12)<<"Wong"<<setw(13)<<"Harry"<<setw(18)<
<"121-A Alabama St."<<setw(12)<<"Lakeville"<<setw(5)<<"IL"<<endl;

return 0;
}

OUTPUT

* ALL GROUP
Part 2

*1. Assume that you want to generate a table of multiples of any given number.
Write a program that allows the user to enter the number and then generates
the table, formatting it into 10 columns and 20 lines. Interaction with the
program should look like this (only the first three lines are shown):
Enter a number:
7 14 21 28 35 42 49 56 63 70
77 84 91 98 105 112 119 126 133 140
147 154 161 168 175 182 189 196 203 210
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int numb;
cout<<"enter your number: ";
cin>>numb;
for (int x=0;x<200;x+=10)
{
for(int n=1;n<=10;n++)
{
cout<<setw(5)<<(x+n)*numb;
}
cout<<endl;
}
return 0;
}

OUTPUT
*2. Write a temperature-conversion program that gives the user the option of
converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the
conversion. Use floating-point numbers. Interaction with the program might
look like this:
Type 1 to convert Fahrenheit to Celsius,
2 to convert Celsius to Fahrenheit: 1
Enter temperature in Fahrenheit: 70
In Celsius that’s 21.111111
#include<iostream>
using namespace std;
int main()
{
int opt;
float c, f;

cout<<"(1) f to c or (2) c to f:";


cin>>opt;

if(opt==1)
{
cout<<"Enter f: ";
cin>>f;
c=(f-32)/1.8000;
cout<<"In c that's "<<c<<endl;
}
else if(opt==2)
{
cout<<"Enter c: ";
cin>>c;
f=(c*9/5)+32;
cout<<"In f that's "<<f<<endl;
}
else
{
cout<<"Invalid.";
}

return 0;
}

OUTPUT
*3. Create the equivalent of a four-function calculator. The program should ask
the user to enter a number, an operator, and another number. (Use floating
point.) It should then carry out the specified arithmetic: adding, subtracting,
multiplying, or dividing the two numbers. Use switch statement to select the
operation. Finally, display the result.
When it finishes the calculation, the program should ask whether the user
wants to do another calculation. The response can be ‘y’ or ‘n’. Some sample
interaction with the program might look like this:
Enter first number, operator, second number: 10 / 3
Answer = 3.333333
Do another (y/n) ? y
Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n) ? n
#include<bits/stdc++.h>
using namespace std;
int main()
{
float num1,num2;
double answer;
char dummychar,ch;
do{
cout<<"Enter first number, operator, second number:";
cin>>num1>>dummychar>>num2;
cout<<endl;
switch(dummychar)
{

case '+':
answer= num1+num2;
break;

case '-':
answer= num1-num2;
break;

case '*':
answer=num1*num2;
break;
case '/':
answer= num1/num2;
break;

default:
answer=0;
}
cout<<"Displaying the calculation:"<<answer<<endl;
cout<<"You want to do another calculation(y or n):";
cin>>ch;

}while(ch!='n');
cout<<"\n End! ";
return 0;
}

OUTPUT
4. Use for loops to construct a program that displays a pyramid of Xs on the
screen. The pyramid should look like this
X
X X X
X X X X X
X X X X X X X
X X X X X X X X X
Except that it should be 20 lines high, instead of the 5 lines shown here. One
way to do this is to nest two inner loops, one to print spaces and one to print
Xs, inside an outer loop that steps down the screen form line to line.
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
int i, j, k, c=20;
for(i=1;i<=20;i++)
{
for(k=1;k<c;k++)
{
cout<<" ";
}
for(j=1;j<=(2*i)-1;j++)
{
cout<<"X";
}
cout<<endl;
c--;
}

return 0;
}

OUTPUT
5. Write a program that calculates how much money you’ll end up with if you
invest an amount of money at a fixed interest rate, compounded yearly.
Have the user furnish the initial amount, the number of years, and the yearly
interest rate in percent. Some interaction with the program might look like
this:
Enter initial amount: 3000
Enter number of years: 10
Enter the interest rate (percent per year): 5.5
At the end of 10 years, you will have 5124.43 dollars.
At the end of the first year you have 3000+(3000*0.055), which is 3165. At
the end of the second year you have 3165+(3165*0.055), which is 3339.08.
do this as many times as there are years. A for loop makes the calculation
easy.
#include <iostream>
using namespace std;
int main()
{
int amount, years;
float rate, total;

cout<<"Enter initial amount: ";


cin>>amount;
cout<<"Enter number of years: ";
cin>>years;
cout<<"Enter interest rate (percent per year): ";

cin>>rate;
rate=rate/100;
total=amount;

for(int i=0;i<years;i++){
total=total + (total * rate);
}
cout<<"At the end of "<<years<<" years you will have "<<total<<" dollars";

return 0;
}

OUTPUT
6. Write another version of the program from (5) so that, instead of finding the
final amount of your investment, you tell the program the final amount and
it figures out how many years it will take, at a fixed rate of interest
compounded yearly, to reach this amount. What sort of loop is appropriate
for this problem? Use an integer value for the year.
[G3&G7]
7. Create a four-function calculator for fractions. Here are the formulas for the
four arithmetic operations applied to fractions:
Addition: a/b + c/d=(a*d + b*c) / (b*d)
Subtraction: a/b - c/d=(a*d - b*c) / (b*d)
Multiplication: a/b * c/d=(a*c) / (b*d)
Division: a/b / c/d=(a*d) / (b*c)
The user should type the first fraction, an operator, and a second fraction.
The program should then display the result and ask whether the user wants
to continue.
#include<bits/stdc++.h>
using namespace std;
void addition(int a,int b,int c,int d){
int add1=(a*d+b*c);
int add2=b*d;
cout<<"Display adadition: "<<add1<<"/"<<add2<<endl;
}
void subtraction(int a,int b,int c,int d){
int sub1=a*d-b*c;
int sub2=b*d;
cout<<"Subtraction: "<<sub1<<"/"<<sub2<<endl;
}
void multiplication(int a,int b,int c,int d){
int mul1=a*c;
int mul2=b*d;
cout<<"Multiplication: "<<mul1<<"/"<<mul2<<endl;
}
void division(int a,int b,int c,int d){
int div1=a*d;
int div2=b*c;
cout<<"Division: "<<div1<<"/"<<div2<<endl;
}
int main()
{
int a,b,c,d;
char dummychar,ch;
do{
cout<<"Enter the first fraction number (a/b):";
cin>>a>>dummychar>>b;
cout<<endl;
cout<<"Enter the second fraction number (c/d):";
cin>>c>>dummychar>>d;
cout<<endl;
addition(a,b,c,d);
subtraction(a,b,c,d);
multiplication(a,b,c,d);
division(a,b,c,d);
cout<<"Would you like to continue calculation again (y/n):";
cin>>ch;
cout<<endl;
}while(ch!='n');
return 0;
}
OUTPUT

* ALL GROUP

You might also like