Sie sind auf Seite 1von 11

Object Oriented Programming

CSL-210

LAB JOURNAL

Name: _______________________________

Enrollment No: __________________________________

Section: _________________________________________

DEPARTMENT OF ELECTRICAL ENGINEERING

BAHRIA UNIVERSITY ISALMABAD CAMPUS


Exp. Obtained
Experiment Name Date Sign
No Marks

Total Marks: _______________ Signature: _________________

2
EXPERIMENT 01
Overview of Functions and Multidimensional Arrays.
Objectives:
 To revise the concepts of Functions.
 To revise the concepts of Arrays.
Equipment /Tool:
PC, Microsoft Visual Studio 2013
Background:
A programming paradigm that provides code encapsulation, code security and better code
maintainability. Object oriented (OO) design access each entity through objects. Objects are the
instantaneous that represent their classes/ entities. Each class contains its member variables and
member functions which holds the data (or can generate the data) of the class entity. The data
about the class entity can be accessed or can be made inaccessible to the external environment
through set of access modifiers. Not all languages are object oriented and some of the language
which supports OO paradigm are: C++, C#, Java, Objective-C, PHP etc.
Arrays:
An array is a collection of variables of similar type, placed contiguously in memory. Arrays are
used to avoid the inconvenience of giving each data element a unique variable name. One data
structure having more than one data members of the same type. Array declaration tells the compiler
about
 The type of array.
 The name of array.
 The size of array.
type arrayName [ arraySize ];
Example:

3
An array can have two or more dimensions. This permits them to model multidimensional objects,
such as graph paper or the computer display screen. They are often used to represent tables of
values consisting of information arranged in rows and columns. To identify a particular element,
we must specify two subscripts. By convention,
array_name[row_number][col_number]
For example, arr[i][j]. A sample code to display a 2D array is shown below

Functions:
Any portion of the code that carries out a specific task or is to be repeated several times can be
converted into a function. For example, the summing operation takes 2 inputs and returns their
sum as output. sum() function can be called every time to calculate the sum. There are three main
components of a function
• Function Prototype (Declaration)
• Tells the compiler that a function of this kind is defined somewhere in the program
• Function Call
• Takes the program control to the called function
• Function Header and Body
• Contains the body of the function i.e. all the commands that make up the function
A sample code is shown below

4
Procedure:
Perform all the tasks and provide your code and result in the space below the questions.
Lab Tasks

Q1) Create a 2D array ‘a’ and add its diagonal values

Also compute its total number of elements


Answer:
Solution

#include <iostream>
using namespace std;

int main()
{
int A[3][3] ,x=0 ,z=0;

for (int a=0 ; a<3 ;++a)


{
for(int b=0 ; b<3 ; ++b )
{
cin>>A[a][b];
z++;
if (a==b)

5
x=x+A[a][b];
}
cout<<endl;
}
cout<<endl;
cout <<x<<endl;
cout<<z;
return 0; }

Q2) Create an array ‘a’ in main containing following values, then pass it in the function ‘add’ and
sum the array

There should be two input parameters to the function ‘add’ i.e. the array itself and its size

Answer:
Solution
#include <iostream>
using namespace std;
float sum(int[], int s);
int main()
{
const int s=5;
int A[s]={10,27,21,5};

sum(A,s);
return 0;}
float sum (int A[], int s)
{
int x=0;
for(int i=0;i<s ;++i)
{
x=x+A[i];
}
cout<<x;
return 0; }

6
Q3) Write two functions with same name Calculator one that takes two integers while other takes
two float values. First function displays addition, subtraction and modulus, while the 2nd one
displays addition subtraction and division.

Answer:
Solution

#include <iostream>
using namespace std;
int calculator (int, int );
float calculator (float , float);

int main()
{
int a, b;
cin>>a;
cout<<endl;
cin>>b;

calculator(a,b);
calculator((float)a,(float)b);
return 0;
}

int calculator (int a, int b )


{
cout<<endl <<endl;
cout<<"Sum = "<<a+b<<endl;
cout<<"Subtraction ="<<a-b<<endl;
cout<<"Modulus ="<<a%b<<endl;
return 0;
}

float calculator (float a, float b )


{
cout<<endl;
cout<<"Sum "<<a+b<<endl;
cout<<"Subtraction"<<a-b<<endl;
cout<<"Division ="<<a/b;
return 0;
}

7
---------------------------------------------

Q4) Write three functions of name ‘perimeter’ but with different number of input arguments as
given below. Ask user whether he/ she wants to compute perimeter of equilateral, scalene or
isosceles triangles and the call the appropriate function.
float area(float a)
float area(float a, float b)
float area(float a, float b, float c)

Answer:
Solution
#include <iostream>
using namespace std;
float perimeter (float );
float perimeter (float , float);
float perimeter (float , float ,float);
int main()
{
perimeter (1.2,6.5,2.3);
return 0; }

float perimeter (float a )


{
cout<<"perimeter of equilateral triangle iz ="<<3*a<<endl<<endl;
return 0; }

float perimeter (float a , float b ,float c)

{
cout<<"perimeter of scalene triangle iz ="<<a+b+c<<endl<<endl;
return 0; }
float perimeter (float a , float b)
{
cout<<"perimeter of iososceles triangle is ="<<2*a+b<<endl<<endl;
return 0; }

8
Q5) Write a function that calculates factorial of passed argument (use recursive function)

Answer:
Solution

#include <iostream>
using namespace std;
int fact(int , int );
int main ( )
{
int a ,b;
cout<<"enter the +ve integer";
cin>>a;
cin>>b;

fact(a,b);
return 0; }

int fact(int a, int b )


{
if (a>1 )
{
if(b>1)
{
cout<<a * fact(a,b-1);
}}
cout<<a;
return 0; }

Additional Tasks:
……………………………………………………………
……………………………………………………………

9
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
…………………………………………………………….
Lab Assessment Rubric
Lab Title: ______________________________________________________

Needs
Category Excellent (8) Good (6) Satisfactory (4) Total (40)
improvement (2)
Clearly describes Adequately Describes the Cannot describe
Objectives and
the objectives of describes the objectives but the objectives of
Results
lab. Understands objectives, but misses some the lab, or what

10
possible sources cannot discuss details. Cannot was learnt,
of errors and possible discuss possible sources of errors
their effects. sources of sources of error and their effects
Suggests ways to errors and their or their effects
minimize them. effects
Circuit
performs but Circuit does not
Circuit
Circuit works output not give any output.
performs most
Circuit perfectly. All exactly as Most wires are
the functions,
implementation/ wires are expected. Some not connected/
gives output./
software coding attached. / Code connections not code not
Code gives
works perfectly done/ code compiled/ many
some errors
gives some errors
correct output
Adequately
Can clearly Describe the
identify the
identify the problem but
problems and Cannot describe
problems and cannot suggest
steps taken to the problem, and
take steps to fix steps on how to
Trouble Shooting fix them. Uses has no effective
them. Uses an solve them.
an effective strategy on how
effective strategy Trouble
strategy but to solve them.
to solve shooting is not
misses some
problems consistent.
details
Detailed results Adequate Most results are
Some missing
are shown for results are missing. Only
Measurements observations.
each step. 100% shown. 80% 30%
and Observations 70% results are
measurements measurements measurements are
correct
are correct are correct correct
Limited
Good Shows incorrect
Thorough understanding
understanding understanding of
understanding of of the concepts.
of the the concept.
the concepts Cannot apply to
concepts, Cannot find any
Conclusions underlying the real life
however usage of the
lab. Can apply it situations and
cannot apply knowledge.
in real life suggest any use
them in real Cannot describe
situations of the
life situations what was learnt.
knowledge

Lab No: _______ Total Marks: __________ Signature: _______________

11

Das könnte Ihnen auch gefallen