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

Lab 04

The document provides instructions for a lab assignment on pointers in C++. It covers declaring and dereferencing pointers, dynamically allocating memory, passing pointers to functions, and various exercises involving arrays and strings including operations like addition, counting evens, and reversing.

Uploaded by

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

Lab 04

The document provides instructions for a lab assignment on pointers in C++. It covers declaring and dereferencing pointers, dynamically allocating memory, passing pointers to functions, and various exercises involving arrays and strings including operations like addition, counting evens, and reversing.

Uploaded by

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

Lab 04: Pointers

CS 224 Object Oriented Programming and Design Methodologies


Habib University
Spring
Fall 2022

Pointer variables are used to store addresses. The basic minimum one needs to know about pointers is:
how to declare a pointer, how to get address of a variable, and how to dereference a pointer. The first listing
shows all three concepts.

int n = 55;
int * p = &n ;
std : : c o u t << ” t h e a d d r e s s o f n i s : ” << p ;
std : : c o u t << ” t h e v a l u e s t o r e d a t a d d r e s s ”<<p<<” i s : ” << * p ;

1. Char*
In the first listing we were able to print the value of the integer pointer p which contained the address
of the integer n. Try doing the same with a character pointer, i.e., declare a character variable, store its
address in a character pointer and print the value of that character pointer.

You will see that things don’t work the way you would expect. That is because the default behaviour of
cout treats a character pointer as the start of a string (a null terminated character array) and tries to
output a string. Think of an alternate way to store the address of a char and write a program to store
and print it.
2. Dynamic Memory Allocation
We saw in class that an array is a pointer. One common use of pointers is to use them to store the
addresses of dynamically created arrays. For example, the following code uses a pointer to store the start
address of an array of size n.
int n ;
s t d : : c i n >> n ;
int * a r r a y = new int [ n ] ;

As we have created the array dynamically, it is now our responsibility to destroy it. Once we are done
working with the array, we have to destroy it as follows.
delete [ ] a r r a y ;

Write a program to input the size, n, of an integer array from the user. The program then

ˆ create an integer array of size n


ˆ populates the array with n random integers
ˆ outputs the input array to screen

1
CS 224 Lab 04 Spring 2022

Write three separate functions for the above tasks and call these functions in your program. Based on
the task functionality, you will decide the arguments and return types of each function.
Make sure to pass a pointer to your functions and to use pointer notation in the functions instead of
array notation. Make sure to destroy all memory that you created (using new) when you are done with
it and before exiting the program.

Observe the following instructions in the questions that follow.


ˆ Wherever possible, reuse functions from previous problems.
ˆ Pass pointers to your functions instead of arrays, .
ˆ Use pointer notation in your functions instead of array notation.
ˆ Make sure to destroy (using delete []) all memory that you created (using new) when you are done
with it and before exiting the program.
ˆ Write appropriate code to test the functionality you have coded.

3. Vector Arithmetic
In Lab 2, we wrote functions to perform arithmetic on vectors. These functions took the empty result
vector as an argument and populated it. We will now write functions to perform vector arithmetic that
will not take the result vector as arguments. Rather they will create and return the result vector. Write a
program to input the dimension, n, of the vectors you will work with. The program then inputs 2 vectors
from the user and asks the user for the desired operation–addition or subtraction. The corresponding
functions (for addition and subtraction) take the vectors and their dimension as arguments and return
the result vector which was dynamically created inside the function. Your program should then print
the result vector.
4. Array Operations
(a) Write a function count even(int*, int) which receives an integer array and its size, and returns
the number of even numbers in the array.
(b) Write a function that returns a pointer to the maximum value in a double array. If the array is
empty, return NULL.
(c) Write a function that receives an array and its size and returns another array of the same size and
containing the same elements.
(d) Write a function that receives 2 arrays and their size and swaps their elements. Both arrays have
the same size. The function does not return anything.
5. String Operations [Bonus: to be done at home if time falls short]
(a) Write a function my strlen(char*) which returns the length of the parameter cstring. Write the
function without using the C++ function strlen.
(b) Write a function that takes a string (char*) as argument and prints it in reverse order.
(c) Write a function that takes a string (char*) as argument and reverses it. The function returns
nothing.
(d) Write a program to input 2 strings from the user and returns a new string which is the concatenation
of the two input strings (the second appended to the first).
(e) Write a function that receives 2 strings and returns 0 if their lengths are equal, -1 if the first string
is longer, and +1 if the second string is longer.
(f) Write a function contains(char*, char) which returns true if the 1st parameter cstring contains
the 2nd parameter char, or false otherwise.
(g) A palindrome is a string that reads the same forward and backward, e.g. “madam”. Write a function
that receives a string and returns true if the string is a palindrome, or false otherwise.

Page 2 of 3
CS 224 Lab 04 Spring 2022

Credits
Some questions have been adapted from the following websites.
ˆ http://condor.depaul.edu/ntomuro/courses/309/notes/pointer_exercises.html

ˆ https://codeforwin.org/2017/12/pointer-programming-exercises-and-solutions-in-c.html

Page 3 of 3

You might also like