# Programming Exercise

Array Programming Exercise P-7

Programming Exercise Looping Structure P-12

#### Program 1:Write a program that declares and assigns a value ‘245’ to a variable ‘x’. It displays the value and address of this variable on the screen.

```#include<iostream.h>
#include<conio.h>
main()
{
int x = 245;
clrscr();
cout<<"Value of variable = "<<x<<endl;
cout<<"Address of variable = "<<&x<<endl;
getch();
}```

#### Program 2:Write a program that assigns values to variables using pointer variables. It computes the sum of values and displays the result on the screen.

```#include<iostream.h>
#include<conio.h>
main()
{
int x, y, *px, *py;
clrscr();
px = &x;
py = &y;
*px = 5;
*py = 8;
cout<<"Sum of x & y = "<<x+y;
getch();
}```

#### Program 3:Write a program that initializes a pointer with memory address of a variable. It gets a value into variable through pointer and displays the value of the variable with reference to the name of the variable.

```#include<iostream.h>
#include<conio.h>
main()
{
float x;
float *px = &x;
clrscr();
cout<<"Enter any value ? ";
cin>>*px;
cout<<"Value of variable x = "<<x;
getch();
}```

#### Program 4:Write a program that declares and initializes variables x of ‘int’ type and y of ‘float’ type. It displays the values and addresses of these variables on the screen using pointer to ‘void’.

```#include<iostream.h>
#include<conio.h>
main()
{
int x = 245;
float y = 25.6;
void *pv;
clrscr();
pv = &x;
cout<<"Value of x = "<<x<<endl;
cout<<"Address of x = "<<pv<<endl;
pv = &y;
cout<<"Value of y = "<<y<<endl;
cout<<"Address of y = "<<pv<<endl;
getch();
}```

#### Program 5:Write a program that initializes integer values to an array and displays the values of the array on the screen using pointer notation.

```#include<iostream.h>
#include<conio.h>
main()
{
int a={66,77,44,33,11}, *pa;
clrscr();
pa = a;
cout<<"Values of array "<<endl;
for(int i = 0; i<=4; i++)
cout<<*(pa+i)<<endl; //or cout<<*(pa++)<<endl;
getch();
}```

#### Program 6:Write a program that inputs values into an array and displays the odd values of array on the screen using pointer notation.

```#include<iostream.h>
#include<conio.h>
main()
{
int a, *pa, i;
clrscr();
pa = a;
cout<<"Enter six values "<<endl;
for(i = 0; i<=5; i++)
cin>>a[i];
cout<<"\nOdd Values are: "<<endl;
for(i = 0; i<=5; i++)
if(*(pa+i)%2==1) cout<<pa[i]<<endl;
getch();
}```

#### Program 7:Write a program that inputs values into an array using pointer notation. It displays the values of array in reverse order on the screen using pointer notation.

```#include<iostream.h>
#include<conio.h>
main()
{
int a, *pa, i;
clrscr();
pa = a;
cout<<"Enter six values "<<endl;
for(i = 0; i<=5; i++)
cin>>*pa++;
pa = &a;
cout<<"\n Values in reverse order: "<<endl;
for(i = 0; i<=5; i++)
cout<<*pa--<<endl;
getch();
}```

#### Program 8:Write a program that computes the cube of a number using pointer as argument to function.

```#include<iostream.h>
#include<conio.h>
main()
{
void cube (int *);
int n = 3;
clrscr();
cout<<"Value of n before function call:"<<n<<endl;
cube(&n);
cout<<"Value of n after function call:"<<n<<endl;
getch();
}

// Definition of cube() function
void cube (int *px)
{
*px = *px * *px * *px;
}```

#### Program 9:Write a program that passes an array as argument to a function and multiplies each element of the array by 5 using pointer notation.

```#include<iostream.h>
#include<conio.h>
main()
{
void temp(int *);
int x[] = {6, 2, 12, 3, 32, 7};
clrscr();
temp(x);
for (int i = 0; i < = 5; i ++)
cout<<x[i]<<endl;
getch();
}

void temp (int *p)
{
for(int i = 0; i <= 5; i ++)
{
*p = *p * 5;
p++;
}
}```

#### Program 10:Write a program that swaps (exchanges) two values by passing pointers as arguments to the function.

```#include<iostream.h>
#include<conio.h>

void swap(int*, int*);
main()
{
int x, y;
clrscr();
clrscr();
cout<<"Enter first value ? ";
cin>>x;
cout<<"Enter second value ? ";
cin>>y;
swap(&x, &y);
cout<<"Values after swapping"<<endl;
cout<<"First value = "<<x<<endl;
cout<<"Second value = "<<y<<endl;
getch();
}

void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}```

#### Program 11:Write a program that allocates memory locations for two integer values using ‘new’ operator. It inputs values into the allocated memory locations and computes the sum of these values. It also displays the result on the computer screen. At the end, it de-allocates the allocated memory locations using ‘delete’ operator.

```#include<iostream.h>
#include<conio.h>
main()
{
int *p1, *p2, sum;
clrscr();
p1 = new int;
p2 = new int;
cout<<"Enter first value ? ";
cin>>*p1;
cout<<"Enter second value ? ";
cin>>*p2;
sum = *p1+*p2;
cout<<"Sum of values = "<<sum<<endl;
delete p1;
delete p2;
getch();
}```

#### Program 12:Write a program that allocates memory locations for an array of ‘int’ type using ‘new’ operator and inputs a value for the length of the array during the program execution. It also inputs values into the array and finds out the largest value from the array.

```#include<iostream.h>
#include<conio.h>
main()
{
float *p1,*p2, max;
int n, i;
clrscr();
cout<<"Enter the size of array ? ";
cin>>n;
p1 = new float[n];
p2 = p1; // assigns the starting address of p1 to p2
cout<<"Enter "<<n<< " values in array\n";
for(i = 0; i<n; i++)
cin>>*p1++;
i = 0;
p1 = p2; // assigns the starting address of p1 from p2
max = *p1; // assigns the value of starting address of p1 to max
while(i<n)
{
if(max<*p1)
max = *p1;
*p1++;
i++;
}
cout<<"The Largest value is : "<<max;
delete[] p1;
getch();
}```

#### Program 13:Write a program that allocates memory locations for four integer values using ‘new’ operator and stores the addresses of the allocated memory locations into an array of pointers. It also finds out the smallest value from the four values.

```#include<iostream.h>
#include<conio.h>
main()
{
int *p, *pn1, *pn2, *pn3, *pn4, min, i;
clrscr();
pn1 = new int;
cout<<"Enter first value ? ";
cin>>*pn1;
pn2 = new int;
cout<<"Enter second value ? ";
cin>>*pn2;
pn3 = new int;
cout<<"Enter third value ? ";
cin>>*pn3;
pn4 = new int;
cout<<"Enter fourth value ? ";
cin>>*pn4;

// assign addresses of pn1, pn2, pn3 and pn4 to pointer of array p.

p = pn1;
p = pn2;
p = pn3;
p = pn4;
min = *p; // assigns first value of pointer to array to min
i = 1;
while(i<=3)
{
if(min>*p[i])
min = *p[i];
i++;
}
cout<<"The smallest value is : "<<min;
delete pn1;
delete pn2;
delete pn3;
delete pn4;
getch();
}```