In: Computer Science
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign address of a to v. 5) write a loop to print values of array a using pointer variable v
//Program:
#include<iostream>
using namespace std;
int main()
{
int a[10], *p, *q, *v; //Declare an array a of size 10
and three pointer variables p, q, and v
int i;
for(int i=0; i<10; i++) //loop to fill
array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
{
a[i]=(i+1)*10;
}
p=
&a[2];
//assigning value of a[2] to p
q =
&a[5];
//assigning value of a[5] to q
i = *q - *p;
cout<<"The value of i is"<<
i<<endl;
i = *p - *q;
cout<<"The value of i is "<<
i<<endl;
v=a;
//assign address of a to v
for(i=0; i<10;
i++)
//loop to print values of array a using pointer variable v
{
cout<<*(v+i)<<" ";
}
return 0;
}
//Output: