You are on page 1of 29

Character Array & Strings

Note: dont use built in Functions.


Character Array:
1. Write a program that accept a character array from the user and copy
those characters into another array in reverse order.
2. Given the following declaration and definition.
Char a[6] = {z,x,m,s,e,h};
Write a program to rotate all values in a to the right by one element.
Strings:
1. Write a program that accept a string array from the user and
displays its length.
2. Write a program that accept two strings as a parameter from the
user and concatenate those strings into a third string.
3. Write a program that accept a string array from the user and count
the total number of vowel and consonants in a string.
4. Write a program that accept a string from the user and search the
character u in the array, and replace it with the character p
Enter a string: documents and files should be submitted on
time
Enter character you want to replace: u
Enter character with which you want to replace: p
Output string: docpments and files shopld be spbmitted on
time
1. Write a function that prints
* * * * *
* *
**
*
*
*
* *
* *
* * * * *
2. Write a Function that that take size of the side of a square from the
user and pass it as a parameter; it then prints a hollow square of that

3.
4.
5.
6.

size out of asterisks and blanks. For example, if user enters 5 it should
print.
*****
*
*
*
*
*
*
*****
Write a program that by defining a function even_odd(), to test
whether a given integer is even or odd. Pass an integer value as an
argument to a function.
Write a function multiple() that determines for a pair of integers
whether the second is multiple of first or not.
Write a function smallest() that takes three arguments to the function
and prints the smallest of all three.
Write a function prime() that finds whether the given number is prime
number or not.

7. Write a function fact() to calculate the factorial value of any integer


entered through the keyboard.
8. Write a function reverse() that takes an integer value and returns the
number with its digits reversed. For example, given the number 7631,
the function should return 1367.
9. Write a function power() that displays first number raised to the
power of second number. i.e. xy
10.
Write a function Fibonacci() that displays first 10 Fibonacci
numbers.
i.e. Fibonacci series is
1, 1, 2, 3, 5, 8, 13, 21, 34, 55

LAB 04 (Character Array & Strings)


Note: dont use built in Functions.
Character Array:
3. Write a program that accept a character array from the user and copy
those characters into another array in reverse order.
4. Given the following declaration and definition.
Char a[6] = {z,x,m,s,e,h};
Write a program to rotate all values in a to the right by one element.
Strings:

5. Write a program that accept a string array from the user and
displays its length.
6. Write a program that accept two strings as a parameter from the
user and concatenate those strings into a third string.
7. Write a program that accept a string array from the user and count
the total number of vowel and consonants in a string.
8. Write a program that accept a string from the user and search the
character u in the array, and replace it with the character p
Enter a string: documents and files should be submitted on
time
Enter character you want to replace: u
Enter character with which you want to replace: p
Output string: docpments and files shopld be spbmitted on
time
9. Write a program that takes string as an input from the user and
then count:
a. The total number of characters in the string.
b. The total number of words in the string.
c. Total spaces in the array string.
d. Displays total number of punctuation marks if any in the string.
10. Write a program that accept a string from the user and then
convert all uppercase character to lower case characters and lower
case character to upper case.
11. Write a function that takes the string from the user and check
whether the given string is Palindrome or not. (Using built-infunction and user defined function).
12. Write a program that will read a string and delete from it all
occurrences of the word is.

Lab 05 (void Functions)


11.

Write a function that prints


* * * * *
* *
**
*
*
*
* *
* *

