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) / 1438-1439 (1)
Homework 1 (Solutions)
(1)
a.
N N N
∑ (2i−1 )=2 ∑ i−∑ 1
i=1 i=1 i=1
N ( N +1)
=2 −N
2
=N 2 +N −N
=N 2
b.
The easiest way to prove this is by induction. The case N = 1 is trivial. Otherwise,
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
1350 16 90 135 16
P1=&A; A B
0
*P2=*P1/=*P2; 84 84 90 A 84 B 84
CS2321 – Homework 1 Page 2/1
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) / 1438-1439 (1)
Homework 1 (Solutions)
(1)
a.
N N N
∑ (2i−1 )=2 ∑ i−∑ 1
i=1 i=1 i=1
N ( N +1)
=2 −N
2
=N 2 +N −N
=N 2
b.
The easiest way to prove this is by induction. The case N = 1 is trivial. Otherwise,
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
1350 16 90 135 16
P1=&A; A B
0
*P2=*P1/=*P2; 84 84 90 A 84 B 84
CS2321 – Homework 1 Page 2/1