[M2-TECHNICAL](1)
[M2-TECHNICAL](1)
[M2-TECHNICAL](1)
CCS0015L
(DATA STRUCTURES AND ALGORITHMS)
EXERCISE
2
REVIEW OF POINTERS
Name Role
Members (if Group):
Section:
Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
● Understand the fundamental principles of data structures and algorithms: concepts of abstract data; types of
common data structures; description, properties, and storage allocation of data structures. [CLO: 1]
A pointer is a variable whose value is the address of another variable. Like any variable or constant, you
must declare a pointer before you can work with it. The general form of a pointer variable declaration is:
Type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the
pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for
multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
Following are the valid pointer declaration:
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the
same, a long hexadecimal number that represents a memory address. The only difference between
pointers of different data types is the data type of the variable or constant that the pointer points to.
There are few important operations, which we will do with the pointers very frequently. (a) we define a
pointer variables (b)assign the address of a variable to a pointer and (c) finally access the value at the
address available in the pointer variable. This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand.
Write a C++ program that has a menu that performs the following options:
string_length() - function to find the length of a string using pointers.
string_compare() - function to compare two strings, s1 and s2 using pointers.
string_concatenate() - function to concatenate string s2 onto the end of string s1 using pointers.
Note: The functions should not use string functions available in string.h
Sample Output:
#include <iostream>
int main() {
char str1[100];
char str2[100];
int choice;
do {
std::cout << "Select an option:\n";
std::cout << "1. Find the length of a string\n";
std::cout << "2. Compare two strings\n";
std::cout << "3. Concatenate two strings\n";
switch (choice) {
case 1: {
std::cout << "Enter a string: ";
std::cin >> str1;
int length = string_length(str1);
std::cout << "Length of the string: " << length << std::endl;
break;
}
case 2: {
std::cout << "Enter the first string: ";
std::cin >> str1;
std::cout << "Enter the second string: ";
std::cin >> str2;
int result = string_compare(str1, str2);
if (result < 0)
std::cout << "String 1 is less than String 2" << std::endl;
else if (result > 0)
std::cout << "String 1 is greater than String 2" << std::endl;
else
std::cout << "String 1 is equal to String 2" << std::endl;
break;
}
case 3: {
std::cout << "Enter the first string: ";
std::cin >> str1;
std::cout << "Enter the second string: ";
std::cin >> str2;
string_concatenate(str1, str2);
std::cout << "Concatenated string: " << str1 << std::endl;
break;
}
case 4: {
std::cout << "Exiting..." << std::endl;
break;
}
default: {
std::cout << "Invalid choice. Please try again." << std::endl;
break;
}
return 0;
}
VII. REFERENCES
● Wittenberg, Lee.(2018). Data structures and algorithms in C++. s.l.: Mercury Learning
● Baka, Benjamin(2017). Python data structures and algorithms : improve the performance and speed of
your applications. Birmingham, U.K : Packt Publishing
● Downey, Allen.(2017). Think data structures : algorithms and information retrieval in Java. Sebastopol,
CA: O'Reilly
RUBRIC:
Criteria 4 3 2 1 Score
Solution(x5) A completed
A completed solution is An incomplete
A completed
solution is implemented solution is
solution runs
tested and runs on the required implemented
without errors.
but does not platform, and on the required
It meets all the
meet all the uses the platform. It
specifications
specifications compiler does not
and works for
nd/or work for specified. It compile and/or
all test data.
all test data. runs, but has run.
logical errors.
Program
Design(x3) The program Not all of the
Few of the
The program design selected
selected
design uses generally uses structures are
structures are
appropriate appropriate appropriate.
appropriate.
structures. The structures. Some of the
Program
overall program Program program
elements are
design is elements elements are
not well
appropriate. exhibit good appropriately
designed.
design. designed.
Completeness
of
Document(x2) All required There are few
All required
parts of the parts of the Most of the
parts in the
document are document are parts of the
document are
complete and missing but the document are
present and
correct(code, rest are missing and
correct but not
output of complete and incorrect.
complete.
screenshots) correct.
Total