0% found this document useful (0 votes)
16 views41 pages

s3 - Notes

Uploaded by

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

s3 - Notes

Uploaded by

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

Sacred Heart Canossian College

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 electric circuit is a closed path in which electrons move to produce electric


currents.

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.

The nano scale

A microchip the size of your fingernail contains billions of transistors, so


it’s easy to understand just how small the features on a chip need to be.
Chip features are measured in nanometers. A nanometer is one billionth of
a meter, or a millionth of a millimeter.

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:

Salary = hourly pay * total number of working hours + bonus

(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

3. Write down a suitable formula for each of the following queries:

(a) Convert the names of the students to uppercase letter.


(b) Create an email address for each student (e.g. [email protected]).
(c) Find the date that is 1000 days after today.
(d) Find the number of days you have been alive in this world.
(e) Plot the graph y  x 2  2x  1 from x = -6 to x = 6 using 0.1 as the step value.
(f) Generate a random integer between 0 and 9 inclusively. Hence, randomly select a
student from a group of 10 students.

4. Sorting is a process of arranging data records in ascending or descending order. Filtering


is a quick and easy way to find and work with a subset of data in a range. A filtered
range displays only the rows that meet the criteria you specify for a column.

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. The user makes a request for accessing her bank account.


2. The server-side program composes the SQL according to the request and sends the query
to the DBMS.
3. The DBMS retrieves and updates the customer's record and send back the result to the
server-side program.
4. The server-side program presents the result to the user with reference to the interface she
uses.

Example 2: Point-of-sale and stock control system

1 The customer presents the product at checkout counter.


2 The cashier uses a bar code reader to input the product id into the POS terminal
3 The system identifies the product item and retrieves its record from the database.
4 It will show the total price and print out the receipt for the customer.
5 The transaction record will be stored in the database. Besides, the inventory level of the
product will also be deducted.
6 The system will check the sales volume of each product periodically and keep its stock
at an appropriate level by delivering the products from the warehouse to the chain store.

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:

 Searching data records stored in the database


 Sorting the retrieved data records
 Inserting data records into the database
 Deleting data records from the database
 Updating data records stored in the database

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:

Select Class_no, Name


From Students
Where Class = “5F”
Order by Name asc;

The following SQL statement inserts a data record into the table “Students”.

Insert into Students


Values (“s012345”, “Andy Lo”, “4F”, 38);

3
sone aadhetriene duta records
Run multi and malti -
tasking hvironment
in a -
user

Sacred Heart Canossian College


S3 Computer Literacy
Assignment

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.

(c) Find those ‘3D’ students who have borrowed books.


(d) Find those English books that have been borrowed by students.

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

(a) Find the first 10 students in the class ‘3A’.


(b) Find those students with English name ‘Angela’. Arrange the result in ascending
order with reference to their class and class number.
(c) Find those form 3 students with student id that is not started with ‘s21’.

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

Structure of a C++ program


Probably the best way to start learning a programming language is by writing a
program. Here is our first program:

#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

cout << "Output sentence"; // prints Output sentence on screen


cout << 120; // prints number 120 on screen
cout << x; // prints the content of variable x on screen

1
In order to insert a line break in the output, we should use the endl manipulator.

cout << "First sentence." << endl;


cout << "Second sentence." << endl;

Example 2

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int i;

cout << "Please enter an integer: ";


cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << "." << endl;

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:

int x; // declare the integer variable x


cin >> x; // input an integer from the keyboard put it into x

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:

int x; // declare an integer variable x


float y; // declare a decimal number variable y

Here is a summary of the data types commonly used in C++:

Data Types Descriptions


int Integer.
float Decimal number.
char Character.
string A sequence of characters.
bool Boolean value. It can take one of two values: true or false.

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;

cout << "What's your name? ";


getline (cin, x); // get a sentence from the user
cout << "Hi, " << x << endl;
cout << "How are you?" << endl;

system("PAUSE");
return 0;
}

3
Example 4

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
float x, y;
float sum, difference, product, quotient;

cout << "x = ";


cin >> x;
cout << "y = ";
cin >> y;

sum = x + y;
difference = x - y;
product = x * y;
quotient = x / y;

cout << "x + y = " << sum << endl;


