Lab 02 Solution
Lab 02 Solution
CS102-Lab 2
Date:
25-01-2016
Instructors:M. Haris,Fatima,Ifra
Pointers in C++
Task 1
Write a program that asks the user to enter integers as inputs to be stored in
the variables 'a' and 'b' respectively. There are also two integer pointers
named ptrA and ptrB. Assign the values of 'a' and 'b' to ptrA and ptrB
respectively, and display the values of a, b through pointer. Also display
address of a & b, address of ptrA&ptrB. Addresses of a & b thats stored
in the ptrA and ptrB. Note that address store in ptrA must same as address of
A and address store in ptrb must same as address of B.
Solution :
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout<< "Enter value of A: ";
cin>> a;
cout<< "Enter value of B: ";
cin>> b;
int *ptrA=&a;
int *ptrB=&b;
cout<< "Value of ptrA is " << *ptrA<< " stored in address "<<ptrA<<"\n";
cout<< "Value of ptrBis "<< *ptrB<<" sored in address "<<ptrB<<"\n";
return 0;
}
Task 2
Write a C++ program to find the max of an integral data set. The program
will ask the user to input the number of data values for arraythen insert the
values to array. The program should find the max value in array through
linear search technique and assign the value to the variable by name MAX,
then program display the max value of array through pointer.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
int max=0;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int u=0;u<=n;u++)
{
if (arr[u]>max)
max=arr[u];
}
int *ptr;
ptr=&max;
//int *pointer= &max;