Anda di halaman 1dari 8

Assignment no.

4
“DATA STRUCTURE AND ALGORITHMS”

Submitted To:
Sir haris masood

Submitted By:
Arslan Hussain
(UW-14-BSc-EE-13)

Date of Submission:
12-04-2016

DEPARTMENT OF ELECTRICAL ENGINEERING


WAH ENGINEERING COLLEGE WAH CANTT
Data Structures and Algorithms EE-223

Lab # 01
Data Structures and Algorithms

Objective:
 Introduction and Overview of conditional statements: if, else
and else if.
 Introduction and Overview of Iteration Statements: ForLoop,
Nested For loop,while loop, do- while loop.
 Functions:Built-In Functions, User-Defined Functions, Function
Declaration, Function Definition,Calling a Function, Passing
arguments to Functions.

Theory:
Loops are basically means to do a task multiple times, without actually
coding all statements over and over again.

For example, loops can be used for displaying a string many times, for counting
numbers and of course for displaying menus.

Loops in C++ are mainly of three types :-

1. 'while' loop
2. 'do while' loop
3. 'for' loop

For those who have studied C or JAVA, this isn't very new, as the syntax for
the loops are exactly same. So if you know the above languages, don't waste
your time understanding loop syntax in C++.

Whileloop :-
Let me show you a small example of a program which writes ABC three(3) times.
#include<iostream>

int main()
{
inti=0;
while(i<3)
{
i++;
cout<<"ABC"<<endl;
}
}

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

The output of the above code will be :-


ABC
ABC
ABC

A point to notice here is that, for making it more easy to understand, we could also write,

inti=1;
while(i<=3)

This would make our code more easy for a newbie, but in actuality it doesn't
make a difference either way.

Do while loop :-
It is very similar to the 'while' loop shown above. The only difference being, in a
'while' loop, the condition is checked beforehand, but in a 'do while' loop, the
condition is checked after one execution of the loop.

Example code for the same problem using a 'do while' loop would be :-
#include
<iostream>intmain()
{
inti=0;
do
{
i++;
cout<<"ABC"<<endl;
}while(i<3);
}

The output would once again be same as in the above example.

Forloop :-
This is probably the most useful and the most used loop in C/C++. The syntax is
slightly more complicated than that of 'while' or the 'do while' loop.

The general syntax can be defined as :-


for(<initial value>;<condition>;<increment>)

To further explain the above code, we will take an example. Suppose we had to
print the numbers 1 to 5, using a 'for' loop. The code for this
would be :-
#include<iostream>

int main()
{
for(inti=1;i<=5;i++)

cout<<i<<endl;
}
Department of Electrical Engineering, WEC
Data Structures and Algorithms EE-223

The output for this code would be :-


1
2
3
4
5
Here variable 'i' is given an initial value of 1. The condition applied is till 'i' is less than or
equal to 5. And for each iteration, the value of 'i' is also incremented by 1.

Notice here that if we wanted to print,


5
4
3
2
1

we would change our 'for' loop to :-


for(inti=5;i>=1;i--)

Nested loops:A loop can be nested inside of another loop. C++ allows at
least 256 levels of nesting.

Exercise 1:

Write program of Nested loops to get mentioned output:


1
22
333
4444
55555

Code:
#include<iostream>
using namespace std;
int main()
{
int a=1;
for(int i=0;i<5;i++)
{
for(int j=0;j<a;j++)
{
cout<<a;

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

} a++;

cout<<"\t"<<endl;
}
}
Output:

Functions:
Functions allow to structure programs in segments of code to perform individual
tasks.In C++, a function is a group of statements that is given a name, and which can
be called from some point of the program. The most common syntax to define a function
is:

type name ( parameter1, parameter2, ...) { statements }

Where:
- type is the type of the value returned by the function.
- name is the identifier by which the function can be called.
- parameters (as many as needed): Each parameter consists of a type followed by an
identifier, with each parameter being separated from the next by a comma. Each
parameter looks very much like a regular variable declaration (for example: int x), and in
fact acts within the function as a regular variable which is local to the function. The
purpose of parameters is to allow passing arguments to the function from the location
where it is called from. Statements are the function's body. It is a block of statements
surrounded by braces { } that specify what the function actually does.

Example 1:
Write a program to find the sum of 2 values using functions.
#include<iostream.h>
#include<conio.h> void
main (void) {inta,b;
void sum(int,int);

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

clrscr();
cout<<"enter first value"<<endl;
cin>>a;
cout<< "enter second value"<<endl;
cin>> b;
sum(a,b);
cout<<"ok";
}
void sum(int x, int y)
{
int s;
s=x+y;
cout<<"sum="<<s<<endl;
}

Example 2:
// function example
#include <iostream>
using namespace std;

int subtraction (int a, int b)


{
int r;
r=a-b;
return r;
}

int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout<< "The first result is " << z << '\n';
cout<< "The second result is " << subtraction (7,2) << '\n';
cout<< "The third result is " << subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout<< "The fourth result is " << z << '\n';
system("pause");
}

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

Exercise 2:
Write a program for simple calculator by passing three arguments to the function.
Code:

#include<iostream>
using namespace std;
int add(int x,int y,int z)
{
int r= x+y+z;
return r;
}
int subtract(int x,int y,int z)
{
int q=x-y-z;
return q;
}
int multiply(int x,int y,int z)
{
int i=x*y*z;
return i;
}
int main()
{ int a,b,c ;
char op;
cout<<"enter the values"<<endl;
cin>>a>>b>>c;
cout<<"enter operator"<<endl;
cin>>op;
switch(op)
{
case '+':
int k;
k= add(a,b,c);
cout<<" addition of numbers are "<<k<<endl;
break;
case '-':
int l;

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

l= subtract(a,b,c);
cout<<" subtraction of numbers are "<<l<<endl;
break;
case'*':
int w;
w=multiply(a,b,c);
cout<<" Multipliation of numbers are "<<w<<endl;
default:
cout<<"invalid operator"<<endl;
}

}
Output:

Conclusion:
In this lab we conclude that we can make program in different manners so
we should opt a way that is simple and easy to understand. A good
program is that which requires less execution time.
……………………………………………………………………………………..

Department of Electrical Engineering, WEC

Anda mungkin juga menyukai