Kingdom of Saudi Arabia المملكة العـربية السعودية
Ministry of Higher Education وزارة التعليم العالي
Prince Sattam bin Abdulaziz University جامعة األمير سطام بن عبدالعزيز
College of Computer Engineering and Sciences كلية هندسة وعلوم الحاسب
Computer Science Department قسم علوم الحاسب
Course Title: Data Structures and Algorithms (CS2321) / 1443 (2)
Homework 1 (Solutions)
(1)
Let the following program
int n = 5;
char *pv, *ppv;
pv = (char *) malloc(n * sizeof(char));
pv[0] = 'A;'
pv[1] = 'E;'
*(pv + 2) = 'I;'
pv[3] = 'O;'
*(pv + 4) = 'U;'
for (ppv = pv+4; *ppv; ppv--)
print(*ppv);
free(pv);
Write the output.
U O I E A
Transform the program above from using only the subscript notation (without using pointers).
char pv[5];
int i;
pv[0] = 'A;'
pv[1] = 'E;'
pv[2] = 'I;'
pv[3] = 'O;'
pv[4] = 'U;'
for (i=4; i>=0; i--)
print(pv[i]);
CS2321 – Homework 1 Page 1/1
(2)
#include<iostream>
using namespace std;
void printOut(int x)
{
cout<<x%10<<endl;
if(x/10!=0)
printOut(x/10);
}
(3)
024
024
024
420
(4)
P1 P2
A B C
P1 ref to *P1 P2 ref to *P2
Initialization 10 15 20 null X null X
P1=&A 10 15 20 A 10 null X
P2=&C; 10 15 20 A 10 C 20
*P1=(*P2)++; 20 15 21 A 20 C 21
P1=P2; 20 15 21 C 21 C 21
P2=&B; 20 15 21 C 21 B 15
*P1-=*P2; 20 15 6 C 6 B 15
*P1*=*P2; 20 15 90 C 90 B 15
A=(*P2)++ * *P1; 1350 16 90 C 90 B 16
P1=&A; 1350 16 90 A 1350 B 16
*P2=*P1/=*P2; 84 84 90 A 84 B 84
(5) Multiple choice questions
A pointer
a) Refers to many variables.
b) Contains the value of a variable.
c) Contains the address of a variable.
d) None of the above.
CS2321 – Homework 1 Page 2/1
The name of an array represents
a) a pointer to its last case.
b) a pointer to its first case.
c) a reference to its first case.
d) both of b) and c).
What is correct equivalence between the next subscript and offset notations
a) t[i]=2; <=> *t+i=2;.
b) t[i]=2; <=> *(t+i)=2;.
c) t[i][j]=2; <=> **t+i+j=2;.
d) t[i][j]=2; <=> *(*t+i+j)=2;.
(6) Transform the following code from subscript notation to offset notation
subscript notation offset notation
float t[4]; float *t ;
int m[4][5]; int **m;
int i, j; int i, j;
t=(float*)malloc(4*sizeof(float)) ;//Memory allocation
for(i=0; i<4; i++) for(i=0; i<4; i++)
t[i]=(float)i+1; *(t+i)=(float)i+1;
m=(int**)malloc(4*sizeof(int*)) ;//Memory allocation
for(i=0; i<4; i++)
*(m+i) = (int*)malloc(5*sizeof(int)) ;
for(i=0; i<4; i++) for(i=0; i<4; i++)
for(j=0; j<5; j++) for(j=0; j<5; j++)
m[i][j]=(int)t[i]+2*i+j *(*(m+i)+j)=(int)(*(t+i))+2*i+j
for(i=0; i<4; i++) for(i=0; i<4; i++)
{ {
cout<<t[i]; cout<<*(t+i);
for(j=0; j<5; j++) for(j=0; j<5; j++)
cout<<m[i][j]; cout<<*(*(m+i)+j);
} }
free(t); ;//Memory free
for(i=0; i<4; i++)
free(m[i]);
free(m);
(7) Let the following program
void main()
{
int X = -5, Y = 6, Z = 20;
int *P1, *P2;
P1=&X;
P2=&Y;
*P1=*P2+Z;
Z=*P1+Z-10+*P2;
P1=P2=&Z;
*P2=2**P1+Y-X
}
CS2321 – Homework 1 Page 3/1
Complete the following table for each instruction of the above program.
P1 P2
X Y Z
P1 ref to *P1 P2 ref to *P2
Initialization -5 6 20 null -- null --
P1=&X; -5 6 20 X -5 null --
P2=&Y; -5 6 20 X -5 Y 6
*P1=*P2+Z;
26 6 20 X 26 Y 6
*P1=6+20=26
Z=*P1+Z-10+*P2;
26 6 42 X 26 Y 6
Z=26+20-10+6
P1=P2=&Z; 26 6 42 Z 42 Z 42
*P2=2**P1+Y-X
26 6 64 Z 64 Z 64
*P2=2*42+6-26=64
(8) Given the following program
void main()
{
int a = 5, b = 10;
int *P, *T ;
P=&a;
T=(int*) malloc (5*sizeof(int)) ;
for(int i=0 ; i<5 ; i++)
*(t+i)=i**P+b ;
for(int i=4 ; i>=0 ; i--)
cout<<T[i]<<" " ;
}
Write the output.
30 25 20 15 10
(9) Given the following program
void main()
{
int i,j,*pi,*pj,T[5][4],n=5;
pi=&i;
pj=&j;
for(*pi=0 ; *pi<n ; (*pi)++)
for(j=0 ; j<n-1 ; j++)
*(*(t+*pi)+j)=i+*pj;
*pi=0;
while(i<n)
{
j=0;
while(*pj<n-1)
print(T[*pi][(*pj)++]);
print("\n");
CS2321 – Homework 1 Page 4/1
(*pi)++;
}
}
Write the output.
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
(10*) Let P a pointer that refers to an array A:
int A[] = {12, 23, 34, 45, 56, 67, 78, 89, 90};
int *P;
P = A;
What are the values or addresses provided by the following expression?
Expression Value or address
*P+2 14
*(P+2) 34
&A[4]-3 Address of A[1]
A+3 Address of A[3]
P+(*P-10) Address of A[2]
*(P+*(P+8)-A[7]) 23
(11) Let the following program
void main()
{
int T[3]={5,10,15},*P1=Null, *P2=Null;
P1=T+1;
P2=&T[2];
*T+=*P1+*P2;
for(int i=0;i<3;i++)
*(T+i)+=5;
*(++P1)=*T-5;
}
Complete the following table for each instruction of the above program.
P1 P2
T[0] T[1] T[2]
P1 ref to *P1 P2 ref to *P2
Initialization 5 10 15 Null -- Null --
P1=T+1; 5 10 15 T[1] 10 Null --
P2=&T[2]; 5 10 15 T[1] 10 T[2] 15
*T+=*P1+*P2; 30 10 15 T[1] 10 T[2] 15
CS2321 – Homework 1 Page 5/1
for(int i=0;i<3;i++)
35 15 20 T[1] 15 T[2] 20
*(T+i)+=5;
*(++P1)=*T-5; 35 15 30 T[2] 30 T[2] 30
(12) Let the following program
void main()
{
int T[4]={-15,-10,-5,10} ;
int *P1=T+1 ;
int *P2=T+2 ;
int *P3=Null;
for(P3=T; P3<T+3; P3++)
*P3+=10;
*P3+=T[0]+*P1+*P2 ;
}
Complete the following table for each instruction of the above program.
P1 P2 P3
T[0] T[1] T[2] T[3] P1 ref P2 ref P3 ref
*P1 *P2 *P3
to to to
int T[4]={-15,-10,-5,10} ;
int *P1=T+1 ;
int *P2=T+2 ;
-15 -10 -5 10 T[1] -10 T[2] -5 Null --
int *P3=Null;
for(P3=T; P3<T+3; P3++)
-5 0 5 10 T[1] 0 T[2] 5 T[3] 10
*P3+=10;
*P3+=T[0]+*P1+*P2 ; -5 0 5 10 T[1] 0 T[2] 5 T[3] 10
(13) Let the following program
void main()
{
int T[5]={-20,-10,0,10,20} ;
int *P1=T+3 ;
int *P2=T+2 ;
int *P3=T;
for(int i=4; i>=0; i--)
*(T+i)+=*P3++;
}
Complete the following table for each instruction of the above program.
P1 P2 P3
P1 P2 P3
*T *(T+1) *(T+2) *(T+3) *(T+4)
ref *P1 ref *P2 ref *P3
to to to
int T[5]={-20,-10,0,10,20}
int *P1=T+3 ;
int *P2=T+2 ;
-20 -10 0 10 20 T[3] 10 T[2] 0 T[0] -20
int *P3=T;
for(int i=4; i>=0; i--)
*(T+i)+=*P3++;
-20 -10 0 0 0 T[3] 0 T[2] 0 T[4] 0
CS2321 – Homework 1 Page 6/1
(14) Let the following program
double **t;
int n=2, m=3;
t=(double**)malloc(n*sizeof(double*));
for(int i=0; i<n; i++)
*(t+i)=(double*)malloc(m*sizeof(double));
**t=1.0;
*(*t+1)=2.0;
*(*t+2)=3.0;
**(t+1)=4.0;
*(*(t+1)+1)=5.0;
*(*(t+1)+2)=6.0;
for(int j=0; j<m; j++)
for(int i=n-1; i>=0; i--)
print(*(*(t+i)+j));
for(int i=0; i<n; i++)
free(*(t+i));
free(t);
Write the output.
4 1 5 2 6 3
Transform the program above from using only the subscript notation (without using pointers).
double t[2][3];
t[0][0]=1.0;
t[0][1]=2.0;
t[0][2]=3.0;
t[1][0]=4.0;
t[1][1]=5.0;
t[1][2]=6.0;
for(int j=0; j<m; j++)
for(int i=n-1; i>=0; i--)
print(t[i][j]);
(15*) (4x5 marks)
1. Consider the following statements:
int *p;
int i;
int k;
i = 42;
k = i;
p = &i;
CS2321 – Homework 1 Page 7/1
After these statements, which of the following statements will change the value of i to 75? Explain.
A. k = 75;
B. *k = 75;
C. p = 75;
D. *p = 75;
E. Two or more of the answers will change i to 75.
Explanation:
We have i and k two standard variables and p is a pointer that points to i.
k has the same value of i equal to 42.
For k=75, only the value of k is modified because we use the direct reference so i doesn’t changed.
For *k=75, it is an illegal instruction because k is not a pointer.
For p=75, the content of p (should be an address) is changed to 75 then i doesn’t changed.
For *p=75, the content of the variable pointed (referred) by p (that is i) should be changed to 75.
2. Explain the error.
char c = 'A';
double *p = &c;
Explanation:
p is a pointer of type *double that can’t refer (point) to a variable of type char.
3. Give the value of the left-hand side variable in each assignment statement. Assume the lines are
executed sequentially. Assume the address of the blocks array is 4434.
Value of the left-hand side
Code variable in each assignment
statement
int main()
{
char blocks[3] = {'A','B','C'}; [‘A’|‘B’|‘C’]
char *ptr = &blocks[0]; 4434
char temp; ‘’
temp = blocks[0]; ‘A’
temp = *(blocks + 2); ‘C’
temp = *(ptr + 1); ‘B’
temp = *ptr; ‘A’
ptr = blocks + 1; 4435
temp = *ptr; ‘B’
temp = *(ptr + 1); ‘C’
ptr = blocks; 4434
temp = *++ptr ; ‘B’
temp = ++*ptr; ‘C’
temp = *ptr++; ‘C’
temp = *ptr; ‘C’
return 0;
}
CS2321 – Homework 1 Page 8/1
4. Write a piece of code which prints the characters in a string in a reverse order. Use the offset
notation ONLY. Do NOT use the subscript notation.
char s[10] = "abcde";
char* cptr;
// First advance cptr to the end of the string (where the '\0' char is)
cptr = s;
while(*cptr != '\0')
cptr++;
// Rewind one position to start from the last char
cptr--;
// Use s as an anchor to stop the backward traversal.
// Note *cptr-- does two things: *cptr and cptr--.
while (cptr >= s)
print(*cptr--);
CS2321 – Homework 1 Page 9/1