Docsity

COMPUTER FUNDAMENTALS AND PROGRAMMING (Chapter 1-4), Study notes of Programming for Engineers

Toggle sidebar
bg1
COMPUTER FUNDAMENTALS AND
PROGRAMMING โ€“ MIDTERM REVIEWER
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
1. Binary System โ€“ base 2 (0,1)
2. Decimal System โ€“ base 10 (0-9)
a. commonly used
3. Octal System โ€“ base 8 (0-7)
a. used in IP address
4. Hexadecimal System โ€“ base 16 (0-9, A-F)
a. used in assembly language and mac
address
๏‚ท 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
256 . 1310
To binary: divide by/multiply by 2
SHORTCUT: n= โ€ฆ32, 16,โ€ฆ,2,1
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,โ€ฆ,0
To decimal: (Decimal part)
n= 0, -1, -2,โ€ฆ
234 . 348
b = 8
Integer part
Decimal part
2
3
4
.
3
4
n
2
1
0
-1
-2
๐‘‘๐‘›ร— ๐‘๐‘›
Answer:
234 . 348โ†’156.437510
C. Base N1 to Base N2
Base N1 โ†’ Decimal โ†’Base N2
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)
1. Use the diminished radix formula
2. Make sure that the given number is in or
converted to decimal (base 10).
3. Use the radix formula.
Solution 2 (STEPS)
1. Find n. (The number of digits of the given
number)
2. Identify the base (or radix).
3.
Radix
Integer part
Decimal part
2 x 82 = 128
3 x 81 = 24
4 x 80 = 4
3 x 8-1= 0.375
4 x 8-2= 0.0625
156.4375
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download COMPUTER FUNDAMENTALS AND PROGRAMMING (Chapter 1-4) and more Study notes Programming for Engineers in PDF only on Docsity!

COMPUTER FUNDAMENTALS AND

PROGRAMMING โ€“ MIDTERM REVIEWER

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

  1. Binary System โ€“ base 2 (0,1)
  2. Decimal System โ€“ base 10 (0-9) a. commonly used
  3. Octal System โ€“ base 8 (0-7) a. used in IP address
  4. Hexadecimal System โ€“ base 16 (0-9, A-F) a. used in assembly language and mac address

๏‚ท 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

  1. 13 10

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,โ€ฆ

  1. 34 8 b = 8 Integer part Decimal part 2 3 4. 3 4 n 2 1 0 -1 -

๐‘‘๐‘› ร— ๐‘๐‘›

Answer:

  1. 34 8 โ†’ 156. (^437510)

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)

  1. Use the diminished radix formula
  2. Make sure that the given number is in or converted to decimal (base 10).
  3. Use the radix formula.

Solution 2 (STEPS)

  1. Find n. (The number of digits of the given number)
  2. Identify the base (or radix).

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.

  1. The maximum number of the base is ๐‘š๐‘Ž๐‘ฅ. ๐‘›๐‘œ. = ๐‘๐‘Ž๐‘ ๐‘’ โˆ’ 1 So if the base is 10, then the maximum number is 9.

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

  1. Subtract the given from the created number. 9 9 9 9 9 10 โˆ’2 5 6 1 3 10
  2. The answer is (r-1)โ€™s complement.
  3. Add the answer in (r-1)โ€™s complement to 1. This will result to the r's complement.

Solution 3 (STEPS) Note: This is only applicable for binary system.

  1. (r-1)โ€™s complement: Replace 0 โ†’ 1. And 1โ†’0. Eliminate the first digit from the left.
  2. Add the answer in (r-1)โ€™s complement to 1. This will result to the r's complement

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.

3. ITERATION OR REPETITION STRUCTURE

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:

  1. Pretest repetition statements
  2. Posttest 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

OR

A B A OR B

In using Visual logic,

  1. Use console Console Output Expression:

โ€œ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

AREA= 0.5BH

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=

EVEN=EVEN+2 A

A

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:

  1. ๐‘โ„Ž๐‘Ž๐‘Ÿ - one alphanumeric character enclosed in single quotes. ๐‘โ„Ž๐‘Ž๐‘Ÿ x = โ€˜5โ€™;
  2. ๐‘–๐‘›๐‘ก - positive or negative integers with or without a sign. (commas are not allowed) ๐‘–๐‘›๐‘ก x = 5; ๐‘–๐‘›๐‘ก x = 5.25;
  3. ๐‘“๐‘™๐‘œ๐‘Ž๐‘ก- also known as โ€œdoubleโ€, positive or negative numbers containing an integer part and a fractional part with a decimal point in between. ๐‘‘๐‘œ๐‘ข๐‘๐‘™๐‘’ x = 5.25; 3. ๐‘ ๐‘ก๐‘Ÿ๐‘–๐‘›๐‘” - an set of characters used to hold data containing more than one character enclosed in double quotes.

๐‘ ๐‘ก๐‘Ÿ๐‘–๐‘›๐‘” x = โ€œABC123โ€;

  1. ๐‘๐‘œ๐‘œ๐‘™ โ€“ true of false ๐‘๐‘œ๐‘œ๐‘™ x = true

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 using namespace std;

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:

COMBINED OPERATORS:

Example 14:

INCREMENT/ DECREMENT

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)