Sei sulla pagina 1di 22

Lab 9

Pre lab:-
For loop:- For loop is repetition control structure that allow you to efficiently write a loop that
needs to execute specific number of times.

Increment operator: - Increment operators are used to increase the value of the variable by
one.

Decrement operator:- It is use to decrease the value of the variable by one.

In lab:-
Function:- A function is a group of statements that together perform a task.
Types of function:-
We have two ways to call them,
 Built in Functions
 User Defined functions

Built in functions:- The Functions those are provided by C++ Language are refers to the
Built in Functions For example. cin and cout, getch , Clrscr are the Examples of Built in
Functions.
User defined functions:- the Functions those are developed by the user for their Programs
are known as User Defined Programs.

Parameter passing to functions


The parameters passed to function are called actual parameters. For example, in the above
program 10 and 20 are actual parameters.
The parameters received by function are called formal parameters. For example, in the above
program x and y are formal parameters.
There are two most popular ways to pass parameters.

Pass by Value: In this parameter passing method, values of actual parameters are copied to
function’s formal parameters and the two types of parameters are stored in different memory
locations. So any changes made inside functions are not reflected in actual parameters of caller.

Pass by Reference: Both actual and formal parameters refer to same locations, so any
changes made inside the function are actually reflected in actual parameters of caller.
Structure of a Function
 The declaration syntax of a function is as follows:
 return-value-type function-name( argument-list )
 {declarations and statements}
 The first line is the function header and the declaration and statement part is the body
 of the function.

Example:
Below is a simple C program to demonstrate functions in C.

Output:-
In C, parameters are always passed by value. Parameters are always passed by value in C. For
example. in the below code, value of y is not modified using the function fun().
#include <stdio.h>
void fun(int x)
{
x = 30;
}

int main(void)
{
int x = 20;
fun(x);
printf("x = %d", x);
return 0;
}

Output:

x = 20

# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}

int main()
{
int x = 20;
fun(&x);
printf("x = %d", x);

return 0;
}

Output:

x = 30
Practivce 1: Make table using Function
#include<iostream>
using namespace std;
void table()
{
int c;
cout<<"Enter an entiger:";
cin>> c;
for(int i=1;i<=10;i++){

cout<<c<<"X"<<i<<"="<<c*i<<endl;
}

}
int main(){
table();
system("pause");
return 0;}

Out Put:
Practice 2:
#include<iostream>
using namespace std;
void printline()
{
for(int j=1;j<50;j++)
{cout<<"*";}
cout<<endl;}
int main()
{
printline();
cout<<"datatype size in bits"<<endl;
printline();
cout<<" int 4 "<<endl
<<" double 8 "<<endl
<<" char 1 "<<endl;
printline();
getchar();
getchar();
return 0;
}

Out Put:
Practice 3:
//This program calculates the square of a given number
#include <iostream>
using namespace std;
// Function declarations.
int square(int);

main()
{
int number, result;
result = 0;
number = 0;
cout << " Please enter the number to calculate the square ";
cin >> number;
// Calling the function square(int number)
result = square(number);
cout << " The square of " << number << " is " << result;
}
// function to calculate the square of a number
int square ( int number)
{
return (number * number ) ;
}