* * * * *
12.
Write a Function that that take size of the side of a square from
the user and pass it as a parameter; it then prints a hollow square of
that size out of asterisks and blanks. For example, if user enters 5 it
should print.
*****
*
*
*
*
*
*
*****
13.
Write a function comparison() that takes two values as
parameter. It prints sum of x and y if x is greater than y, otherwise it
prints x minus two times y.
14.
Write a function Dconversion() that converts the binary integer
and print its decimal equivalent.
Hint: The decimal equivalent of binary 1101 is 1*1+0*2+1*4+1*8 or
1+0+4+8=13
15.
Write a function printable() that takes a positive integer less
than 50 and prints its multiplication table.
16.
Write a program that by defining a function even_odd(), to test
whether a given integer is even or odd. Pass an integer value as an
argument to a function.
17.
Write a function multiple() that determines for a pair of integers
whether the second is multiple of first or not.
18.
Write a function smallest() that takes three arguments to the
function and prints the smallest of all three.
19.
Write a function prime() that finds whether the given number is
prime number or not.
20.
Write a function fact() to calculate the factorial value of any
integer entered through the keyboard.
21.
Write a function reverse() that takes an integer value and
returns the number with its digits reversed. For example, given the
number 7631, the function should return 1367.
22.
Write a function power() that displays first number raised to the
power of second number. i.e. xy
23.
Write a function Fibonacci() that displays first 10 Fibonacci
numbers.
i.e. Fibonacci series is
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
24.
Write a function Factor() that takes a number from user and
displays its factors.
25.
.Write a function perfect() that takes a number from user and

display whether it is perfect number or not. A perfect number is one


whose factors add up to the number itself, e.g. 6 = 1+2+3.)

Lab 06 (Value Returning Function)


1. Write a function Sum_of_Number(..) that receive a number from the
user as a parameter to the function, and returns the sum of that
number

2. Write a function print(..) that receives a three digit number as a


parameter to the function, and print number in words. e.g. if user enter
972 it print Nine seven two.
..
3. Write the functions fAdd(), fSub(), fMul( ) and fDiv( ) to
perform the four basic arithmetic operations for the given parameters.
Write another function userInterface() that will ask the user for two
numbers and also for the operation. Depending upon the users
operation selection this function must select one of the four functions
and show the result. The userInterface() function must quit if the user
selects the quit option.
4. Write a function perfect(..) that receive a number as a parameter to
the function and display whether it is perfect number or not. A perfect
number is one whose factors add up to the number itself, e.g. 6 =
1+2+3.)
5. Write a function Days(..) that receive number of days as parameter to
the function and convert given number of days to a measure of time
given in years, weeks and days. For example 375 days is equal to 1
year 1 week and 3 days (ignore leap year)
6. Write
the
functiosn
areaofcircle(..),
areaofsquare(),
areaofrectangl() to find the areas of different geometrical figures
such as circle, square, rectangle. Write another function user
userInterface() that will ask the user numbers and also for the
operation. Depending upon the users operation selection this function

must select one of the three functions and show the result. The
userInterface() function must quit if the user selects the quit option.
Note: Area of circle is: = r2
Area of Square is: = a2
Area of Rectangle is = w h

Lab 07 (Pass by Value and Reference Parameter)


1. Run the following programs and check their execution and output:
a)
Function Overloading:
int cube(int a)
{
return a*a*a;
}
double cube(double a)
{
return a*a*a;
}
void main ()
{
cout<<"cube of integer value 5 is :"<<cube(5)<<endl;
b) Default Arguments
cout<<"\n\ncube of float value 10.24 is :"<<cube(5.4)<<endl;
}void show_parameters(int a=1, int b=2, int c=3)
{
cout << "a " << a << " b " << b << " c " << c << endl;
}
void main(void)
{
show_parameters();
show_parameters(1001);
show_parameters(1001, 2002);
2002,
3003);of a variable:
c)show_parameters(1001,
Local and Global
scope
}

int count1 = 100;


main()
{
int count1 = 10;
int count3 = 50;
cout << "Value of outer count1 = " << count1 << endl;
cout << "Value of global count1 = " << ::count1 << en
dl;
{
int count1 = 20;
int count2 = 30;
cout << "Value of inner count1 = " << count1 << en
dl;
cout << "Value of global count1 = " << ::count1 <<
endl;
count1 += 3;
count3 += count2;
}
cout << "Value of outer count1 = " << count1 << endl
<< "Value of outer count3 = " << count3 << endl;
}
d)
Simple/Automatic Variable
Static
variable
void simpl()
{
int a=0;
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
simpl ();
}