cout << "x - y = " << difference << endl;
cout << "x * y = " << product << endl;
cout << "x / y = " << quotient << endl;

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>

using namespace std;

int main()
{
int x, y;
int quotient, remainder;

cout << "x = ";


cin >> x;
cout << "y = ";
cin >> y;

quotient = x / y;
remainder = x % y;

cout << "x / y = " << quotient;


cout << " ... " << remainder << endl;

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;

cout << "Enter an alphabet: ";


cin >> x;

if ((x=='a') || (x=='e') || (x=='i') || (x=='o') || (x=='u'))


cout << "It is a vowel" << endl;
else
cout << "It is a consonant" << endl;

system("PAUSE");
return 0;
}

6
Example 7

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int mark;
char grade;

cout << "Enter your mark: ";


cin >> mark;

if (mark >= 80)


grade = 'A';
else if (mark >= 70)
grade = 'B';
else if (mark >= 60)
grade = 'C';
else if (mark >= 50)
grade = 'D';
else
grade = 'F';

cout << "Your grade is " << grade << endl;

system("PAUSE");
return 0;
}

Sample Output
Enter your mark: 63
Your grade is C

7
Example 8

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int n, k;

cout << "Enter an integer: ";


cin >> n;

k = 1;
while (k <= n)
{
cout << k << ", ";
k = k + 1;
}

cout << endl;

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>

using namespace std;

int main()
{
int n;

cout << "Enter an integer: ";


cin >> n;

while (n >= 0)
{
cout << n << ", ";
n = n - 1;
}

cout << "Fire!" << endl;

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.

A random number generator is a program designed to generate a sequence of numbers


that lack any pattern, i.e. appear random.

srand(time(NULL)) initializes the random number generator


x = rand() generates a random integer and put it into x
http /
/computer shccedu hk / computer
:
~
Example 10
-
.

use name = shcs


password =

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;

cout << "What's your name? ";


________________________;
cout << "What's your age? ";
cin >> age;

cout << "Hi, " << ____________ << endl;


cout << "Next year, you will be ";
cout << ____________ << " years old." << endl;

Sample Output
What's your name? Clara Chan
What's your age? 16
Hi, Clara Chan
Next year, you will be 17 years old.

2. What is the expected output of the following C++ program? (Input = 3)

#include <iostream>
#include <stdlib.h>

using namespace std;

#define PI 3.14159265

12
int main()
{
float r, d, c, a;

cout << "Radius: ";


cin >> r;

d = 2 * r;
c = 2 * PI * r;
a = PI * r * r;

cout << "Diameter = " << d << endl;


cout << "Circumference = " << c << endl;
cout << "Area = " << a << endl;

system("PAUSE");
return 0;
}

3. What is the expected output of the following C++ program? (Input = 64)

int height, feet, inch;

cout << "Height in inches = ";


cin >> height;

feet = height / 12;


inch = height % 12;

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

int hour, wage;

cout << "Number of working hours = ";


cin >> hour;

if (hour <= 40)


wage = 60 * hour;
else if (hour <=65)
wage = 60 * 40 + 90 * (hour - 40);
else
wage = 60 * 40 + 90 * 25 + 120 * (hour - 65);

cout << "The wage is equal to " << wage << endl;

5. What is the expected output of the following C++ program?


(a) Input = 1 (b) Input = 6 (c) Input = 12

float x;
char status;

cout << "Input x: ";


cin >> x;

if ((x >= 3) && (x <= 8))


status = 'A';
else if ((x <= 0) || (x >= 10))
status = 'B';
else
status = 'C';

cout << status << endl;

14
6. What is the expected output of the following C++ program?
(a) Input = 4 (b) Input = 23 (c) Input = 72

int x;

cout << "Enter an integer = ";


cin >> 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;

cout << "Enter x = ";


cin >> 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;

cout << "Enter x = ";


cin >> x;

cout << "Enter y = ";


cin >> y;

while (x <= y+2)


{
cout << x << ", ";
x = x * 2;
}

9. What is the expected output of the following C++ program? (Input = 3, 8)

int x, y, answer;

16
cout << "Enter x = ";
cin >> x;

cout << "Enter y = ";


cin >> y;

answer = 0;

while (x <= y)
{
answer = answer + x;
x = x + 1;
}

cout << answer << endl;

10. What is the expected output of the following C++ program? (Input = 5)

int x, answer;

cout << "Enter x = ";


cin >> x;

answer = 1;

while (x > 0)
{
answer = answer * x;
x = x - 1;
}

cout << answer << endl;

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;
}