Out Put:
Practice 4:
#include<iostream>
#include<math.h>
using namespace std;
main()
{
int a;
float b,c,d,e,f,g,h,i,j,k,l;
cout<<"Result of all build-in-function"<<endl;
cout<<endl;

cout<<"Enter the negative integer number for abs()"<<endl;


cin>>a;
cout<<"Result of absolute funton\t"<<abs(a)<<endl;

cout<<"enter the second value for fabs()"<<endl;


cin>>b;
cout<<"Result of factional absolute funton\t"<<fabs(b)<<endl;

cout<<"enter the 3rd value for ceil()"<<endl;


cin>>c;
cout<<"Result of ceil function\t"<<ceil(c)<<endl;

cout<<"enter the 4th value for floor()"<<endl;


cin>>d;
cout<<"Result of floor function\t"<<floor(d)<<endl;

cout<<"enter the 5th value for sine()"<<endl;


cin>>e;
cout<<"Result of sine function\t"<<sin(e)<<endl;

cout<<"enter the 6th value for cose()"<<endl;


cin>>f;
cout<<"Result of cose function\t"<<cos(f)<<endl;

cout<<"enter the 7th value for tan()"<<endl;


cin>>g;
cout<<"Result of tan function\t"<<tan(g)<<endl;

cout<<"enter the 8th value for log()"<<endl;


cin>>h;
cout<<"Result of log function\t"<<log(h)<<endl;

cout<<"enter the 9th value for log10()"<<endl;


cin>>i;
cout<<"Result of log10 function\t"<<log(i)<<endl;
cout<<"enter the 10th value for squreroot()"<<endl;
cin>>j;
cout<<"Result of squire root function\t"<<sqrt(j)<<endl;
cout<<"enter the 11th value for power()"<<endl;
cin>>k;
cin>>l;
cout<<"Result of power function\t"<<pow(k,l)<<endl;
getchar();
getchar();
}
Out Put:

Practice 5:
# include <iostream>
using namespace std;
main ( )
{
//declare & initialize variables
int tryNum = 0 ;
char c ;

// do-while construct
do
{
cout << "Please enter a character between a-z for guessing : ";
cin >> c ;
//check the entered character for equality
if ( c == 'z')
{
cout << "Congratulations, Your guess is correct" ;
tryNum = 6;
}
else
{
tryNum = tryNum + 1;
}
}
while ( tryNum <= 5);
}
Out Put:

Practice 6:
#include<iostream>
using namespace std;
void factorial(int a);
int main(void)
{
int num;
cout<<"Enter A Number= "<<endl;
cin>>num;
factorial(num);
return 0;
}
void factorial(int a)
{
int i;
long double fact;
fact=1;
for(i=1;i<=a;i++)
fact=fact*i;
cout<<"Factorial of "<<a<<" is "<<fact<<endl;
}

Out Put:

Practice 6:
#include<iostream>
using namespace std;
int Fibonacci(int nNumber)
{

if (nNumber == 0)
return 0;
if (nNumber == 1)
return 1;
return Fibonacci(nNumber-1) + Fibonacci(nNumber-2);

// And a main program to display the first 100 Fibonacci numbers


int main(int)
{
using namespace std;
for (int i=0; i< 10; i++)
cout << Fibonacci(i) << " ";
system("pause");}

Out Put:

Practice 7:
#include<iostream>
using namespace std;
void display()
{cout<<"Always prayer for others!"<<endl;}
void display(int n)
{cout<<"The value of n"<<endl<<n<<endl;}
void display(double c)
{cout<<"The value of c"<<endl<<c<<endl;}
void display(char s,int m)
{cout<<"The value of s and m\t"<<s<<m<<endl;}
int main()
{
display();
display(50);
display(20.5);
display('*',10);
getchar();
getchar();
return 0;
}

Out Put:

Practice 8:
#include<iostream.h)
#include<conio.h>
#include<stdio.h>
int max(int a,int b);
int main()
{
int x,y;

cout<<"enter two numbers";


cin>>x>>y;
max(x,y);
getch():
}
int max(int a,int b)
{
if(a>b);
cout<<"max num is"<<a;
else
cout<<"max num is"<<b;
}

Practice 9:
#include<iostream>
using namespace std;
void bank(int x,int &y,char z)
{
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
}
int main()
{int a,b;
char c;
cout<<"Enter two numbers:"<<endl;
cout<<"\nENTER 1st number:"<<endl;
cin>>a;
cout<<"ENTER 2nd number:"<<endl;
cin>>b;
cout<<endl;
bank(a,b,'g');
getchar();
getchar();
}

Output:-

Practice 10:
#include<iostream.h>
#include<conio.h>
int swap(int a,int b)
{
int c=a;
a=b;
b=c;
return 0;
}
int main()
{
int a=15, b=20;
swap(a,b);
cout<<a<<endl;
cout<<b<<endl;
getch();
}
Out Put:
LAB 10
PRE LAB:
 FUCTIONS
IN LAB:
ARRAYS:
They are special kind of data type
They are like data structures in which
identical data types are stored
In C++ each array has
– name
– data type
– size
They occupy continuous area of
Memory
Storage of an array in memory:

Declaration of Arrays:
array Type array Name[number of Elements ];
For example ,
int age [ 10 ] ;
More than one array can be declared on a line
int age [10] , height [10] , names [20] ;
Mix declaration of variables with declaration of arrays
int i , j , age [10] ;

Referring to Array Elements:


Array name e.g. age
index number
age [ 4 ]

Practice 1: Using Arrays


for ( i = 0 ; i < 10 ; i ++ )
{
cin >> age [ i ] ;
}

Practice 2:
totalage = 0 ;
for ( i = 0 ; i < 10 ; i ++ )
{
totalage + = age [ i ] ;
}

Initializing an Array:
int age [ 10 ] ;

for ( i = 0 ; i < 10 ; i ++ )
{
age [ i ] = 0 ;
}
Or
int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ;
int age[ 10 ] = { 0 } ;

Or
int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ; for ( i = 0 ; i < 10 ; i ++ )

Copying Arrays:
Data types should be identical
Size should be same
int a [ 10 ] ;
int b [ 10 ] ;
To copy from array “ a ” to array “ b ” :
b[0]=a[0];
b[1]=a[1];
b[2]=a[2];
b[3]=a[3];
………
………
b [ 10 ] = a [ 10 ] ;
for ( i =0 ; i < 10 ; i ++ )
b[i]=a[i];

Practice 3:
Take the sum of squares of 10 different numbers which are stored in an array
int a [ 10 ] ;
int arraySize =10 ;
int sumOfSquares = 0 ;
for ( i = 0 ; i < arraySize ; i ++ )
{
sumOfSquares = sumOfSquares + a [ i ] * a [ i ] ;}
Initializing an Array:
Initializing array of integers
int c [ 10 ] = { 1,2,3,4,5,6,7,8,9,10 } ;
int c [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;
For character arrays
char name [ 100 ] = { ‘a’,b’,’c’,’0’,’1’ } ;
char name [ 100 ] = “abc01“ ;
char name [ ] = “Hello World“ ;

Character Arrays:
To read name from keyboard and display it on screen
char name [ 100 ] ;
cout << “ Please enter you name” ;
cin >> name ;
cout << name ;

Comparing Two arrays:


Condition: arry size should be equal
int equal = 0 ;
int num1 [ 100 ] , num2 [ 100 ] ;
for ( i = 0 ; i < 100 ; i ++ ){
if ( num1 [ i ] != num2 [ i ] ){
equal = 1 ;
break ;}}

if ( equal ==1 )
cout << “ The arrays are not equal” ;
else
cout << “ The arrays are equal” ;
Pointer:
CONCEPT: Every variable is assigned a memory location whose address can be
retrieved using the address operator &. The address of a memory location
is called a pointe.
Every variable in an executing program is allocated a section of memory large enough to
hold a value of that variable’s type. Current C++ compilers that run on PCs usually allocate a
single byte to variables of type char, two bytes to variables of type short, four bytes
to variables of type float and long, and 8 bytes to variables of type double
Each byte of memory has a unique address. A variable’s address is the address of the
first byte allocated to that variable. Suppose that the following variables are defined in a
program:
char letter;
short number;
float amount;

Pointer Variable: A pointer variable is a variable that holds addresses of memory locations
Practice 1:Hack the location of x and display it
#include<iostream>
using namespace std;
int main()
{
int x=10;
int *p;
p=&x;
cout<<*p<<endl;
system("pause");
return 0;
}
Out Put:

Potrebbero piacerti anche