0% found this document useful (0 votes)
127 views9 pages

CCS0007 - Laboratory Exercise 5

This document outlines a laboratory exercise on pointers for a Computer Programming 2 course. It addresses using pointers to manipulate strings and reverse a string. The exercise involves modifying code to implement the strcat function using pointers and creating a program to reverse a string using a pointer. It provides background on pointers, grading criteria, activity instructions with space for code and output, and questions on using and creating dynamic arrays.

Uploaded by

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

CCS0007 - Laboratory Exercise 5

This document outlines a laboratory exercise on pointers for a Computer Programming 2 course. It addresses using pointers to manipulate strings and reverse a string. The exercise involves modifying code to implement the strcat function using pointers and creating a program to reverse a string using a pointer. It provides background on pointers, grading criteria, activity instructions with space for code and output, and questions on using and creating dynamic arrays.

Uploaded by

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

COLLEGE OF COMPUTER STUDIES

CCS007L
(COMPUTER PROGRAMMING 2)

EXERCISE

5
POINTERS

Student Name / Group


Name: Zamora, Brandon Syd V.
Name Role
Members (if Group):

Section: Tx05

Professor: Jabes Mendoza


I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
 Design, implement and evaluate computer-based systems or applications to meet
desired needs and requirements. [PO: C]

II. COURSE LEARNING OUTCOME/S (CLO)ADDRESSED BY THE LABORATORY EXERCISE


 Apply the fundamental principles of handling CString values, pointers and memory
allocation, and structures using C++in solving computing activities [CLO: 2]

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


At the end of this exercise, students must be able to:
 Create a program that will traverse pointers on a given array and do some
process
 Create a program that simulates some predefined C-String functions using
pointers.

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

CCS007L-Computer Programming 2 Page 2 of 9


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.

Simply, a pointer is a variable that stores the memory address as its value.

A pointer variable points to a data type of the same type, and is created with
the * operator. The address of the variable you're working with is assigned to the pointer:

Create a pointer variable with the name ptr, that points to a string variable, by using the asterisk
sign * (string* ptr). Note that the type of the pointer has to match the type of the variable you're
working with. Use the & operator to store the memory address of the variable called food, and assign it
to the pointer. Now, ptr holds the value of food's memory address.

V. GRADING SYSTEM/ RUBRIC

CCS007L-Computer Programming 2 Page 3 of 9


Trait (Excellent) (Good) (Fair) (Poor)
Able to identify Able to identify Able to identify only Unable to
correctly all input correctly all input one input or output identify any input
and output and and output (22-14pts) and output
Requirement
provide alternative. (25-17pts) (20-11pts)
Specification(30pts)
(28-20pts)

Able to apply Able to apply Able to identify Unable to


required data type required data type required data type or identify required
or data structure or data structure data structure but data type
Data type(20pts)
and produce and produce does apply correctly (9-11pts)
correct results (18- partially correct (12-14pts)
20pts) results (15-17pts)
The program works The program works The program The program
and meets all and meets all produces correct produce s
specifications. Does specifications. results but does not incorrect results
Input
exception al Does some display correctly Does (9-11pts)
Validation(20pts)
checking for errors checking for errors not check for errors
and out-of- range and out of range and out of range data
data (18-20pts) data (15-17pts) (12-14pts)
Unable to run Able to run Able to run program Able to run
program (10pts) program but have correctly without any program
Free from syntax, logic error (8-9pts) logic error and correctly without
logic, and runtime display inappropriate any logic error
errors (10pts) output (6-7pts) and display
appropriate
output (5pts)
The program was The program was The program was The program was
delivered on time delivered after 5 delivered after 10 delivered after
(10pts) minutes from the minutes from the 15 (or more)
Delivery (10pts)
time required. (8- time required. (6- minutes from the
9pts) 7pts) time required.
(5pts)
Use of Comments Specific purpose is Specific purpose is Purpose is noted for No comments
(10pts) noted for each noted for each each function. (6- included. (5pts)
function, control function and 7pts)
structure, input control structure.
requirements, and (8-9pts)
output results.
(10pts)

VI. LABORATORY ACTIVITY

CCS007L-Computer Programming 2 Page 4 of 9


INSTRUCTIONS:
Copy your source codes to be pasted in this document as well as a screen shot of your running
output.
ACTIVITY5.1: strcat function using pointers

Complete the codes for the stringCat function. It will use the same function which is
strcat function.

#include <iostream>
using namespace std;

void stringCat(char *s1, char *s2);

int main(){
char str1[20]= " Happy ";
char str2[20]= " Man";
stringCat(str1,str2);
cout << str1;
system("pause > 0");
return 0;

void stringCat(char *s1, char *s2){


while(*++s1)
;
while(*s1++ = *s2++);

CCS007L-Computer Programming 2 Page 5 of 9


}

ddddddsadsa

CCS007L-Computer Programming 2 Page 6 of 9


ACTIVITY 5.2: Reverse string
Create a program that will return a reverse string using pointer.

sdasdasda

CCS007L-Computer Programming 2 Page 7 of 9


#include <iostream>

using namespace std;


char* stringRev(char *s);

int main(){
char str[]="Happy Day";
cout << stringRev(str);
system("pause > 0");
return 0;
}
char* stringRev(char *s){

char* tmp;
tmp = new char;
int i, cnt(0);
for (i=0; s[i]!='/';i++)
{
cout << s[i];
cnt++;

}
for (i=0; i<cnt; i++)
{
tmp[i] = s[cnt - i - 1];
}
tmp[i] = '\0';
return tmp;

}
VII. AND ANSWER

CCS007L-Computer Programming 2 Page 8 of 9


Briefly answer the questions below. Avoid erasures. For group activity, specify the name of
GROUP MEMBER/s who answered the question. Do not forget to include the source for all
NON-ORIGINAL IDEAS.

 How do you use pointers?


The syntax for using pointers is *

 How do you create a dynamic array?


 We can create a dynamic array using the new keyword (example) pointer_variable =
new data_type;

VIII. REFERENCES
 Zak, Dianne (2016). An Introduction to Programming with C++
 Deitel, Paul & Deitel, Harvey (2012). C++ How To Program, Eighth Edition
 https://www.cprogramming.com/tutorial/lesson6.html

CCS007L-Computer Programming 2 Page 9 of 9

You might also like