You are on page 1of 18

ICT002 (PRELIM)

WEEK 1
What is programming?
 Is the process of developing and implementing various sets of instructions to enable a computer to do a
certain task.
 
 Programming is the process of creating a set of instructions that tell a computer how to perform a task.
Programming can be done using a variety of computer programming languages, such as JavaScript,
Python, and C++.
 Computers are not actually smart. It cannot make decisions or do operations by itself.
 Kung wala tayo wala si computer
 Kailangan ng program para mag karoon siya ng functions at magka perform ng task
 It needs programs for it to know what task should it perform and hot it should be done.
 Work > coding > idea>completing task
 Programming concept
Diagram

Programmer > Programmer language > computer program > computer task
The user > programming language> computer (nakaisip ng idea) to perform task.>
 
Purpose of programming
 The main objective of programming is to solve a problem.
 Computer can solve a lot of problems in a matter of seconds.
 Computer can improve our daily lives. From calculating large amounts of data, from sharing and
retrieving information in a secure place and in communication with our loved ones in faraway place.
 Computer is just a tool that can be used to implement a plan for solving a problem.
 Computer Program= Is a set of instructions for a computer. These instructions describes the steps that
the computer must follow to implement a plan (para mas bilis ang pang execute ni computer) ex: video
games.
 Programming- ginagawa natin sa computer program
 Algorithm= A detailed plan for solving a problem.
 Detailed plan (step by step procedure)
o A process or set of rules to be followed in calculations or other problem-solving
operations, especially by a computer.
o Set rules ( talk about the process how you solve the problem)
o It's set of instructions designed to perform a specific task.
Example
Algorithm vs Computer program
 
Algorithm detail plan vs Computer program set of instruction
 
Problem: Write a solution on how to find the average grades of a class of 10 seconds.
 
Algorithm- pinaguusapan kung paano ma solve setp by step
1. Identify the grades of the 10 students
2. Add all the grades
3. Divide the total of the grades by 10
4. Round off all the answer by 2 decimal places.
5. Show the result
 
Computer program- gumagamit ng syntax
 
Step 1=cout<<Enter grades: ";
Cin>>grades;
Step 2=sum = sum + grades;
Step 3=ave (average) = sum (divided) / 10;
Step 4=4 use data type that round off decimal numbers
Step 5=5 cout<<"the class average is:"<<ave;
 
Algorithm Development Process
This is a key in solving a problem.
 
Five major steps of Algorithm development process
1. Obtain a description of the problem ( gumawa ng description ng problem)
2. Analyze the problem
3. Develop a high-level algorithm
4. Refine the algorithm by adding more detail.
5. Review the algorithm
 
Example: Pick and Plant problem. One must apply algorithm development process to fully understand the concept.
 
Start
---------------------
Finish
------------------------
 
The process:
 
problem statement
is a short, clear, explanation oof the issue.
 
Problem analysis
Investigates a situation stigates a situation/ problem in order to allow the programmer to understand more fully
the problem.
 
High-level algorithm
It shows the major steps that need to be followed to solve a problem.
 
Programming concept (additional)
 
