s3 - Notes
s3 - Notes
S3 Computer Literacy
Microchip
Introduction
Computer systems are constructed of digital electronics. That means that their
electronic circuits can exist in only one of two states: ON or OFF.
These patterns of ON and OFF stored inside the computer are used to encode binary
numbers 1 and 0 respectively. Through binary arithmetic, computers can perform
addition, subtraction, multiplication, division, and do many other things to numbers.
Electronic circuit
An electronic circuit is a structure that directs and controls electric current to perform
various functions including signal amplification, computation, and data transfer. It
comprises several different components such as resistors, transistors, capacitors,
inductors, and diodes.
Logic Gates
Logic gates are the basic building blocks of any digital system. It is an electronic
circuit having one or more than one input and only one output. The relationship
between the inputs and the output is based on a certain logic. Based on this, logic
gates are named as AND gate, OR gate, NOT gate etc.
1
Microchips – the foundation of the digital world
It’s hard to imagine a world without microchips. They’re at the heart of the
devices that we use to work, travel, stay fit and entertain ourselves – from cars
to smartphones and from MRI scanners to industrial robots and data centers.
What’s a microchip?
A microchip is a set of electronic circuits (logic gates) on a small flat piece of silicon.
On the chip, transistors act as miniature electrical switches that can turn a current on
or off. The pattern of tiny switches is created on the silicon wafer by adding and
removing materials to form a multilayered latticework of interconnected shapes.
Digital gold
Silicon is the material of choice in the chip industry. Unlike the metals normally used
to conduct electrical currents, silicon is a ‘semiconductor’, meaning that its
conductive properties can be increased by mixing it with other materials such as
phosphorus or boron. This makes it possible to turn an electrical current on or off.
Flavors of chips
Logic chips are the ‘brains’ of electronic devices – they process information to
complete a task. Among Logic chips, CPUs are the ‘original’ chips, first designed in
the 1960s. But there are also processors with specific functionality in mind, such as
GPUs (which are optimized for visual display) and NPUs (designed for deep and
machine learning applications).
Memory chips store information. There are two types of Memory: volatile and non-
volatile. Volatile Memory chips, such as DRAM, are the ‘working memory’ chips that
save data only while the device’s power is turned on. Non-volatile Memory chips
such as NAND Flash save data even after the device is turned off. For example,
DRAM helps to run programs on your device, whereas NAND stores your photos.
Whereas DRAM is fast, NAND is slow to read and write data.
2
Logic Gates
Logic gates are the basic building blocks of any digital system. It is an electronic
circuit having one or more than one input and only one output. The relationship
between the inputs and the output is based on a certain logic. Based on this, logic
gates are named as AND gate, OR gate, NOT gate etc.
AND Gate
A circuit which performs an AND operation is shown in figure. It has n input (n >= 2)
and one output. (Y = A and B)
Truth Table
Inputs Output Logic Diagram
A B Y
0 0 0 A
0 1 0 Y
B
1 0 0
1 1 1
OR Gate
A circuit which performs an OR operation is shown in figure. It has n input (n >= 2)
and one output. (Y = A or B)
Truth Table
Inputs Output Logic Diagram
A B Y
0 0 0 A
Y
0 1 1 B
1 0 1
1 1 1
NOT Gate
NOT gate is also known as Inverter. It has one input A and one output Y. (Y = not A)
Truth Table
Inputs Output Logic Diagram
A Y
0 1
A Y
1 0
3
Construct a truth table for each of the following digital circuits.
Truth Table
Inputs Output
A B Y
0 0 A
Y
B
否
0 1
1 0
1 1
Truth Table
Inputs Output
A
0
B
0
C
0
Y
!
A
1
:
B
0 0 1
0 1 0 Y
0 1 1
1 0 0 C
1 0 1
1 1 0 1
1 1 1 1
Truth Table
Inputs Output
A
A B Y
0
0 0
B Y
:
0 1
1 0
1 1
4
。
Sacred Heart Canossian College
S3 Computer Literacy
Spreadsheet II
1. The following figure is a spreadsheet that stores the working hours of a group of workers.
(a) Enter a formula in the cell G6 to find the total number of working hours in a week
for Jane Au. (i.e. the sum of working hours from Monday to Friday)
(b) If the total number of working hours is greater than or equal to the number stored in
the cell F2, then a bonus of amount equal to the value stored in the cell B2 will be
given. Enter a formula in the cell H6 to check whether Jane Au will get the bonus.
(c) Enter a formula in the cell I6 to calculate the salary of Jane Au. The salary can be
found by using the following formula:
(d) Enter formulae in the cells J6, B15, B16, B17 and D19 to calculate the
corresponding statistics.
2. With reference to figure 1, create the mark sheet in figure 2 by setting up suitable
formulae in Excel.
1
Figure 1
Figure 2
5. Creating a set of letters, envelopes, or labels would take hours. That's where mail merge
comes in. Using mail merge, all you have to do is create one document that contains the
information that is the same in each version. Then you just add some placeholders for
the information that is unique to each version. Word takes care of the rest.
(a) Choose the main document that you want to merge information into.
(b) Connect to a data file (e.g. spreadsheet, database files) and select records.
(c) Add fields to the main document and preview the merge results.
2
Sacred Heart Canossian College
S3 Computer Literacy
Database System
Introduction
A database management system (DBMS) is a collection of interrelated data (Database) and a
set of programs to access those data. The collection of data, usually referred to as the
database, contains information relevant to an enterprise. The primary goal of a DBMS is to
provide a way to store and retrieve database information that is both convenient and efficient
Accessing databases forms an essential part of almost everyone’s life today. Here are two
examples:
Example 1: e-Banking
SQL Request
Database Server-side User interfaces:
Management Programs ATM, Web,
System Data HTML, Phone etc
Records text, voice
1
Structured Query Language
A database system provides a data definition language to specify the database structure and a
data manipulation language to express database queries and updates. In practice, the data
definition and data manipulation languages are not two separate languages; instead they
simply form parts of a single database languages, such as the SQL language.
To communicate with a DBMS, we use the SQL (Structured Query Language). SQL is
today’s standard database language, and all major database systems understand it.
Data Definition
Here is the Data Definition of the school library database files generated by SQL:
Students
Field Name Field Type Field Width Index
Student_id Character 7 Primary Key
Name Character 30
Class Character 2 (Class, Class_no) Unique
Class_no Integer
Books
Field Name Field Type Field Width Index
ISBN Character 12 Primary Key
Title Character 50
Subject Character 20
First_published Date/time
Loans
Field Name Field Type Field Width Index
Loan_no Character 10 Primary Key
Student_id Character 7 Foreign Key with reference to the
same field in the table “Students”
ISBN Character 12 Foreign Key with reference to the
same field in the table “Books”
Date_of_return Date/time
In database tables, each field describes some piece of data that each record in the table has. In
the table “Students”, each row in the table represents a single student record. For example,
(“s001234”, “Phil Li”, “5F”, 1)
2
Primary key is a field that can uniquely identify a particular record in a table. So, we can’t
find more than one student who gets the same id. Also, the ISBN of each book is also unique.
Foreign key is a field that establishes relationship between tables. It is used to ensure
referential integrity in a database. In each loan record, we can find the student who has
borrowed a particular book with reference to the links indicated by the foreign keys
Data Manipulation
Data Manipulation involves the following processes:
The following SQL statement retrieves the class numbers and names of all the students from
the class “5F” and then sorts them in alphabetical order:
The following SQL statement inserts a data record into the table “Students”.
3
sone aadhetriene duta records
Run multi and malti -
tasking hvironment
in a -
user
1. With reference to the school library database defined on p2, write a SQL statement for
each of the following queries:
(a) Find all ‘3C’ students and arrange the records in ascending order according to the
names.
(b) Find all the Science books and arrange the records in descending order according to
the book titles.
2. With reference to the following data definitions, write a SQL statement for each of the
following queries:
Students Marks
Field Name Field Type Field Width Field Name Field Type Field Width
id character 7 id character 7
class character 2 subject character 8
no integer -- mark integer --
name character 33
(d) Find the marks of the student ‘Tsang Hoi Yan’ in all subjects.
(e) Find the marks of ‘3E’ students in English Language.
4
Sacred Heart Canossian College
S3 Computer Literacy
Basic Programming Concept
Introduction
C is a programming language that is used to create computer programs (e.g. operating
systems) running on personal computers, mobile devices, servers and embedded systems.
It is the most widely used programming language of all time. C is the grandfather of C++,
Swift and Java, which are commonly used to write Windows application programs,
iPhone apps and Android apps respectively.
#include <iostream>
#include <stdlib.h>
Program header
using namespace std;
int main()
{
cout << "Hello World!" << endl;
system("PAUSE");
Program footer
return 0;
}
Sample Output
Hello World!
The standard output of a program is the screen, and the C++ stream object defined to
access it is cout. Here are some examples
1
In order to insert a line break in the output, we should use the endl manipulator.
Example 2
#include <iostream>
#include <stdlib.h>
int main()
{
int i;
system("PAUSE");
return 0;
}
Sample Output
Please enter an integer value: 702
The value you entered is 702 and its double is 1404.
The standard input device is the keyboard, and the C++ stream object defined to
access it is cin. The operator (>>) must be followed by the variable that will store the
data. For example:
2
Variables and Data Types
When programming, we store the variables in our computer's memory. But, the
computer has to know what kind of data we want to store in them. Therefore, before
using a variable in C++, we must first declare it by specifying which data type it
belongs to. For example:
Example 3
#include <iostream>
#include <stdlib.h>
#include <string>
Sample Output
What's your name? Sarah Hui
using namespace std;
Hi, Sarah Hui
How are you?
int main()
{
string x;
system("PAUSE");
return 0;
}
3
Example 4
#include <iostream>
#include <stdlib.h>
int main()
{
float x, y;
float sum, difference, product, quotient;
sum = x + y;
difference = x - y;
product = x * y;
quotient = x / y;
system("PAUSE");
return 0;
}
Sample Output
x = 1.2
y=3
x + y = 4.2
x - y = -1.8
x * y = 3.6
x / y = 0.4
4
一
Example 5
#include <iostream>
#include <stdlib.h>
int main()
{
int x, y;
int quotient, remainder;
quotient = x / y;
remainder = x % y;
system("PAUSE");
return 0;
}
Sample Output
x=7
y=3
x / y = 2 ... 1
5
Comparative and Logical Operators
== Equal to ! Not
!= Not equal to && And
> Greater than || Or
>= Greater than or equal to
< Less than
<= Less than or equal to
Control Structures
A program is usually not limited to a linear sequence of instructions. During its
process it may branch, repeat code or take decisions. For that purpose, C++ provides
control structures that serve to specify what has to be done by our program, when and
under which circumstances.
Example 6
#include <iostream>
#include <stdlib.h>
Sample Output
using namespace std;
Enter an alphabet: k
It is a consonant
int main()
{
char x;
system("PAUSE");
return 0;
}
6
Example 7
#include <iostream>
#include <stdlib.h>
int main()
{
int mark;
char grade;
system("PAUSE");
return 0;
}
Sample Output
Enter your mark: 63
Your grade is C
7
Example 8
#include <iostream>
#include <stdlib.h>
int main()
{
int n, k;
k = 1;
while (k <= n)
{
cout << k << ", ";
k = k + 1;
}
system("PAUSE");
return 0;
}
Sample Output
Enter an integer: 8
1, 2, 3, 4, 5, 6, 7, 8,
8
Example 9
#include <iostream>
#include <stdlib.h>
int main()
{
int n;
while (n >= 0)
{
cout << n << ", ";
n = n - 1;
}
system("PAUSE");
return 0;
}
Sample Output
Enter an integer: 9
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, Fire!
9
Random Number Generaton
Random numbers are numbers that occur in a sequence such that two conditions are
met: (1) the values are uniformly distributed over a defined interval or set, and (2) it is
impossible to predict future values based on past or present ones.
howaneyon
#include <iostream>
#include <stdlib.h>
Sample Output
#include <time.h>
12143 3
24063 3
using namespace std;
31945 5
6317 7
int main()
13612 2
{
17500 0
int i, x, y;
2356 6
16931 1
srand(time(NULL));
9439 9
10308 8
i = 0;
24248 8
while (i < 20)
28454 4
{
362 2
x = rand();
8995 5
y = x % 10;
2782 2
cout << x << "\t" << y << endl;
10662 2
i = i + 1;
21842 2
}
23670 0
515 5
system("PAUSE");
5153 3
return 0;
}
10
Arrays
An array is a series of elements of the same type placed in contiguous memory
locations that can be individually referenced by adding an index to a unique identifier.
Instead of declaring individual variables, such as number0, number1, ..., and number7,
you declare one array variable such as number and use number[0], number[1], and ...,
number[7] to represent individual variables.
int number[8] = {23, 96, 37, 61, 80, 45, 72, 19};
number 23 96 37 61 80 45 72 19
0 1 2 3 4 5 6 7
Example 11
int i;
int number[10]={36, 81, 45, 17, 69, 3, 23, 92, 70, 54};
i = 0; Sample Output
while (i < 10) 36, 81, 45, 17, 69, 3, 23, 92, 70, 54,
{
cout << number[i] << ", "; Array index = 1
i = i + 1; Content = 81
}
cout << endl << endl; Array index = 6
Content = 23
while (1)
{ Array index =
cout << "Array index = ";
cin >> i;
if ((i < 0) || (i >= 10))
cout << "Out of range" << endl << endl;
else
cout << "Content = " << number[i] << endl << endl;
}
11
Sacred Heart Canossian College
S3 Computer Literacy
Class Exercise
1. Fill in the blanks of the following C++ program so that it will generate the
following sample output.
____________ name;
int age;
Sample Output
What's your name? Clara Chan
What's your age? 16
Hi, Clara Chan
Next year, you will be 17 years old.
#include <iostream>
#include <stdlib.h>
#define PI 3.14159265
12
int main()
{
float r, d, c, a;
d = 2 * r;
c = 2 * PI * r;
a = PI * r * r;
system("PAUSE");
return 0;
}
3. What is the expected output of the following C++ program? (Input = 64)
cout << feet << " feet and " << inch << " inches" << endl;
13
4. What is the expected output of the following C++ program?
(a) Input = 45 (b) Input = 70 (c) Input = 30
cout << "The wage is equal to " << wage << endl;
float x;
char status;
14
6. What is the expected output of the following C++ program?
(a) Input = 4 (b) Input = 23 (c) Input = 72
int x;
if (x % 3 == 0)
{
if (x % 2 == 0)
cout << x + 1;
else
cout << x + 2;
}
else if (x % 3 == 1)
{
if (x % 2 == 0)
cout << x + 3;
else
cout << x + 4;
}
else
{
if (x % 2 == 0)
cout << x + 5;
else
cout << x + 6;
}
15
7. What is the expected output of the following C++ program? (Input = 10)
int x;
while (x >= 0)
{
cout << x << ", ";
x = x - 3;
}
8. What is the expected output of the following C++ program? (Input = 3, 23)
int x, y;
int x, y, answer;
16
cout << "Enter x = ";
cin >> x;
answer = 0;
while (x <= y)
{
answer = answer + x;
x = x + 1;
}
10. What is the expected output of the following C++ program? (Input = 5)
int x, answer;
answer = 1;
while (x > 0)
{
answer = answer * x;
x = x - 1;
}
17
11. Fill in the blanks of the program so that it will generate the following sample
output.
int x, y;
x = 1;
while ( (a) )
{
y = 1;
while ( (b) )
{
cout << x*y << "\t";
y= (c) ;
}
cout << endl;
x= (d) ;
}
Sample Output
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
18
12. Fill in the blanks of the program so that it will generate the following sample
output.
int x, y;
x = 1;
while ( (a) )
{
y = 1;
while ( (b) )
{
cout << (c) << "\t";
y = y + 1;
}
cout << (d) ;
x = x + 1;
}
Sample Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
19
13. What is the expected output of the following C++ program if the random
numbers generated are 18148, 31278, 17852, 21951
int i, x, y;
srand(time(NULL));
i = 0;
while (i < 4)
{
x = rand();
y = x % 6 + 1;
cout << x << "\t" << y << endl;
i = i + 1;
}
int number[10], i;
i = 0;
while (i < 10)
{
number[i] = 2 * i + 1;
i = i + 1;
}
i = 0;
while (i < 10)
{
cout << number[i] << ", ";
i = i + 1;
}
cout << endl << endl;
20
15. What is the expected output of the following C++ program if the inputs are 1 6
and 9 1
int i, j, temp;
int number[10]={36, 81, 45, 17, 69, 3, 23, 92, 70, 54};
while (1)
{
i = 0;
while (i < 10)
{
cout << number[i] << ", ";
i = i + 1;
}
cout << endl << endl;
if ((i >= 0) && (i < 10) && (j >= 0) && (j < 10))
{
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
else
cout << "Out of range" << endl;
21
Sacred Heart Canossian College
S3 Computer Literacy
Further Practice
Sample Output
Number of assignment done = 8
Examination mark = 20
Grade = C
2. Write a C++ program to output all the odd integers between x and y inclusively.
You may assume x and y are positive integers and x is less than y.
Sample Output
x=6
y = 19
7, 9, 11, 13, 15, 17, 19,
Sample Output
Number 1 = 15
Continue (yes=1/no=0)? 1
Number 2 = 22
Continue (yes=1/no=0)? 1
Number 3 = 8
Continue (yes=1/no=0)? 0
Sum = 45
22
4. Write the following number guessing game using C++ programming language.
Sample Output
Random number = 8
More (1) or Less (0) > 1
You are wrong.
Random number = 5
More (1) or Less (0) > 1
You are right ......
Random number = 10
More (1) or Less (0) >
Sample Output
36, 81, 45, 17, 69, 3, 23, 92, 70, 54,
Array index = 10
Array index = 1
Reset value = 111
Array index =
Sample Output
The array elements in random order are
3, 1, 0, 5, 4, 9, 7, 6, 8, 2,
23
Sacred Heart Canossian College
S3 Computer Literacy
Further Practice
Question 1 (a)
#include <iostream>
#include <stdlib.h>
int main()
{
int number, mark;
__________ grade;
system("PAUSE");
return 0;
}
24
Question 1 (b)
#include <iostream>
#include <stdlib.h>
int main()
{
int number, mark;
__________ grade;
system("PAUSE");
return 0;
}
25
Question 2
#include <iostream>
#include <stdlib.h>
int main()
{
__________ number, x, y;
if (x % 2 == 0)
number = __________;
else
number = __________;
system("PAUSE");
return 0;
}
26
Question 3
#include <iostream>
#include <stdlib.h>
int main()
{
float number, sum=0;
int c=1, n=0;
while (__________)
{
____________________;
cout << "Number " << n << " = ";
cin >> number;
____________________;
cout << "Continue (yes=1/no=0)? ";
cin >> c;
}
system("PAUSE");
return 0;
}
27
Question 4
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main()
{
int x, y;
int outcome, guess;
srand(time(NULL));
x = ____________________ ; // generate the first random integer
between 1 and 17 inclusively
while (1)
{
cout << "Random number = " << x << endl;
cout << "More (1) or Less (0) > ";
cin >> guess;
if ( ____________________ )
cout << "You are right ......" << endl << endl;
else
cout << "You are wrong." << endl << endl;
____________________ ;
}
system("PAUSE"); return 0; }
28
Question 5
#include <iostream>
int main()
{
int i, x;
int number[10]={36, 81, 45, 17, 69, 3, 23, 92, 70, 54};
while (1)
{
i = 0;
while ( ____________________ )
{
cout << ____________________ << ", ";
____________________ ;
}
cout << endl << endl;
____________________ ;
}
system("PAUSE");
return 0;
}
29
Question 6
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, x, y, temp;
int number[10];
srand(time(NULL));
temp = number[x];
____________________ ;
number[y] = temp;
i = i + 1;
}
30
cout << "the array elements in random order are " << endl;
system("PAUSE");
return 0;
}
31