void stat()
{
static int a=0;
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
stat();
}

by value and reference:


void main()
{
clrscr();
int x=5;
cout<<"x\t"<<x<<endl;
cout<<"&x\t"<<&x<<endl;
}

e) Pa
ss

f)
void numbers(int x, int &y)
{
int b;
x+=6;
y+=11;
b=55;
cout<<b<<" "<<x<<" "<<y<<endl;
}
void main()
{
int a,b,c;
a=22;
b=90;
c=14;
numbers(a,a);
numbers(a,b);
numbers(b,a);
cout<<a<<" "<<b<<" "<<c<<endl;
}

2. Write a function called zeroSmaller() that is passed two int


arguments by reference and then sets the smaller of the two
numbers to 0. Write a main() program to exercise this function.
3. Write a function swap(..) that accept two parameter as an argument
to the function, and exchange content of these two variables.
4. Write a function date_validity(..) that accept three parameter (date,
month and year) as an argument to the function, and
check whether a date entered by user is valid or not.
5. Write a function called hms_to_secs() that takes three int values
for hours, minutes, and secondsas arguments, and returns the
equivalent time in seconds (type long). Create a program that
exercises this function by repeatedly obtaining a time value in

hours, minutes, and seconds from the user (format 12:59:59),


calling the function, and displaying the value of seconds it returns.

Lab 08 (functions)
1. What will be the output of the following programs, debug the program and
then write the answer:
a) Static variable:
Simple variable
Static Variable
void simpl()
{
int a=0;

void stat()
{
static int a=0;

cout<<a<<endl;
a++;
}

cout<<a<<endl;
a++;
}

main()
{
for(int i=0;i<5;i++)
simpl ();
}
Output:

main()
{
for(int i=0;i<5;i++)
stat();
}
Output:

b) Function Overloading:
Output:
int cube(int a)
{
return a*a*a;
}
double cube(double a)
{
return a*a*a;
}
void main ()
{
cout<<"cube
of
integer
is :"<<cube(5)<<endl;
cout<<"\n\ncube
of
float
is :"<<cube(5.4)<<endl;
}

value
value

c) Passing array to function:


Whole array

5
10.24

Single Index

void show_array(int array[], int


number_of_elements)
{
int i;
for (i = 0; i < number_of_elements; i+
+)
cout << array[i] << ' ';
cout << endl;
}
void main()
{
int little_numbers[5] = { 1, 2, 3, 4,
5 };
int big_numbers[3] = { 1000, 2000,
3000 };
show_array(little_numbers, 5);
show_array(big_numbers, 3);
}
Output:

2.

void show_array(int &a)


{
a=100;
}
void main()
{
int number[5] = { 1, 2, 3, 4,
5 };
show_array(number[2]);
for(int i=0;i<5;i++)
cout<<number[i]<<endl;
}

Output:

Write a function average() that take array and its size as an


argument and then calculate and return the average of an array.
3. Write a function that takes two arrays and their and their size as
parameters to the function. The function must check whether the given
value in the index is equal or not. e.g. A[0] is equal to B[0], A[1] is
equal to B[1], and so on.
4. Twenty-five numbers are entered from the keyboard into an array. The
number to be searched is entered through the keyboard by the user.
Write a program to find if the number to be searched is present in the
array or not, and if it is present then display that it is present also
display the number of times it appears in the array.
5.
Write a function that copies one-dimension array into a twodimensional array of k rows and j columns. The rows must be valid
factor of the number of elements in the one-dimensional array; that is
k * j = n.

Lab 09 (Recursive functions)


1. Given the TOH.cpp file. Compile the program and check the ans.
2. Write a recursive function that calculates the sum of the n number.
3. Write recursive GCD function, that finds greatest common divisor of
two integers. Mathematical Rule is given below.
gcd (x,y) = x

