CS2310-Lab 10 Pointers and Call by Reference Solved
You will test your programs locally. There’s no need to upload your programs to PASS
Q-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 addresses of A and B to ptrA and ptrB respectively and display the values in A and B using ptrA and ptrB.
Note:
1) Outputs may be different in different runs as we print addresses in program, the same hereinafter.
2) To manipulate the stream to print foo in hexadecimal use the hex manipulator: cout << hex << foo;
Expected Outputs:
Enter value of A: 10
Enter value of B: 20
Value of ptrA is 10 stored in address 002EF83C
Value of ptrB is 20 stored in address 002EF830
Q-2. Write a C++ program to find the max of an integer array. The program will ask the user to input the size of the array and the value of each element. The program prints on screen the maximum value in this array and the pointer that points to the maximum value.
Expected Outputs.
Enter number of values: 3 Enter values in array:
1 2 3
Largest integer value in the array is 3
Largest integer value in the array is stored in address
00659AC8
1
Computer Programming
Q-3. Download ex1.cpp and ex2.cpp. Compile and execute the program. Explain the output.
File Program segment Ex1.cpp #include <iostream
using namespace std;
int main() { int v = 5, *ptr; ptr = &v; *ptr = 42;
cout << "v = " << v << endl; cin *ptr; // Let’s enter 100
// What happens if you write cin ptr; ? cout << "v = " << v << endl; v = 7;
cout << "*ptr is " << *ptr << endl; cout << "Address of v is " << ptr << endl; return 0;
}
Ex2.cpp #include <iostream
using namespace std;
void f(int *a, int *b) {
int *c; c = a; *c = *c + 10;
*b = *b + 10;
}
int main() { int x = 3, y = 4; int *ptr1; ptr1 = &x; f(ptr1, &y);
cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "*ptr1 = " << *ptr1 << endl; return 0;