0% found this document useful (0 votes)
268 views

Lab 02 Solution

The document contains solutions to two tasks involving pointers in C++. Task 1 asks the user to enter two integers which are stored in variables a and b. Pointer variables ptrA and ptrB are assigned the addresses of a and b. The program then displays the values of a and b through the pointers and their addresses. Task 2 asks the user to enter the number of integers for an array. It takes input for the array, finds the maximum value using a for loop, stores it in the variable max, and assigns a pointer ptr to the address of max. The program then displays the maximum value through the pointer.

Uploaded by

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

Lab 02 Solution

The document contains solutions to two tasks involving pointers in C++. Task 1 asks the user to enter two integers which are stored in variables a and b. Pointer variables ptrA and ptrB are assigned the addresses of a and b. The program then displays the values of a and b through the pointers and their addresses. Task 2 asks the user to enter the number of integers for an array. It takes input for the array, finds the maximum value using a for loop, stores it in the variable max, and assigns a pointer ptr to the address of max. The program then displays the maximum value through the pointer.

Uploaded by

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

Activity

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;

cout<<"Enter number of values:";


cin>>n;
cout<<"Enter values in array:\n";
intarr[n];

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;

cout<<"Largest integer value in the array is "<<*ptr;


getch();
return 0;
}

You might also like