Sunteți pe pagina 1din 8

Object Oriented Programming

Lab 1

Muhammad Mohsin
F-63078
BS(CS) 32-B

Department of Computer Science


APCOMS
APCOMS, Rawalpindi
Lab # 1:

Objectives:
1. Write a program in C to swap two numbers using function and also print their square.
2. Write a program with a function volume () which accepts three sides of a cube and returns its
volume. Provide a default value of 1 for all the three sides of a cube. Call this function with zero,
one, two and three arguments and display the volume returned in the main ().
3. Write a program that have three function getData(), divide(),print().In main() declare the two
numbers, pass the reference to get function and input the numbers. Divide function accept 4
arguments and returns the quotient and remainder of two numbers.
4. Write a C++ program which reads a string, less than 10 characters long. This string represents an
integer expressed in roman numbers. Let a function convert the number from roman to arabic
form (i.e., our standard digits). Let then the main program writes out both forms. The roman
numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1.

Examples:
LXXXVII = 87
CCXIX = 219
MCCCLIV = 1354
MMDCLXXIII = 2673

Tools Used:

Application used to design these programs is Microsoft Visual Studio.

Submission Date: 21-Feb-2020

Evaluation Marks: Signature: Sir M.Emad Amjad


Task # 1:
Write a program in C to swap two numbers using function and also print their square.
Procedure/Program:
//HEADER FILES
#include <iostream>
using namespace std;
//FUNCTION PROTOTYPE
void swap(int&, int&, int&);
void square(int&, int&);
//MAIN BODY
void main()
{
//HEADING
cout << "\n\t\t\t\t\t\t\"APCOMS\"\n\t\t\t\t\t\t--------\n";
cout << "\n\t\t\"Program that swaps the values of the two pre-defined integer variables and print square
after swaping.\"";
cout << "\n\t\t--------------------------------------------------------------------------------------------------------
\n\n";
//PRE-DEFINED INTEGER VARIABLES
int numb1 = 100;
int numb2 = 200;
//INTEGERS VALUE BEFORE SWAP
cout << " => Before swap, value of first integer = " << numb1 << "\n\n";
cout << " => Before swap, value of second integer = " << numb2 << "\n\n";
//INTEGERS VALUE AFTER SWAP
swap(numb1, numb2);
cout << " => After swap, value of first integer = " << numb1 << "\n\n";
cout << " => After swap, value of second integer = " << numb2 << "\n\n";
square(numb1, numb2);
cout << " => After square, value of first integer = " << numb1 << "\n\n";
cout << " => After square, value of second integer = " << numb2 << "\n\n";
cout << "\n\t\"End\"\n\n";
system("pause");
}
//FUNCTION DEFINATION
void swap(int& num1, int& num2, int& diff)
{
diff = num1;
num1 = num2;
num2 = diff;
}
void square(int& number1, int& number2)
{
number1 = number1 * number1;
number2 = number2 * number2;
}
//END
Result/Output:

Task # 2:
Write a program with a function volume () which accepts three sides of a cube and returns its volume.
Provide a default value of 1 for all the three sides of a cube. Call this function with zero, one, two and
three arguments and display the volume returned in the main ().
Procedure/Program:
//HEADER FILES
#include <iostream>
using namespace std;
//FUNCTION PROTOTYPE
float volume(float s1 = 1, float s2 = 1, float s3 = 1);
//MAIN BODY
void main()
{
//HEADING
cout << "\n\t\t\t\t\"APCOMS\"\n\t\t\t\t--------\n";
cout << "\n\t\"Program that print the volume of cube with zero, one, two and three arguments\"";
cout << "\n\t-------------------------------------------------------------------------------\n\n";
cout << " INPUT:\n" << " ======\n\n";
float num1, num2, num3, result1;
cout << " => Enter first side = ";
cin >> num1;
cout << "\n => Enter second side = ";
cin >> num2;
cout << "\n => Enter third side = ";
cin >> num3;
cout << "\n\n OUTPUT:\n" << " =======\n\n";
result1 = volume(num1, num2, num3);
cout << "\n => Volume with three arguments = " << result1 << "\n";
result1 = volume(num1, num2);
cout << "\n => Volume with two arguments = " << result1 << "\n";
result1 = volume(num1);
cout << "\n => Volume with one arguments = " << result1 << "\n";
result1 = volume();
cout << "\n => Volume with default arguments = " << result1 << "\n";
cout << "\n\t\"End\"\n\n";
system("pause");
}
//FUNCTION DEFINATION
float volume(float s1, float s2, float s3)
{
return (s1 * s2 * s3);
}
//END

Result/Output:

Task # 3:
Write a program that have three function getData(), divide(),print().In main() declare the two
numbers, pass the reference to get function and input the numbers. Divide function accept 4
arguments and returns the quotient and remainder of two numbers.
Procedure/Program:
//HEADER FILES
#include <iostream>
using namespace std;
//FUNCTIONS PROTOTYPE
void getData (int &, int &);//INPUT FUNCTION
void divide (int , int , int &,int &);//QUOTIENT AND REMINDER FUNCTION
void print (int &,int &);//PRINT OUTPUT FUNCTION
//MAIN BODY
void main()
{
//HEADING
cout<<"\n\t\t\t\t\t\t\"APCOMS\"\n\t\t\t\t\t\t--------\n";
cout<<"\n\t\t\"Program that calculate the qoutient and reminder of two numbers.\"";
cout<<"\n\t\t------------------------------------------------------------------\n\n";
int number1,number2,quotient,reminder;
getData (number1,number2);
divide (number1,number2,quotient,reminder);
print (quotient,reminder);
system ("pause");
}
//FUNCTIONS DEFINATION
void getData (int &num1, int &num2)
{
cout<<" => Enter dividend = ";
cin>>num1;
cout<<"\n => Enter divisor = ";
cin>>num2;
}
void divide (int nu1, int nu2, int &quot,int &remd)
{
quot=nu1/nu2;
remd=nu1%nu2;
}
void print (int &quo,int &rem)
{
cout<<"\n => Quotient = "<<quo<<"\n";
cout<<"\n => Reminder = "<<rem<<"\n\n";
}
//END

Result/Output:

Task # 4:
Write a C++ program which reads a string, less than 10 characters long. This string represents an
integer expressed in roman numbers. Let a function convert the number from roman to arabic form
(i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are
written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1.
Examples:
LXXXVII = 87
CCXIX = 219
MCCCLIV = 1354
MMDCLXXIII = 2673
Procedure/Program:
//HEADER FILES
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
//FUNCTIONS PROTOTYPE
int Roman_value(char);
int romanToInt(string);

//Main Function
void main()
{
//HEADING
cout << "\n\t\t\t\t\t\"APCOMS\"\n\t\t\t\t\t--------\n";
cout << "\n\t\t\"Program that converts from numbers roman to integer.\"";
cout << "\n\t\t------------------------------------------------------\n";
cout << "\t\t\t\"ALERT!! MAKE SURE CAPSLOCK IS ON\"";
cout << "\n\t\t\t----------------------------------\n\n";
cout << " INPUT:\n" << " ======\n\n";
string Input_S;
int Result;
cout << " => Enter the roman numbers = ";
cin >> Input_S;
Result = romanToInt(Input_S);
cout << "\n\n OUTPUT:\n" << " =======\n\n";
cout << " => " <<Input_S<<" = "<< Result << "\n";
}

//Function to retrun value of roman symbols


int Roman_value(char roman_num)
{
switch (roman_num)
{
case 'I':return 1;break;
case 'V':return 5;break;
case 'X':return 10;break;
case 'L':return 50;break;
case 'C':return 100;break;
case 'D':return 500;break;
case 'M':return 1000;break;
}
}

//Function to convert Roman Numerals to Integer


int romanToInt(string Entered_S)
{
int i, String_Length= Entered_S.length(), ans = 0, Temp = 0;

for (i = String_Length - 1; i >= 0; i--)


{
if (Roman_value(Entered_S[i]) >= Temp)
{
ans = ans + Roman_value(Entered_S[i]);
}
else
{
ans = ans - Roman_value(Entered_S[i]);
}
Temp = Roman_value(Entered_S[i]);
}
return ans;
}
//END
Result/Output:

S-ar putea să vă placă și