0% found this document useful (0 votes)
4 views10 pages

Sheet3 (Recursive&Pointers)

Uploaded by

ahmedshawku5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Sheet3 (Recursive&Pointers)

Uploaded by

ahmedshawku5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Recursion part

A)print number of zeros in an integer number which will entered by


the user using recursive function.
#include<iostream>
using namespace std;
int countZero(int n)
{
if (n==0) return 1;
else if (n<10) return 0;
else if (n%10==0) return
countZero(n/10)+1;
else countZero(n/10);

int main()
{ int n,count;
cin>>n;
count=countZero(n);
cout<<"no of zero in"<<n<<" = "<<count;
}

A)Write a program to add numbers from 1 to n using recursion.

#include<iostream>
using namespace std;

int sum (int n)


{
if(n==1)
return 1;
else
return (n+sum(n-1));
}

int main()
{int a;
cout<<"enter value of a";
cin>>a;
int res=sum(a);
cout<<"res= "<<res;
cin>>res;
return 0;
}
Pointer sheet

A)Write a program to read ten numbers from a user and store them in
array called a then call a function to calculate the square of each
number.
 By calling function 10 times.
 By calling function only one time using global variable.
 By calling function only one time using pointer.
//call function 10 times
#include<iostream>
using namespace std;

int square(int n)
{
int res=n*n;
return res;
}

int main()
{
int a[10],i;
cout<<"enter 10 number";
for(i=0;i<10;i++)
{
cin>>a[i];
}
for(i=0;i<10;i++)
{
cout<<square(a[i])<<endl;
}
return 0;
}
//global variables
#include<iostream>
using namespace std;

int a[10];

void square()
{
for(int i=0;i<10;i++)
a[i]=a[i]*a[i];
}
int main()
{
cout<<"enter 10 number";

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

square();

for(int i=0;i<10;i++)
{
cout<<a[i]<<endl;
}

return 0;
}
//Pointers
#include<iostream>
using namespace std;

void square(int *p)


{
for(int i=0;i<10;i++)
{
//*p =*p * *p;
//p++;
*(p+i)=*(p+i) * *(p+i);
}
}
int main()
{
int a[10];

cout<<"enter 10 number";
for(int i=0;i<10;i++)
{
cin>>a[i];
}
//square(&a[0]);
square(a);
for(int i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
return 0;
}

B) Write a program by using pointers to read 3 arrays [a,b,c] each


array has 10 elements and calculate the sum of every two arrays
[a+b,b+c,a+c] using 3 functions read, add, print.

#include<iostream>
using namespace std;

void read(int *p, int n)


{
for(int i=0;i<n;i++)
{
cin>>*(p+i);
}
}
void print(int *p, int n)
{
for(int i=0;i<n;i++)
{
cout<<*(p+i)<<endl;
}
}
void add(int *p1, int *p2,int *p3,int n)
{
for(int i=0;i<n;i++)
{
*(p3+i)=*(p1+i) + *(p2+i);
}
}
int main()
{int a[10],b[10],c[10],
res1[10],res2[10],res3[10];
cout<<"enter array a";
read(a,10);
cout<<"enter array b";
read(b,10);
cout<<"enter array c";
read(c,10);
add(a,b,res1,10);
add(b,c,res2,10);
add(a,c,res3,10);
cout<<"a+b"<<endl;
print(res1,10);
cout<<"b+c"<<endl;
print(res2,10);
cout<<"a+c"<<endl;
print(res3,10);

return 0;
}

C) Write a program to call a function that print the reverse order of n


numbers using pointer.
#include <iostream>
using namespace std;

void reverse(int *p,int size)


{
for(int i=size;i>=0;i--)
{
cout<<*(p+i)<<endl;
}
}

int main (){


int * array;
int n;
cout<<"please enter number of numbers";
cin>>n;
array= new int[n];
cout<<"Enter the array numbers";
for(int i=0;i<n;i++)
{
cin>>*(array+i);
}
reverse(array,n);

D)Tracing the following code and choose the right answer.


1. int a;
int* p;
a = 2;
p = &a;
a = a + 1;
cout << *p;
a) 2 b) 3 c) Won't run

2. int a;
int* p;
a = 2;
p = a;
a = a + 2;
cout << *p;
a) 2 b) 4 c) Won't run

3. int a;
int b;
int* p;
p = &a;
*p = 4;
p = &b;
*p = 3;
cout << a << “ “ << b;

a) 4 3 b) 3 3 c) Won't run

4. int a;
int b;
int* p;
int* q;
a = 3;
p = &a;
q = p;
*q = *q + 5;
cout << *p;
a) 8 b) 3 c) Won't run

5. int a;
int* p;
a = 4;
p = &a;
cout << (*p) / a;
a) 1 b) 4 c) Won't run

6. ref(int* p)
{(*p) = (*p) * 2;}
int main()
{
int a = 5;
ref(&a);
cout << a;
}

a) 5 b) 10 c) Won't work

7. int a;
int b;
int* p;
int* q;
a = 3;
p = &a;
q = p;
b = 4;
*q = b;
cout << *p << a;

a) 4 3 b) 3 4 c) 4 4

8. int a;
int* p;
a = 3;
p = &a;
cout << p;
a) 3 3 b) A memory address c) Won’t run

You might also like