[Solved] CS2310 Lab 10-Pointers and Call by Reference

$25

File Name: CS2310_Lab_10-Pointers_and_Call_by_Reference.zip
File Size: 414.48 KB

SKU: [Solved] CS2310 Lab 10-Pointers and Call by Reference Category: Tag:
5/5 - (1 vote)

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:

  • Outputs may be different in different runs as we print addresses in program, the same hereinafter.
  • 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

  • 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 3Largest integer value in the array is 3Largest integer value in the array is stored in address00659AC8

1

Computer Programming

  • 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; // Lets 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;}

2

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CS2310 Lab 10-Pointers and Call by Reference
$25