if y=0

gcd(y, x%y)
if y0
4. Write a program to find the exponential power of an integer using
recursive function.
5. Write a recursive program that performs multiplication of two digits,
using addition operator.
6. Following program takes a decimal number from user and prints its
Binary using recursive function. You are required to write this program
in IDE, compile and then execute it. You have to make a word
document in which you will show execution of the following program.
#include<iostream.h>
#include<conio.h>
/*------ Function for Decimal to Binary Conversion -----*/
void decToBin(int num, int base)
{
if(num > 0)
{
decToBin(num / base, base);
cout << num % base;
}
}
void printStars()
{
cout<<"\n\n***************************\n";
cout<<"***************************\n\n";

}
void main()
{
clrscr();
int decimalNum;
int base;
base = 2;
cout<<"\n\n Enter number in Decimal\t";
cin>>decimalNum;
cout<<endl;
printStars();
cout<<"\n\n Decimal " <<" = " << " Binary \n\n" ;
cout<<" " << decimalNum <<"\t
";
decToBin ( decimalNum, base);
printStars();
getch();
}
7. Write recursive GCD function, that finds greatest common divisor of
two integers. Mathematical Rule is given below.
gcd (x,y) = x
gcd(y, x%y)

8.

if y=0
if y0

Write a function that check whether the given year is a leap year or not.
void leap (void)
{
int y;
cout<<" \n Enter The year : ";
cin>> y;
if (y%4==0 || y%100>0 && y%400==0 )
cout<<y<<" is a leap year ";
}
int main ()
{
leap ();

getch ();
}

LAB 10 (Pointers)
1.
a)
int num, *numPtr;
num = 987;
numPtr = &num;
cout<<"The value in num is ,"<< num<<endl;
cout<<"The address of num is, "<<&num<<endl;
cout<<"The value in numPtr is ,"<< numPtr<<endl;
cout<<"The value in the address pointed by numPtr is ,"<<
*numPtr<<endl;
cout<<"The address of numPtr is ,"<< &numPtr<<endl;
cout<<"The value in the address of num is ,
"<<*&num<<endl;

Output:

b) Given the following declaration.


int a=5;
int b=7;

int *p = &a;
int *q = &b;
What is the value of each of the following expression?
i. ++a
ii. ++(*p)
iii. &a
v. *p
vi.
--(*q)
vii. --b;
viii. &b
ix. q++

iv. p++
x. *q

Output:

2. Write a function that receives a floating-point number as reference


parameter and sends back the integer and fraction parts.
3. Write a function names addem with two call-by-reference integer
parameters. The function is to add 2 to the first parameter and 5 to the
second parameter. Test the function by calling it with the values of 17
and 25.
4. Write a program that inputs two integers and passes them to a
function using pointers. The function exchanges the values and the
program finally displays the value.
5. Write a function that takes an instance of a date in terms of three digits
(day, month, year) as parameters returns 1 if the date is a valid and
returns 0 otherwise. For example 29-02-2010, 31-4-1992 and 32-1-200
are invalid dates. Apply all the checks.

LAB 11 (Pointers Arrays)


1.

Output:

int main()
{
int num[5] = {3,4,6,2,1};
int *p = num;
int *q = num+2;
int *r = &num[1];
cout<<num[2]<<setw(3)<<*(num +
2)<<endl;
cout<<*p<<setw(3)<<*(p +1)<<endl;
cout<<*q<<setw(3)<<*(q+1)<<endl;
cout<<*r<<setw(3)<<*(r+1)<<endl;
a)
} Given the following declaration.

char a[20] = {z, x, m, s, e, h};


