






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Chapter 1: Number System
Number System ๏ท Also known as the base/radix system
(^5818) Radix ๏ท Pertains to the number of symbols that is used for counting in a particular number system General format (DIGIT PRESENTATION): ๐๐โ1, ๐๐โ2, โฆ ๐ 1 , ๐ 0
Common Number System
๏ท Radix starts from base 2 to base 36 (0-9, A-Z) A โ 10 H โ 17 O โ 24 V โ 31 B โ 11 I โ 18 P โ 25 W โ 32 C โ 12 J โ 19 Q โ 26 X โ 33 D โ 13 K โ 20 R โ 27 Y โ 34 E โ 14 L โ 21 S โ 28 Z - 35 F โ 15 M โ 22 T โ 29 G - 16 N - 23 U - 30
Number System Conversion
A. Decimal to Base N INTEGER PART: Continuous division base N DECIMAL PAR: Continuous multiplication base N
To binary: divide by/multiply by 2 SHORTCUT: n= โฆ32, 16,โฆ,2, To octal: divide by/multiply by 8 To hexadecimal: divide by/multiply by 16 B. Base N to decimalโ Using positional notation, it is the total sum of ๐๐ ร ๐๐ where: n= digit position dn= digit presentation b= radix
Whole number To decimal: (Integer part only) n= โฆ9,โฆ,
To decimal: (Decimal part) n= 0, -1, -2,โฆ
๐๐ ร ๐๐
Answer:
C. Base N 1 to Base N 2 Base N 1 โ Decimal โBase N 2
Base 2: n= ..64,...2, 1 Base 8: n= binary grouping (3) Base 16: n= binary grouping (4) Base (general): n= โฆ9,โฆ,0, -1, -2,โฆ
Determining the radix Using positional notation, both digits must be converted to decimal then solve for the unknown base.
Complements
Two forms of complement
Diminished radix Radix (r-1)โs complement (๐๐^ โ 1 ) โ ๐
r's complement [(๐๐^ โ 1 ) โ ๐] + 1
where: r= radix N= given number in (base) decimal n= no. of characters/digit of the given number
Solution 1 (STEPS)
Solution 2 (STEPS)
Radix
Integer part Decimal part
2 x 8^2 = 128 3 x 8^1 = 24 4 x 8^0 = 4 3 x 8-^1 = 0. 4 x 8-^2 = 0.
The maximum number of the base is ๐๐๐ฅ. ๐๐. = ๐๐๐ ๐ โ 1 So if the base is 10, then the maximum number is 9.
Create a number made up of n times the maximum number. Example: (^2561310) n= 5 digits b= 10 max.no.= 10-1=
then, 9 9 9 9 9 10
Solution 3 (STEPS) Note: This is only applicable for binary system.
Operations involving Number Systems ๏ง Addition ๏ง Subtraction ๏ง Multiplication ๏ง Division
In performing any operation, always be mindful of radix (or base).
In division, convert the given number into decimal.
Every programming language has a construct that can be used to define controlled repetition ( loops ) on a program block. A program block that is executed, repeated by a repetition statement is called loop body. The repetition is controlled by a condition (predicate) associated with the repetition statements.
Looping - Looping allows you to execute one or more lines of code as many times as you need.
There are two types of repetition statements:
Pretest repetition statements โ compute a value for its predicate before entering the loop body, and will execute the loop body as long as the predicate is true. Once the predicate is computed to be false, repetition stops and the program control pauses to the next statement that follows the repetition statements.
On the other hand, a posttest repetition statement executes the loop body first and then computes the predicate. If the predicate is true the loop body will be executed again; otherwise the repetition terminates.
Logical Operations
AND
A B A AND B 0 0 0 0 1 0 1 0 0 1 1 1
In using Visual logic,
โstatement (space)โ & or + variable & or + โ(space) unitsโ ยง
Example 1: Draw a flowchart that will calculate the area of a triangle. Input: B,H Output: AREA
Example 2: Create a flowchart that will determine if the grade is passed or failed. (75% passing) Input: GRADE Output: โPASSEDโ or โFAILEDโ
BEGIN
Input: B,H
Output: AREA
END
GRADE^ Input:
GRADE >=
โPASSEDโ^ Output:^ Output: โFAILEDโ
BEGIN
END
Example 3: Construct a flowchart that user will input 3 numbers and the output is to determine the largest number. Input: A, B, C Output: Largest Number
Example 4: The user will input a positive integer (N) to generate the first N even numbers. Draw its flowchart. Input: N Output: First N even numbers
BEGIN
GRADE^ Input:
GRADE >=
A > C B > C
Output: A^ Output: C Output: B Output: C
END
BEGIN
EVEN=
Input: N
N=
END Output: EVEN
EVEN=
Chapter 3: Introduction to C++ programming
Programming Definitions
Computer programming ๏ท the process of implementing the algorithm using a language the computer can understand. (e.g, C++, C, Java, python)
Programming language ๏ท is a set of special words, symbols and rules for constructing a program. There are many programming languages each with their own set of rules.
Identifiers ๏ท are used in C++ to name things. They may contain o letters (lowercase/uppercase) o digits (0-9) o the underscore ( _ ) character
Acceptable: Num num num_ _num 1 ๏ท Letters/underscore character are allowed to come first in the variable identifier. ๏ท C++ is case-sensitive, In other words, uppercase and lowercase letters are considered to be different.
Data Types ๏ท A set of valid data values along with the operations that may be performed on those values. The C++ data types we will use are as follows:
๐ ๐ก๐๐๐๐ x = โABC123โ;
Assignment statement Syntax: variable = expressions
Example:
x=(num1/num2)/ int x=5; or int x; x=5;
Basic structure of a C++ program
Preprossesor directives ๏ท information of the program needs (a list of all necessary header files used in the program). Heading ๏ท int main ( ) - functions by definition return a value Main function { variable declarations and executable statements
FORMAT:
#include
int main ( ) { //Executable Statements โฎ return 0; }
Basic input & output
Basic Output in C++ ๏ท ๐๐๐ข๐ก is a predefined variable in C++ that indicates you are going to output a stream of characters to an output device.
The following expression is a literal constant of type string and a simple arithmetic expression.
cout << โHello World!โ; cout << (num1 + num2)/2; cout << "The average is " << Average;
General form of cout: cout<< ExprOrString << ExprOrString โฆ ;
NOTE: ๐๐๐๐ (end line) causes the cursor to move to the beginning of the next line. This is one way to get blank lines in your output.
Example: cout << โHelloโ << endl << โWorld!โ; Output: Hello World!
Basic Input in C++ ๏ท ๐๐๐ is a predefined variable in C++ that indicates you are going to input a stream of characters from an input device (cin is associated with the standard input device which is the keyboard).
General form of cin: cin >> variable1 << variable2 โฆ ;
Example: cin >> x >> y >> z; or cin >> x; cin >> y; cin >> z;
NOTE: Unlike the output statement which may contain constants, variables or more complicated expressions, the only items that may be specified in an input statement are the names of one or more variables.
Example: cin >> length; cin >> width; or cin >> length >> width;
Arithmetic in C++ ๏ท Arithmetic expressions can be made up of constants, variables, operators and parentheses. The arithmetic operators in C++ are as follows o + (addition) o (subtraction) o (multiplication) o / (division) o % (modulus - the remainder from integer division) NOTE: The % operator may appear ONLY with integer values.
When expressions are evaluated, they are evaluated left to right according to the following precedence rules: ( )
The expression 4 + 8 / 2 is evaluated as follows: 4 + 4 = 8 (division has the highest precedence)
The expression (4 + 8) / 2 is evaluated as follows: 12 / 2 = 6 (parentheses have the highest precedence)
Logical operators AND ( & ) OR ( | ) NOT (! ) XOR ( ^ )
Shifting operators Shift left (โช) Shift right (โซ)
Relational operators equal ( == ) not equal (! = ) less than ( < ) greater than ( > ) less than or equal ( <= ) greater than or equal ( >= ) logical and (& &) logical or ( || )
Combined operators C++ allows operators to be combined for a short-hand notation. Many environments discourage the use of operators in this way as it can make a program more difficult to read and understand. += โ= โ= /= % = & = | = ^ = Example: value = value + 5; is equal to value += 5; num3 *= num1 + 15; is also equal to num3 = num3 * (num1 + 15);
Increment/Decrement
In addition to the math operators, C++ provides increment and decrement operators. ++ increment -- decrement
Increment and Decrement Operators Used in Expressions ++n or --n ๏ท increment/ decrement the contents of n and use the new value of n in the expression. N++ or n-- ๏ท use the current value of n in the expression and when finished, increment/decrement n.
Example 1:
Example 2:
Example 11:
Example 12:
Example 13:
Example 14:
Example 15: N++ or ++N = same answer
Example 16:
Example 17:
Example 18:
Example 19:
Chapter 4: Selection Structures
Selection ๏ท choosing between two or more alternative actions.
Boolean Expression ๏ท an expression that evaluates to either true or false. ๏ท When boolean expressions are formed they must set up a relationship between items in the expression. This relationship is set up using a set of operators called the relational operators.
Relational Operators equal ( == ) not equal (! = ) less than ( < ) greater than ( > ) less than or equal ( <= ) greater than or equal ( >= ) logical and (& &) logical or ( || ) NOTE: The assignment operator is (=) but the relational operator is(==). In other words, if you are assigning a value to a variable, use (=) but if you are asking whether two things are equal, use (==).
Example: Given two variables n1 and n2, assume n1 contains 5 and n2 contains 3. Evaluate each of the following boolean expressions:
Given two variables n1 and n2, assume n1 contains 5 and n2 contains 3. Evaluate each of the following boolean expressions:
n1 + 3 != n2 + 5 n1 >= n n1 * 2 <= n2 * 2 n2 <= n n2 + n1 + 3 == n1 + 6
Precedence Order ๏ท The precedence order when the relational operators are added is as follows:
< <= > >= == != = ๏ท Parentheses may be used to alter the order of evaluation.
Logical operators and expressions
AND ( && ) ๏ท combines two logical expressions and requires that both expressions be TRUE for the entire expression to be TRUE.
num1 <= 10 && num1 != 5
OR ( || ) ๏ท combines two logical expressions and states that if either or both of the expressions are TRUE, the entire expression is TRUE
num1 <1 || num1 > 10
NOT (!) ๏ท precedes a single expression and gives its opposite as the result.
hrsWkd <= 40 is equivalent to !(hrsWkd > 40)
Rewrite the following expressions: !(num1 == num2) !(num1 == num2 || num1 == num3) !(num1 == num2 && num3 > num4)