14. What is the expected output of the following C++ program?

int number[10], i;

i = 0;
while (i < 10)
{
number[i] = 2 * i + 1;
i = i + 1;
}

cout << "the array elements are " << endl;

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;

cout << "Input 2 array indexes = ";


cin >> i >> j;

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;

cout << endl;


}

21
Sacred Heart Canossian College
S3 Computer Literacy
Further Practice

1. The assessment of computer literacy consists of two parts: Assignment and


Examination. The grade (A, B, C, F) for each student is determined by the
following criteria. Write a C++ program to find the grade with reference to the
number of assignments done and the examination mark.

Grade Number >= 6 Number < 6


Mark >= 30 A B
Mark < 30 C F

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,

3. Write a C++ program to sum up a sequence of integers.

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) >

5. Write a C++ program to update the elements of the following array.

Sample Output
36, 81, 45, 17, 69, 3, 23, 92, 70, 54,

Array index = 10
Array index = 1
Reset value = 111

36, 111, 45, 17, 69, 3, 23, 92, 70, 54,

Array index =

6. Write a C++ program to arrange all one-digit integers in random order.

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>

using namespace std;

int main()
{
int number, mark;
__________ grade;

cout << "____________________";


cin >> number;
cout << "____________________";
cin >> mark;

if (mark >= 30)


{
if (number >= 6)
____________________;
else
____________________;
}
else
{
if (number >= 6)
____________________;
else
____________________;
}

cout << "__________" << __________ << endl;

system("PAUSE");
return 0;
}

24
Question 1 (b)

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int number, mark;
__________ grade;

cout << "____________________";


cin >> number;
cout << "____________________";
cin >> mark;

if ((mark >= 30) && (number >= 6))


____________________;
else if ((mark >= 30) && (number < 6))
____________________;
else if ((mark < 30) && (number >= 6))
____________________;
else
____________________;

cout << "__________" << __________ << endl;

system("PAUSE");
return 0;
}

25
Question 2

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
__________ number, x, y;

cout << "__________";


cin >> x;
cout << "__________";
cin >> y;

if (x % 2 == 0)
number = __________;
else
number = __________;

while (number <= y)


{
cout << number << ", ";
number = __________;
}

cout << endl;

system("PAUSE");
return 0;
}

26
Question 3

#include <iostream>
#include <stdlib.h>

using namespace std;

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;
}

cout << "Sum = " << sum << endl;

system("PAUSE");
return 0;
}

27
Question 4
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

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;

y = ____________________ ; // generate the next random integer


between 1 and 17 inclusively
if ( ____________________ )
outcome = 2;
else if ( ____________________ )
outcome = 1;
else
outcome = 0;

if ( ____________________ )
cout << "You are right ......" << endl << endl;
else
cout << "You are wrong." << endl << endl;

____________________ ;
}

system("PAUSE"); return 0; }

28
Question 5
#include <iostream>

using namespace std;

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;

cout << "Array index = ";


cin >> i;
while ((____________) || (____________))
{
cout << "Array index = ";
cin >> i;
}

cout << "Reset value = ";


cin >> x;
cout << endl;

____________________ ;
}

system("PAUSE");
return 0;
}

29
Question 6
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
int i, x, y, temp;
int number[10];

i = 0; // initialize the array


while ( ____________________ )
{
____________________ ;
____________________ ;
}

srand(time(NULL));

i = 0; // shuffle the array


while (i < 100)
{
x = ____________________ ; // generate random integers x and y
y = ____________________ ; // between 0 and 9 inclusively

temp = number[x];
____________________ ;
number[y] = temp;

i = i + 1;
}

30
cout << "the array elements in random order are " << endl;

i = 0; // output the array


while ( ____________________ )
{
cout << ____________________ << ", ";
____________________ ;
}
cout << endl << endl;

system("PAUSE");
return 0;
}

31

You might also like