char *pa = a;
int i = 2;
int j = 4;
int *pi = &i;
What is the value of each of the following expression?
i. int main()
*(pa + j)
ii. *(pa + *pi)
Output:
{
int a[5]={1,2,3,4,5};
int *p;
cout<<"Array itself a Pointer\n";
for(int i=0;i<5;i++)
{
cout<<*a<<"\t";
*a+=1;
}
cout<<endl;
p=&a[2];
cout<<"Array Pointer "<<a[0]<<"\tPointer
2]<<endl;
cout<<"Array Pointer "<<a[1]<<"\tPointer
1]<<endl;
cout<<"Array Pointer "<<a[2]<<"\tPointer
"<<p[0]<<endl;
cout<<"Array Pointer "<<a[3]<<"\tPointer

value "<<p[value "<<p[value


value

2. Write a function that reverses the elements of an array in place. In


other words, the last element must become the first; the second from
the last become the second, and so on. The function must accept only
ne pointer value and return void.
3. We have two arrays, A and B, each containing ten integers. Write
function that checks if every element of array A is equal to its
corresponding element in array B. in other words, the function must
check if A[0] is equal to B[0], A[1] is equal to B[1], and so on. The
function must accept only two pointer values and return an integer
zero for equal and not zero for unequal.
4. Write a function to find the smallest number in an array using pointers.
5. Given the following declaration and definition.
Char a[6] = {z,x,m,s,e,h};
And using only pointer notation, write loop to rotate all values in a to
the right by one element.
7. Write a function Sum_of_Number(..) that receive a number from the
user as a parameter to the function, and returns the sum of that
number

8. Write a function print(..) that receives a three digit number as a


parameter to the function, and print number in words. e.g. if user enter
972 it print Nine seven two.
..

9. Write the functions fAdd(), fSub(), fMul( ) and fDiv( ) to


perform the four basic arithmetic operations for the given parameters.
Write another function userInterface() that will ask the user for two
numbers and also for the operation. Depending upon the users
operation selection this function must select one of the four functions
and show the result. The userInterface() function must quit if the user
selects the quit option.
10.
Write a function perfect(..) that receive a number as a
parameter to the function and display whether it is perfect number or
not. A perfect number is one whose factors add up to the number
itself, e.g. 6 = 1+2+3.)
11.
Write a function Days(..) that receive number of days as
parameter to the function and convert given number of days to a
measure of time given in years, weeks and days. For example 375 days
is equal to 1 year 1 week and 3 days (ignore leap year)
12.
Write the functiosn areaofcircle(..), areaofsquare(),
areaofrectangl() to find the areas of different geometrical figures
such as circle, square, rectangle. Write another function user
userInterface() that will ask the user numbers and also for the
operation. Depending upon the users operation selection this function
must select one of the three functions and show the result. The
userInterface() function must quit if the user selects the quit option.
Note: Area of circle is: = r2
Area of Square is: = a2
Area of Rectangle is = w h

Lab 07 (Pass by Value and Reference Parameter)


6. int
Run
the following
programs and check their execution and output:
cube(int
a)
g)
Function
Overloading:
{
return a*a*a;
}
double cube(double a)
{
return a*a*a;
}
void main ()
{
cout<<"cube of integer value 5 is :"<<cube(5)<<endl;
cout<<"\n\ncube of float value 10.24 is :"<<cube(5.4)<<endl;
}

h) Default Arguments
void show_parameters(int a=1, int b=2, int c=3)
{
cout << "a " << a << " b " << b << " c " << c << endl;
}
void main(void)
{
show_parameters();
show_parameters(1001);
show_parameters(1001, 2002);
show_parameters(1001, 2002, 3003);
}

i) Local and Global scope of a variable:


int count1 = 100;
main()
{
int count1 = 10;
int count3 = 50;
cout << "Value of outer count1 = " << count1 << endl;
cout << "Value of global count1 = " << ::count1 << endl;
{
int count1 = 20;
int count2 = 30;
cout << "Value of inner count1 = " << count1 << endl;
cout << "Value of global count1 = " << ::count1 << endl;
count1 += 3;
count3 += count2;
}
cout << "Value of outer count1 = " << count1 << endl
<< "Value of outer count3 = " << count3 << endl;
}
j)
Simple/Automatic Variable
Static
variable
void simpl()
{
int a=0;
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
simpl ();
}

void stat()
{
static int a=0;
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
stat();
}

by value and reference:


void main()
{
clrscr();
int x=5;
cout<<"x\t"<<x<<endl;
cout<<"&x\t"<<&x<<endl;
}

k) Pa
ss

l)
void numbers(int x, int &y)
{
int b;
x+=6;
y+=11;
b=55;
cout<<b<<" "<<x<<" "<<y<<endl;
}
void main()
{
int a,b,c;
a=22;
b=90;
c=14;
numbers(a,a);
numbers(a,b);
numbers(b,a);
cout<<a<<" "<<b<<" "<<c<<endl;
}

7. Write a function called hms_to_secs() that takes three int values


for hours, minutes, and secondsas arguments, and returns the
equivalent time in seconds (type long). Create a program that
exercises this function by repeatedly obtaining a time value in
hours, minutes, and seconds from the user (format 12:59:59),
calling the function, and displaying the value of seconds it returns.

Lab 08 (functions)
6. What will be the output of the following programs, debug the program and
then write the answer:
d) Static variable:
Simple variable
Static Variable
void simpl()
{
int a=0;

void stat()
{
static int a=0;

cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
simpl ();
}
Output:

cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
stat();
}
Output:

e) Function Overloading:
Output:
int cube(int a)
{
return a*a*a;
}
double cube(double a)
{
return a*a*a;
}
void main ()
{

cout<<"cube
of
integer
is :"<<cube(5)<<endl;
cout<<"\n\ncube
of
float
is :"<<cube(5.4)<<endl;
}

value

value

10.24

f) Passing array to function:


Whole array
void show_array(int array[], int
number_of_elements)
{
int i;
for (i = 0; i < number_of_elements; i+
+)
cout << array[i] << ' ';
cout << endl;
}
void main()
{
int little_numbers[5] = { 1, 2, 3, 4,
5 };
int big_numbers[3] = { 1000, 2000,
3000 };
show_array(little_numbers, 5);
show_array(big_numbers, 3);
}
Output:

Single Index
void show_array(int &a)
{
a=100;
}
void main()
{
int number[5] = { 1, 2, 3, 4,
5 };
show_array(number[2]);
for(int i=0;i<5;i++)
cout<<number[i]<<endl;
}

Output:

7. Twenty-five numbers are entered from the keyboard into an array. The
number to be searched is entered through the keyboard by the user.
Write a program to find if the number to be searched is present in the
array or not, and if it is present then display that it is present also
display the number of times it appears in the array.

8.

Write a function that copies one-dimension array into a twodimensional array of k rows and j columns. The rows must be valid
factor of the number of elements in the one-dimensional array; that is
k * j = n.

Lab 09 (Recursive functions)


9. Given the TOH.cpp file. Compile the program and check the ans.
10.
Write a recursive function that calculates the sum of the n
number.
11. Write recursive GCD function, that finds greatest common divisor of
two integers. Mathematical Rule is given below.
gcd (x,y) = x

if y=0

gcd(y, x%y)
if y0
12. Write a program to find the exponential power of an integer using
recursive function.
13. Write a recursive program that performs multiplication of two digits,
using addition operator.
14. Following program takes a decimal number from user and prints its
Binary using recursive function. You are required to write this program
in IDE, compile and then execute it. You have to make a word
document in which you will show execution of the following program.
#include<iostream.h>
#include<conio.h>
/*------ Function for Decimal to Binary Conversion -----*/
void decToBin(int num, int base)
{
if(num > 0)

{
decToBin(num / base, base);
cout << num % base;
}
}
void printStars()
{
cout<<"\n\n***************************\n";
cout<<"***************************\n\n";
}
void main()
{
clrscr();
int decimalNum;
int base;
base = 2;
cout<<"\n\n Enter number in Decimal\t";
cin>>decimalNum;
cout<<endl;
printStars();
cout<<"\n\n Decimal " <<" = " << " Binary \n\n" ;
cout<<" " << decimalNum <<"\t
";
decToBin ( decimalNum, base);
printStars();
getch();
}
15. Write recursive GCD function, that finds greatest common divisor of
two integers. Mathematical Rule is given below.
gcd (x,y) = x
gcd(y, x%y)

