You are on page 1of 7

CCS0007

Computer Programming 2 for IT

EXERCISE

4
Pointers

Name of Student Name of Professor


CASISON, ALLYSA MARIE R. SIR MEDINA
Date Performed Date Submitted
OCTOBER 19, 2019 OCTOBER 19, 2019
I. OBJECTIVES
At the end of the experiment students must be able to:
• Create a program that will traverse pointers on a given array and do some process
• Create a program that simulate some pre-defined C-String functions using pointers

II. 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.

III. EXPERIMENTAL PROCEDURE

INSTRUCTIONS:
Copy your source codes to be pasted in this document as well as a screen shot of
your running output.
Upload your document using the link provided in your Canvas.

ACTIVITY4.1: strcat function using pointers


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

#include <iostream>
using namespace std;
void stringCat(char *s1, char *s2);

int main ()
{
char str1[20]="The 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++) );


}

ACTIVITY 4.2: Reverse string


Create a program that will return a reverse string using pointer.
SOURCE CODE:

#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; i<s[i]!='\0'; i++){
cout << s[i];
cnt++;
}
for(i=0; i<cnt; i++)
{
tmp[i] = s[cnt - i - 1];
}
tmp[i]='\0';
return tmp;
}
Question and Answer
1. How does strcat function works?
It is also the function which called concatenate which means to combine the string by adding it
on the end of the other string.

2. How do you use pointers?


We use the * symbol or what we called the deferencing operator and the & symbol which is the
address of operator. It is where it holds the memory address.

IV. ASSESSMENT

Department Information Technology


Subject Code CCS0007
Description COMPUTER PROGRAMMING 2 FOR IT
Term/Academic Year

Topic Pointers
Lab Activity No 4
Lab Activity Pointers
CLO 2

Note: The following rubrics/metrics will be used to grade students’ output in the lab
exercise.
Trait (Excellent) (Good) (Fair) (Poor)
Able to identify Able to identify Able to identify Unable to
correctly all correctly all only one input or identify any
Requirement input and output input and output output input and output
Specification(30pts) and provide (25-17pts) (22-14pts) (20-11pts)
alternative.
(28-20pts)
Able to apply Able to apply Able to identify Unable to
required data required data required data identify required
type or data type or data type or data data type
Data type(20pts) structure and structure and structure but (9-11pts)
produce correct produce partially does apply
results (18- correct results correctly (12-
20pts) (15-17pts) 14pts)
The program The program The program The program
works and meets works and meets produces correct produce s
Input all all results but does incorrect results
Validation(20pts) specifications. specifications. not display (9-11pts)
Does exception Does some correctly Does
al checking for checking for not check for
errors and out- errors and out of
errors and out of
of- range data range data (15- range data (12-
(18-20pts) 17pts) 14pts)
Unable to run Able to run Able to run Able to run
program (10pts) program but program program
Free from syntax, have logic errorcorrectly without correctly without
logic, and runtime (8-9pts) any logic error any logic error
errors (10pts) and display and display
inappropriate appropriate
output (6-7pts) output (5pts)
The program The program The program The program was
was delivered on was delivered was delivered delivered after
time (10pts) after 5 minutes after 10 minutes 15 (or more)
Delivery (10pts)
from the time from the time minutes from the
required. (8-9pts) required. (6-7pts) time required.
(5pts)
Use of Comments Specific purpose Specific purpose Purpose is noted No comments
(10pts) is noted for each is noted for each for each included. (5pts)
function, control function and function. (6-
structure, input control structure. 7pts)
requirements, (8-9pts)
and output
results. (10pts)

Topic Pointers
Lab Activity No 4.1
Lab Activity strcat function using pointers
CLO 2
Requirement Specification
(30pts)
Data type (20pts)
Input Validation (20pts)
Free from syntax, logic,
and runtime errors (10pts)
Delivery (10pts)
Use of Comments (10pts)
TOTAL

Topic Pointers
Lab Activity No 4.2
Lab Activity Reverse string
CLO 2
Requirement Specification
(30pts)
Data type (20pts)
Input Validation (20pts)
Free from syntax, logic,
and runtime errors (10pts)
Delivery (10pts)
Use of Comments (10pts)
TOTAL

You might also like