C++ Lab 12 Alpha

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

LAB MANUAL # 12

Course: Computer Programming (CP-107)


Session: 22CP (Alpha)

Pointers

This lab has been designed to enable students to practice creating and using pointers. In this
lab students will practice how to create and use pointer variables, how to pass pointers as
arguments to functions, how to use array as pointers and creating dynamic arrays. Clear
concept about all these things has already been provided in the class along with examples
and is also mentioned in lab handouts.
Objectives:
In this lab, students will practice:
• Declare and use pointer variables.
• Using pointer as arrays.
• Creating dynamic arrays.

Lab Tasks: 15/


Question # 1: 2/
The following function uses reference variables as parameters. Rewrite the function so
it uses pointers instead of reference variables, and then demonstrate the function in a
complete program.
int doSomething(int &x, int &y)
{
int temp = x;
x = y * 10;
y = temp * 10;
return x + y;
}
Question # 2: 2/
The mode of a set of values is the value that occurs most often or with the greatest
frequency. Write a function that accepts as arguments the following:
• An array of integers
• An integer that indicates the number of elements in the array
The function should determine the mode of the array. That is, it should determine which
value in the array occurs most often. The mode is the value the function should return. If
the array has no mode (none of the values occur more than once), the function should
return -1. Demonstrate your pointer skills by using pointer notation instead of array
notation in this function.
Question # 3: 2/
Write a function named getString that has a local char array of 80 elements. The function
should ask the user to enter a sentence and store the sentence in the array. Then the
function should dynamically allocate a char array just large enough to hold the sentence,
LAB MANUAL # 12
Course: Computer Programming (CP-107)
Session: 22CP (Alpha)

plus the null terminator. It should copy the sentence to the dynamically allocated array,
and then return a pointer to the array. Demonstrate the function in a complete program.

Question # 4: 2/
Write a function that accepts an int array and the array’s size as arguments. The function
should create a new array that is one element larger than the argument array. The first
element of the new array should be set to 0. Element 0 of the argument array should be
copied to element 1 of the new array, element 1 of the argument array should be copied
to element 2 of the new array, and so forth. The function should return a pointer to the
new array.

Question # 5: 2/
Write a program that dynamically allocates memory for an integer array using the new
operator. The user is then asked to enter the values for the array elements. After displaying
the original array elements, the user is prompted to enter an index to update a specific
element in the array. If the index is valid, the program asks for a new value and updates
the array accordingly. Finally, it displays the updated array elements.

Question # 6: 5/

Do as directed:
int x = 50, y = 60, z = 70; Output
int *ptr;
cout << x << " " << y << " " << z << endl;
ptr = &x;
*ptr *= 10;
ptr = &y;
*ptr *= 5;
ptr = &z;
*ptr *= 2;
cout << x << " " << y << " " << z << endl;
for (int x = 0; x < 100; x++) Rewrite the following loop so it uses
cout << arr[x] << endl;
pointer notation (with the indirection
operator) instead of subscript
notation.

int ivar; Valid/invalid (why?)


float *iptr = &ivar;
int numbers[] = { 2, 4, 6, 8, 10 }; Output
cout << *(numbers + 3) << endl;
int set[10]; Write a statement using pointer
notation that stores the value 99 in
set[7];
int numbers[] = {10, 20, 30, 40, 50}; Error
cout << "The third element in the array is ";
cout << *numbers + 3 << endl;

****************

You might also like