if y=0
if y0

16. Write a function that check whether the given year is a leap year or not.
void leap (void)
{
int y;
cout<<" \n Enter The year : ";

cin>> y;
if (y%4==0 || y%100>0 && y%400==0 )
cout<<y<<" is a leap year ";
}
int main ()
{
leap ();
getch ();
}

LAB 10 (Pointers)
6.
c)
int num, *numPtr;
num = 987;
numPtr = &num;
cout<<"The value in num is ,"<< num<<endl;
cout<<"The address of num is, "<<&num<<endl;
cout<<"The value in numPtr is ,"<< numPtr<<endl;
cout<<"The value in the address pointed by numPtr is ,"<<
*numPtr<<endl;
cout<<"The address of numPtr is ,"<< &numPtr<<endl;
cout<<"The value in the address of num is ,
"<<*&num<<endl;

Output:

d) Given the following declaration.


int a=5;
int b=7;
int *p = &a;
int *q = &b;
What is the value of each of the following expression?
ii.
++a
ii. ++(*p)
iii. &a
v. *p
vii.
--(*q)
vii. --b;
viii. &b
ix. q++

iv. p++
x. *q

Output:

7. Write a function that receives a floating-point number as reference


parameter and sends back the integer and fraction parts.
8. Write a function names addem with two call-by-reference integer
parameters. The function is to add 2 to the first parameter and 5 to the
second parameter. Test the function by calling it with the values of 17
and 25.

LAB 11 (Pointers Arrays)


6.

Output:

int main()
{
int num[5] = {3,4,6,2,1};
int *p = num;
int *q = num+2;
int *r = &num[1];
cout<<num[2]<<setw(3)<<*(num +
2)<<endl;
cout<<*p<<setw(3)<<*(p +1)<<endl;
cout<<*q<<setw(3)<<*(q+1)<<endl;
cout<<*r<<setw(3)<<*(r+1)<<endl;
b)
} Given the following declaration.

char a[20] = {z, x, m, s, e, h};

char *pa = a;
int i = 2;
int j = 4;
int *pi = &i;
What is the value of each of the following expression?
ii.
*(pa + j)
ii. *(pa + *pi)
Output:

int main()
{
int a[5]={1,2,3,4,5};
int *p;
cout<<"Array itself a Pointer\n";
for(int i=0;i<5;i++)
{
cout<<*a<<"\t";
*a+=1;
}
cout<<endl;
p=&a[2];
cout<<"Array Pointer "<<a[0]<<"\tPointer
2]<<endl;
cout<<"Array Pointer "<<a[1]<<"\tPointer
1]<<endl;
cout<<"Array Pointer "<<a[2]<<"\tPointer
"<<p[0]<<endl;
cout<<"Array Pointer "<<a[3]<<"\tPointer
"<<p[1]<<endl;

value "<<p[value "<<p[value


value

7. Write a function that reverses the elements of an array in place. In


other words, the last element must become the first; the second from
the last become the second, and so on. The function must accept only
ne pointer value and return void.
8. We have two arrays, A and B, each containing ten integers. Write
function that checks if every element of array A is equal to its

corresponding element in array B. in other words, the function must


check if A[0] is equal to B[0], A[1] is equal to B[1], and so on. The
function must accept only two pointer values and return an integer
zero for equal and not zero for unequal.

You might also like