Input, process then output- calculations
(Researched)
Input- Input ng number at data, compute, calculation of numbers (example: 2,3 (computation)
Process- (example :Addition)
Output- kung ano kinalabasan ng sagot or computation (2 + 3 = 5)

WEEK 2

Programming Language - is a standardized communication technique for expressing instructions to a computer.


Examples: C++, C, C#, Python, Java

Categories of Programming Language:


High-Level Programming Language
uses a more natural and human-readable syntax which makes it easier for humans to understand and write
Low-level Assembly Language
user writes code which is almost identical to the one computers understand

What is C++?
- C++ is a general-purpose programming language invented in the early 1980s by Bjarne Stroustrup at Bell Labs.
- The purpose of C++ is to precisely define a series of operations that a computer can perform to accomplish a task.
Most of these operations involve manipulating numbers and text, but anything that the computer can physically
do can be programmed in C++.

Why Use C++?


C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be
reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
C++ is fun and easy to learn!
As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice versa

Getting Started with C++


- To start your program development with C++, first you need to install two kinds of software on your computer:
Text Editor
-This will be used to type the code of your program.
-Example: Notepad, CodeBlocks.
C++ Compiler
-Compilers are used to compile your source code and translate it into a machine code, a code that a
computer can actually understand.

The various parts of the given program consists of the ff:

The header <iostream> contains everything we needed for handling input and output.

The line using namespace std; tells the compiler to use the standard namespace.
Namespaces are used to organize code into logical groups.

The line int main() is the main function where program execution begins.

The next line cout << "This is my first C++ program."; causes the message "This is my first C++ program" to be
displayed on the screen.

The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process.

Main
Program execution begins with the main function, int main().

Curly brackets { } indicate the beginning and end of a function, which can also be called the function's body. The
information inside the brackets indicates what the function does when executed.

In most program environments, the standard default output destination is the screen. In C++, cout is the stream object
used to access it.
cout is used in combination with the insertion operator. Write the insertion operator as << to insert the data that comes
after it into the stream that comes before.

Statements
A block is a set of logically connected statements, surrounded by opening and closing curly braces

You can have multiple statements on a single line, as long as you remember to end each statement with a semicolon.
Failing to do so will result in an error.

Components to build C++ programs.


1. Integrated Development Environment (IDE): Provides tools for writing source code. Any text editor can be used
as an IDE.
2. Compiler: Compiles source code into the final executable program. There are a number of C++ compilers
available.
The most frequently used and free available compiler is the GNU C/C++ compiler.

New Line

The cout object does not insert a line break at the end of the output.
One way to print two lines is to use the endl manipulator, which will put in a line break.
The endl manipulator moves down to a new line to print the second text.
The new line character \n can be used as an alternative to endl.
The backslash (\) is called an escape character, and indicates a "special" character

Comments

Comments are explanatory statements that you can include in the C++ code to explain what the code is doing.
The compiler ignores everything that appears in the comment, so none of that information shows in the result.

A comment beginning with two slashes (//) is called a single-line comment. The slashes tell the compiler to
ignore everything that follows, until the end of the line.

Multi-Line Comments

Comments that require multiple lines begin with /* and end with */


You can place them on the same line or insert one or more lines between them.

Using Comments
Comments can be written anywhere, and can be repeated any number of times throughout the code.
Within a comment marked with /* and */, // characters have no special meaning, and vice versa. This allows
you to "nest" one comment type within the other.

Variables

are nothing but reserved memory locations to store values.


a variable reserves a memory location, or a space in memory for storing values. The compiler requires that
you provide a data type for each variable you declare.
C++ offer a rich assortment of built-in as well as user defined data types.

Integer, a built-in type, represents a whole number value. Define integer using the keyword int.
C++ requires that you specify the type and the identifier for each variable defined.
An identifier is a name for a variable, function, class, module, or any other user-defined item. An identifier
starts with a letter (A-Z or a-z) or an underscore (_), followed by additional letters, underscores, and digit
The C++ programming language is case-sensitive, so myVariable and myvariable are two different identifiers.

all variables with a name and a data type before using them in a program. In cases in which you have multiple
variables of the same type, it's possible to define them in one declaration, separating them with commas

A variable can be assigned a value, and can be used to perform operations.


For example, we can create an additional variable called sum, and add two variables together.

Use the + operator to add two numbers.


User Input

To enable the user to input a value, use cin in combination with the extraction operator (>>). The variable
containing the extracted data follows the operator.
The following example shows how to accept user input and store it in the num variable:
You can assign a value to a variable only in a declared data type.

WEEK 3

Data types- Refer to the kinds of data that variable can assume, hold or take on in a programming language and for
which operations are automatically provided.
Boolean (true or false)- bool
Character- char
Integer (whole numbers)-int
Floating point-float
Double floating point- double
Texts- string
Legal expression- both operands of the + operator are integers
Illegal expression- The + operator is not defined for integer and string

Integers or int - integer can only hold whole numbers; numbers with decimal points will be converted into whole
numbers.
int can only contain values ranging from -2,147,483,648 to 2,147,483,647… but it has different types
Int data type and range
Short - -32768 to 32767
Long- -2,147,483,648 to 2,147,483,647
long long- -18446744073709551616 to 18446744073709551616
Floating point type variable can hold a real number, such as 420.0, -3.33, or 0.03325.
The words floating point refer to the fact that a varying number of digits can appear before and after the decimal point.
You could say that the decimal has the ability to "float".

There are three different floating point data types: float, double, and long double.

In most modern architectures, a float is 4 bytes, a double is 8, and a long double can be equivalent to a double (8
bytes), or 16 bytes.
String
- A string data type represents a series of characters.
- It must have its value enclosed in double-quotes
Character or char
- A character data type represents a single character.
- It must have its value enclosed in single-quotes.

Boolean or bool
A boolean data type represents two states: true and false.
- It can also accept values 0 and 1; wherein 0 can represent false, and 1 as true.

Cout or standard output stream


The cout object is said to be "connected to" the standard output device, which usually is the display screen.
- The cout is used in conjunction with the stream insertion operator, which is written as << which are two
less than signs

WEEK 4
Flowchart
are a visual outlining tool. They can be used to represent an algorithm.
Algorithm
The sequence of steps necessary to solve any problem.

Terminal Symbol
The terminal symbol indicates the starting or stopping point in the logic. Every flowchart should begin and end with
terminal symbol.
Input/output Symbol
The input/output symbol represents an input or output process in an algorithm, such as reading input or writing output.
Process Symbol
The process symbol represents any single process in an algorithm, such as assigning a value or performing a
calculation. The flow of control is sequential.
Predefined process symbol
The predefined process symbol represents a module in an algorithm – that is, a predefined process that has its own
flowchart.
Decision Symbol
The decision symbol represents a decision in the logic involving the comparison of two values. Alternative path are
followed, depending on whether the decision symbol is true or false.

Flowlines
Flowlines connect various symbols in a flowchart, and contain an arrowhead only when the flow of control is not from
top to bottom or left to right.
Arithmetic Operators
C++ supports these arithmetic operators

Operator Use Description


s

+ op1 + op2 Adds op1 and op2


- op1 - op2 Subtracts op1 and
op2
* op1 * op2 Multiplies op1 and
op2
/ op1 / op2 Divides op1 from
op2
% op1 % Computes the
op2 remainder
of dividing op1 by
op2

Increment and Decrement Operator


Increment Operator
The increment operator is used to increase an integer's value by one, and is a commonly used C++ operator
The increment operator is used to increase an integer's value by one
Increment Operator
The increment operator has two forms, prefix and postfix.

Prefix increments the value, and then proceeds with the expression.


Postfix evaluates the expression and then performs the incrementing.
Decrement Operator
The decrement operator (--) works in much the same way as the increment operator, but instead of increasing the
value, it decreases it by one.

The decrement operator (--) works in much the same way as the increment operator

Assignment Operators
The simple assignment operator (=) assigns the right side to the left side.

C++ provides shorthand operators that have the capability of performing an operation and an assignment at the same
time.
Programming- This is the process of making various sets of instructions to enable a computer to do certain task.
Computer programmer > programming language > computer program > task
Solve a problem- The main purpose of programming
Computer program- this is a set of instruction for computer
Algorithm- this is called a detailed plan for solving a problem
Computer cannot make decisions or do operations by itself
FALSE -A computer program is usually written by computer in a markup language
First step in ADP is problem statement
Compiler- software used to compile your source code and translate it into a machine code, a code that a computer can
actually understand.
Namespace- used to organize code into logical groups
Console window- will open and display program output
C++ is an object oriented programming language which gives a clear structure to programs and allows a code to be
reused, lowering development costs.
Single line or single line comment- A comment beginning with two slashers //
High level programming language- categories of programming language uses a more natural and human-readable
syntax which make it easier for humans to understand and write.
<< -insertion operator as << to insert a data that comes after into the stream that comes before.
Cout – is the stream object used to access it.
The line return 0 terminates main () function and causes it to return the value 0 to the calling process.
The compiler requires that you provide a data type for each variable you declare.
Variables are nothing but reserved memory locations to store values.
Curly brackets {} indicate the beginning and end of the function, which can also be called the function body.
Programming language is a standardized communication technique for expressing instruction to a computer.
Define integer using the keyword int
Low-level assembly language- uses writes code which is almost identical to the one computer understand.
New line character \n can be used as alternative to endl.
Block is a set of logically connected statements, surrounded by opening and closing curly braces
Comments that require multiple lines with /* and end with /*
C++ is a general purposes programming language invented in the early 1980s by Bjarne stroustrup at bell labs.
The line using name space std tells the compiler to use the standard namespace.
Backslash / is called an escape character and indicated a special character.
Comments are explanatory statements that you can include in the c++ code to explain what the code is doing
Text editor software used to type the code of your program
C++ is one of the most popular programming languages.
The purpose of c++ is to precisely define a series of operation that computer can perform to accomplish a task.
You can have a multiple statement on a single line as long as you remember to end each statement with a semicolon.
Failing to do so will result in an error.
The line int main () is the main function where program execution begins
Ant text editor can be used as IDE
The most frequently used and free available compiler is the GNU C++ compiler.
<iostream> parts of the given program that contains everything we needed for handling input and output.

DECISION CONTROL STRUCTURE

Relational Operators

Operator Use Description


0 FALSE

1 TRUE
> op1 > op2 op1 is greater than op2

>= op1 >= op2 op1 Is greater than OR


equal to op2

< op1 < op2 op1 is less than op2

<= op1 <= op2 op1 is less than or


equal to op2

== op1 == op2 op1 and op2 are equal

!== op1 != op2 op1 and op2 are not


equal
Compound assignment
(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

Compound assignment operators modify the current value of a variable by performing an operation on it.
They are equivalent to assigning the result of an operation to the first operand:

expression equivalent to...

y += x; y = y + x;

x -= 5; x = x - 5;

x /= y; x = x / y;

price *= units + 1; price = price * (units+1);

&& will evaluate to exp1 and immediately return a


false value if exp1 is false.

^ is true if and only if only one operand is true and


the other is false.

|| will evaluate to exp1 and immediately return a true


value if exp1 is true.

Logical Not ! takes in one argument, wherein that argument can


be an expression variable or constant.

Conditional Operator ?: is a ternary operator. It takes in 3 arguments that


together form a conditional expression.

&& TRUE = Both True FALSE=If there is a false

|| TRUE= When there is a true


expression

^ TRUE =Different Expression FALSE=Same expression

?! True =False False=True


CONTROL STRUCTURE

Decision Control Structures - statements that allows us to select and execute specific blocks of code while
skipping other sections.

Single-Selection Structure - It selects or ignores a single action.

if-selection structure - Specifies that a statement or block of codes will be executed if and only if a
certain boolean statement is true.

Double-Selection Structure - It selects between 2 different actions.

if-else structure – Is used when we want to execute a certain statement if a condition is true and a
different statement if the condition is false.

if-else-if structure - The statement in the else-clause of an if-else block can be another if-else
structure. This cascading of structure allows us to make more complex selections.

Nested-If - The inclusion of one or more if statements within an existing if statement.

Multiple-Selection Structure - It selects the action to perform from many different actions.

Switch Statements - Switch construct allows branching on multiple outcomes.

switch expression – is an integer or character expression. It identifies the start of the switch statement.
The expression in parentheses ff. this word is evaluated and the result of the
expression compared to various contained within the compound statement.

case – are unique integer or character constant. It is used to identify or label individual
values that are compared to the value of the switch expression.

break – indicates the end of the statement in the switch statement.

default – it operates the same as the last else in an if-else chain.

PRE FINALE LESSONS:

REPETITION CONTROL STRUCTURE

Repetition Control Structures

allows us to execute specific blocks of code a number of times.

*while loop

is a statement or block of statements that is repeated as long as some condition is satisfied.

Form:

while(boolean_expressions){

statement 1;

statement 2;
}

*do while loop

- the statement inside the do while loop are executed several times as long as the condition is satisfied.

Form:

do{

statement 1;

statement 2;

} while(boolean_expression);

*for loop

allows execution of the same code a number of times.

Form:

for(InitializationExpression; LoopCondition; StepExpression){

statement 1;

statement 2;

where:

InitializationExpression – Initializes the loop variable.

Loop Condition- compares the loop variable to some limit value.

Step Expression – updates the loop variable.

FINALE LESSONS :

Array

– stores multiple data items of the same data type, in a contiguous block of memory, divided into a number of slots.

Declaring Arrays

Arrays must be declared like all variables . When declaring an array, you list the data type, array name, number of items
and square brackets

An array is used to store a collection of data, but it may be useful to think of an array as a collection of variables that are all of
the same type.
Instead of declaring multiple variables and storing individual values, you can declare a single array to store all the values.
When declaring an array, specify its element types, as well as the number of elements it will hold.
For example:

int a[5];

In the example above, variable a was declared as an array of five integer values [specified in square brackets].
You can initialize the array by specifying the values it holds:

int b[5] = {11, 45, 62, 70, 88};

The values are provided in a comma separated list, enclosed in {curly braces}.

The number of values between braces { } must not exceed the number of the elements declared within the square brackets [ ].
Array Name – group name.

Index Number/Subscript - assigned to each member of the array allowing the program and the programmer to access
individual values when necessary.

Array Element – item or value inside the array.

Functions

A function is a group of statements that perform a particular task.


You may define your own functions in C++.

Using functions can have many advantages, including the following:


- You can reuse the code within a function.
- You can easily test individual functions.
- If it's necessary to make any code modifications, you can make modifications within a single function, without altering the
program structure.
- You can use the same function for different inputs.

Every valid C++ program has at least one function - the main() function.

The Return Type


The main function takes the following general form:

A function's return type is declared before its name. In the example above, the return type is int, which indicates that the function
returns an integer value.
Occasionally, a function will perform the desired operations without returning a value. Such functions are defined with the
keyword void.

void is a basic data type that defines a valueless state.

Defining a Function

As an example, let's define a function that does not return a value, and just prints a line of text to the screen.

void printSomething()
{
cout << "Hi there!";
}

Our function, entitled printSomething, returns void, and has no parameters.


Now, we can use our function in main().

int main()
{
printSomething();

return 0;
}

Functions

A function declaration, or function prototype, tells the compiler about a function name and how to call the function. The actual
body of the function can be defined separately.

For example:

#include <iostream>
using namespace std;

//Function declaration
void printSomething();
int main() {
printSomething();

return 0;
}

//Function definition
void printSomething() {
cout << "Hi there!";
}

Function Parameters

For a function to use arguments, it must declare formal parameters, which are variables that accept the argument's values.

For example:

void printSomething(int x)
{
cout << x;
}

This defines a function that takes one integer parameter and prints its value.

Formal parameters behave within the function similarly to other local variables. They are created upon entering the function, and
are destroyed upon exiting the function.

Multiple Parameters

You can define as many parameters as you want for your functions, by separating them with commas.

Let's create a simple function that returns the sum of two parameters.

int addNumbers(int x, int y) {


// code goes here
}

As defined, the addNumbers function takes two parameters of type int, and returns int.

If you have multiple parameters, remember to separate them with commas, both when declaring them and when passing the
arguments.

Math Function

Mathematical calculations can be done in C++ programming language using the mathematical functions which are included
in math or cmath library. These mathematical functions are defined to do complex mathematical calculations.

Power

The pow function is used to calculate the power of the base raised to the power of exponent. It accepts two double values as
arguments which are the base and exponents numbers and it returns a single double integer which is base to the power of
exponent.

double pow( double, double)

Sqrt ( square root)

sqrt function in C++ returns the square root of the double integer inside the parameter list. The method accept a double integer
value as input find square root and returns a double integer as output.

double sqrt( double)


Floor

The floor function is used to return the floor value of the given double integer. floor value means rounded down value. the
function accepts a double value as input and Returns double integer value calculated using floor().

double floor( double)

Abs

The abs function returns the absolute value of the integer value. The function accepts an integer value and returns an integer value
that has the same magnitude but positive sign.

double abs( double)

Max and Min

The max(x,y) function can be used to find the highest value of x and y:

And the min(x,y) function can be used to find the lowest value of x and y

Math Functions

min() - returns the smallest number

max() - returns the largest one

fdim() - returns the positive difference between x and y.  The positive difference is x - y if x is greater than y,
and 0 otherwise.

fma()  - performs floating-point multiply-add. Has three parameters: the first two for the multiplication and the third for the
added value.

round() - returns the number, rounded a`s a double data type.

ceil() - rounds upwards

floor() - rounds downwards no matter what

trunc() - cuts the decimal part off and leaves the whole number part intact (does not round it whatsoever)

abs() - returns the absolute value of its parameter.

pow() - takes two input parameters. The first is the base of the power and the second is the exponent.

sqrt() - is an abbreviation of square root, which returns the square root of the number given as a double.

exp() - returns Euler's number raised

C++ FUNCTIONS

A function in a program contains a block of statements that is performing some particular task. Before we discuss more about the
functions, let's see how to –

Declare a function.

Define a function.

Call a function.

How a function is declared?


Before a function is called to perform some particular task, we must declare it. Declaration of a function starts with the declaring
its prototype. Declaring the prototype of a function involves defining the following elements in the following order –

The return type of a function i.e. the type of value returned by a function.

The name of the function.

Argument list within a pair of parentheses(), followed by a semicolon; The argument-list specifies the type and name of arguments
that must be passed to the function. This argument list is also called the formal arguments or formal parameters. 

Note: The empty parenthesis signifies that we are not going to pass any value to function when it is called. 

Let's see an example in which we are declaring a function named welcome() and this function has a void return type, which means
that this function is not going to return any value. and this function will not be passed any value when it is called, hence it
has empty parenthesis() and no type is declared in them. 

Another way to declare a similar function in C++, which will not be passed any value when it is called is by using
the void keyword within its parenthesis. 

Note : By default, every function in C++ language returns an int type of value.

How a function is defined?

Post the declaration of a function, we must define it before it is called. Definition of a function constitutes defining the work done
by the function. Definition of a function starts with the following elements in the following order –

The return type of a function i.e. the type of value returned by a function.

The name of the function.

A pair of parenthesis()

An opening brace {

A single or multiple block of statements contained in the function.

Finally, a closing brace }

Let's continue with our same example and define the function named welcome(which we have declared in the method declaration
segment). This function was declared with a void return type, hence, we will define this method with a void return type and this
function will print a welcome message to the user. 

How a function is called?

Once a function is declared and defined then we can easily call it by just using its name, followed by the pair of parenthesis() and
a semicolon; 

Example of a function in C++

Let's suppose a program has to display a welcome message whenever it is executed following by doing its routine work. In order
to display this welcome message, we could create a separate function in this program called welcome, which will be called at the
beginning of the program and display the welcome message to the user. Let's see how. 

C++ Functions with Arguments

when declaring the prototype of a function, we will have to define the type of each value that will be passed to a function(when it
is called), within the parenthesis(). All such values declared in the prototype of the function are also called the formal arguments
or formal parameters and when such function is called with values, the passed values are called actual arguments or actual
parameters.

You might also like