[M2-TECHNICAL](1)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

COLLEGE OF COMPUTER STUDIES AND MULTIMEDIA ARTS

CCS0015L
(DATA STRUCTURES AND ALGORITHMS)

EXERCISE

2
REVIEW OF POINTERS

Student Name / Group


Name:

Name Role
Members (if Group):

Section:

Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE

● Apply knowledge of computing appropriate to the discipline. [PO: A]

II. COURSE LEARNING OUTCOME/S (CLO) 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]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:

● Implement dynamic memory allocation using pointers

● Apply pointers in data structures & classes.

IV. BACKGROUND INFORMATION

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.

CCS0015L-Data Structures and Algorithms Page 2


of 9
V. LABORATORY ACTIVITY

ACTIVITY 2.1: Array Abstract Data Type

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:

CCS0015L-Data Structures and Algorithms Page 3


of 9
Program: (save as [surname_2_1.cpp] )

Note: Write your complete working program here.

#include <iostream>

int string_length(const char* str) {


int length = 0;
while (*str != '\0') {
length++;

CCS0015L-Data Structures and Algorithms Page 4


of 9
str++;
}
return length;
}

int string_compare(const char* str1, const char* str2) {


while (*str1 != '\0' && *str2 != '\0') {
if (*str1 < *str2)
return -1;
else if (*str1 > *str2)
return 1;
str1++;
str2++;
}
if (*str1 == '\0' && *str2 == '\0')
return 0;
else if (*str1 == '\0')
return -1;
else
return 1;
}

void string_concatenate(char* str1, const char* str2) {


while (*str1 != '\0')
str1++;
while (*str2 != '\0') {
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
}

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";

CCS0015L-Data Structures and Algorithms Page 5


of 9
std::cout << "4. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;

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;
}

CCS0015L-Data Structures and Algorithms Page 6


of 9
}
} while (choice != 4);

return 0;
}

Output:(screenshot of the output)

CCS0015L-Data Structures and Algorithms Page 7


of 9
VI. QUESTION AND ANSWER

Directions: Briefly answer the questions below.

● Are pointers important to learn?


Yes, pointers are an important concept in computer programming, especially in languages like C and
C++. Pointers allow programmers to work with memory addresses and manipulate memory directly,
which can be very powerful and efficient.
● What are the problems with pointers?
While pointers are powerful and convenient tools in programming they do have several flaws. For
example, memory Leaks. Pointers can be used to dynamically allocate memory, but it's the
responsibility of the programmer to free that memory when it's no longer needed. If memory allocated
with pointers is not properly deallocated, it can lead to memory leaks, where memory is not released,
and the program consumes more and more memory over time, potentially causing performance issues
or crashes. Another example is null Pointers. Null pointers are pointers that do not point to any valid
memory location. If you try to dereference a null pointer, it can result in crashes or undefined behavior.
Null pointers can often be the cause of bugs and errors in programs.

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

CCS0015L-Data Structures and Algorithms Page 8


of 9
● Chang, Kung-Hua(2017). Data Structures Practice Problems for C++ Beginners. S.l : Simple and
Example
● Hemant Jain(2017). Problem Solving in Data Structures & Algorithms Using C++: Programming
Interview Guide. USA: CreateSpace Independent Publishing Platform

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

CCS0015L-Data Structures and Algorithms Page 9


of 9

You might also like