Pointers Programming Exercise P-2

Pointers Exercise

Programming Exercise

Pointers Programming Exercise P-1

Array Programming Exercise P-7

Q.1 Write a program that inputs data into an array and sorts it using pointers.

#include<iostream.h>
#include<conio.h>
main()
{
int a[5], *pa, u, i, temp;
clrscr();
for(i = 0; i<=4; i++)
{
cout<<"Enter value in element "<<i<< " ? ";
cin>>a[i];
}

pa = a;
u = 4;
while(u>=1)
{
i = 0;
while(i<u)
{
if(*(pa+i) > *(pa+i+1))
{
temp = *(pa+i);
*(pa+i) = *(pa+i+1);
*(pa+i+1) = temp;
}
i++;
}
u--;
}


cout<<"Values of array in ascending order"<<endl;
for(i = 0; i<=4; i++)

cout<<*(pa+i)<<endl; //or cout<<*(pa++)<<endl;
getch();
}

Q.2 Write a program that inputs data into an array and displays the even values from the array using pointers.

#include<iostream.h>
#include<conio.h>
main()
{
int a[5], *pa;
clrscr();
for(int i = 0; i<=4; i++)
{
cout<<"Enter value in element "<<i<< " ? ";
cin>>a[i];
}

pa = a;
cout<<"\nEven Values are: "<<endl;
for(i = 0; i<5; i++)
if(*(pa+i)%2==0) cout<<pa[i]<<endl;
getch();

}

Q.3 Write a program that declares ten float type variables a, b, c, d, e, f, g, h, i, and j. It also declares an array of pointer with ten elements. The first element refers to variable ‘a’, second element refers to variable ‘b’ and so on. The program should use the array of pointer to input values into the variables and then display the average of the values.

#include<iostream.h>
#include<conio.h>
main()
{
float *P[10], a, b, c, d, e, f, g, h, i, j, avg;
P[0] = &a;
P[1] = &b;
P[2] = &c;
P[3] = &d;
P[4] = &e;
P[5] = &f;
P[6] = &g;
P[7] = &h;
P[8] = &i;
P[9] = &j;

clrscr();
cout<<"Input values into a, b, c, d, e, f, g, h, i and j via *P:\n";
for(int x = 0; x<=9; x++)
{
cout<<" Enter value in element "<<x<< " of array ? ";
cin>>*P[x];
}
avg = (a+b+c+d+e+f+g+h+i+j)/10.0;
cout<<"Average of ten values : "<<avg;

getch();
}

Q.4 Write a program to sum two values by passing pointers as arguments to a function.

#include<iostream.h>
#include<conio.h>
main()
{
int sum(int *, int *), s, a, b;
clrscr();
cout<<"Enter first value ? ";
cin>>a;
cout<<"Enter second value ? ";
cin>>b;
s = sum(&a, &b);
cout<<"Sum of "<<a<<" and "<<b<<" is : "<<s;
getch();
}

int sum (int *x, int *y)
{

return *x+*y;
}

Q.5 Write a program to calculate power of a number by passing two pointer values as arguments to a function.

#include<iostream.h>
#include<conio.h>
main()
{
int power(int *, int *), p, a, b;
clrscr();
cout<<"Enter a number for base ? ";
cin>>a;
cout<<"Enter value for power of base ? ";
cin>>b;
p = power(&a, &b);
cout<<"Result is : "<<p;
getch();
}

int power (int *x, int *y)
{
long pp = 1;
for(int i = 1; i<=*y; i++)
pp = pp * *x;
return pp;
}

Q.6 Write a program that inputs any integer value and then finds out if the given value is a prime number or not by passing the number to a function as pointer argument.

#include<iostream.h>
#include<conio.h>
main()
{
int prime(int *), res, n;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
res = prime(&n);
if (res == 1)
cout<<n<<" is a prime number ";
else
cout<<n<<" is not a prime number ";
getch();
}

int prime (int *x)
{
int d;
for(d=2; d<= *x/2; d++)
if (*x%d ==0)
return 0;
return 1;
} 

Q.7 Write a program that inputs the number of employees and then declares a dynamic float type array ‘emp’ with the same number of elements as number of employees are entered. It inputs the salaries of employees in the array and computes the average salary of all the employees.

#include<iostream.h>
#include<conio.h>
main()
{
float *emp, s, avg_sal;
int n, i;
clrscr();
cout<<"Enter the size of array ? ";
cin>>n;
emp = new float[n];

for(i = 0; i<n; i++)
{
cout<<"Enter salary of employee "<<i+1<< " ? ";
cin>>*emp;
s = s + *emp;
*emp++;
}

avg_sal = s/n;
cout<<"Average salary of all employees is: "<<avg_sal;
delete[] emp;
getch();
}

Leave a Reply