Docsity

COMPUTER FUNDAMENTALS AND PROGRAMMING (Chapter 5-7), Study notes of Programming for Engineers

Toggle sidebar
bg1
COMPUTER FUNDAMENTALS AND
PROGRAMMING – FINALS REVIEWER
Chapter 5: Looping Structures
Repetitions
Repetition
repeating a block of code until a condition is
met.
Three Repetition Structures
1. WHILE STATEMENT
while(Boolean Expression)
{
//Executable Statements
⋮
}
while statement ([pre] while loop)
-validation before executing statement
2. DO-WHILE STATEMENT
do
{
//Executable Statements
⋮
}
while(Boolean Expression);
do while ([post] while loop)
-execute statement before validation
3. FOR STATEMENT
for(initialization; check condition;
increment/decrement)
{
//Executable Statements
⋮
}
For statement
-initialization, validation, update value
-then execute statement
For loop
-initialization and validation, then output
-balik sa for (...., decrement/increment) tsaka lang
gagawin yung decrement/increment then
validation uli (checking yon (yung nasa pangalawa
which is the condition))
These three steps are essential for looping
structures.
1. Initialized the loop control variable.
2. check the loop control variable. (Boolean
Expression)
3. change the loop control variable.
Difference of while and do while statement
while statement (no semicolon)
do while (with semicolon)
*sa do while tsaka while, boolean expression lang
yung initialization tsaka yung pagchange ng loop
control variable ilalagay niyo siya sa executaable
statement (kasama siya dun sa loob ng block)
*unlike sa for statement, required ka mag-initialize,
magvalidate tapos kumbaga process nalang yung
nasa loob ng for loop (sa may executable
statement)
*mas simplified yung sa for statement
Break and Continue Statements
The break statement can also be used to jump out
of a loop.
The continue statement breaks one iteration (in the
loop), if a specified condition occurs, and continues
with the next iteration in the loop.
Break statement - exit loop
Continue statement - pag nakita yung continue
babalik siya sa boolean expression
1. Create a c++ program that the user will input a
positive integer and the output is to determine the
factorial of the number.
2. Create a c++ program that the user will input a set
of positive integers (zero to terminate) and the output
is to count the number of even and odd numbers.
pf3
pf4
pf5

Partial preview of the text

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

COMPUTER FUNDAMENTALS AND

PROGRAMMING – FINALS REVIEWER

Chapter 5: Looping Structures Repetitions

Repetition  repeating a block of code until a condition is met.

Three Repetition Structures

  1. WHILE STATEMENT

while(Boolean Expression) { //Executable Statements ⋮ } while statement ([pre] while loop)

- validation before executing statement

  1. DO-WHILE STATEMENT

do { //Executable Statements ⋮ } while(Boolean Expression); do while ([post] while loop)

- execute statement before validation

  1. FOR STATEMENT for(initialization; check condition; increment/decrement) { //Executable Statements ⋮ } _For statement
  • initialization, validation, update value
  • then execute statement_

For loop

  • initialization and validation, then output
  • balik sa for (...., decrement/increment) tsaka lang gagawin yung decrement/increment then validation uli (checking yon (yung nasa pangalawa which is the condition))

These three steps are essential for looping structures.

  1. Initialized the loop control variable.
  2. check the loop control variable. (Boolean Expression)
    1. change the loop control variable.

Difference of while and do while statement while statement (no semicolon) do while (with semicolon)

*sa do while tsaka while, boolean expression lang yung initialization tsaka yung pagchange ng loop control variable ilalagay niyo siya sa executaable statement (kasama siya dun sa loob ng block) *unlike sa for statement, required ka mag-initialize, magvalidate tapos kumbaga process nalang yung nasa loob ng for loop (sa may executable statement) *mas simplified yung sa for statement

Break and Continue Statements The break statement can also be used to jump out of a loop.

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

Break statement - exit loop Continue statement - pag nakita yung continue babalik siya sa boolean expression

  1. Create a c++ program that the user will input a positive integer and the output is to determine the factorial of the number.
  2. Create a c++ program that the user will input a set of positive integers (zero to terminate) and the output is to count the number of even and odd numbers.
  1. Create a c++ program that the user will input a positive integer and the output is to determine the sum of the digits.

Chapter 6: Functions Function

  • it is a set of instructions that is designed to perform a specific task.
  • A program to be broken into small tasks.
  • These are subprograms that do not execute until they called.

function name(function argument) *parameters

Types of Functions

  1. Pre-defined functions
    • already been defined as a part of the language. Example: sqrt(num1); pow(num1,num2); abs(num1); _pre-defined functions
  • built in na siya sa compiler ng program_
  1. User defined functions
    • functions created by a user. _user-defined function
  • created by the user_

Return statement – causes the value to be returned to the calling function. Format: return expression;

Value returning functions  Value returning functions are used when only one result is returned and that result is used directly in an expression.

Function prototype – a function declaration that does not include the body of the function.

FORMAT: returnDataTypeFunctionName(ListofArgumentData Types);

Function definition– a function declaration that includes the body of the function. It is the actual code of the function and always outside the main ( ) function. General form: returnDataType FunctionName(ListofParameters) {

//Executable Statements } Function Declaration

  • Function Prototype (maglalagay ka ng data type) *nandun dapat lahat ng gagamitin mong function
  • Function Definition (execution na nung function, kung para san mo gagamitin)(outside ng main function)

*match dapat yung parameters

Void functions

  • A void function does not return a value to its caller.
  • It is used when input/output is performed and more than one value is to be returned by modifying one or more parameters rather than using a return statement. FORMAT: void FunctionName(ListofArgumentDataTypes)

Void Function

  • pwedeng hindi ka magreturn ng value
  • possible na ginagamit lang yung void function when nagpeperform ka ng input output operations lang
  • or if nagmomodify ka ng madaming parameters din tapos wala kang ginagamit na return statement

Accessing variables in functions Local variable

  • A variable declared within a block and not accessible outside of that block. When the function ends (returns) all of its local variables are destroyed. GLOBAL variable
  • A variable declared outside of all the functions in a program.

Local Variable

  • commonly inside the main function
  • within a certain function
  • ibig sabihin, di mo magagamit yung variables na cinreate mo doon sa certain function name.. so ibig sabihin, pag nagjump ka sa ibang function hindi mo magagamit yung mga variables na nagamit mo sa other function.
  • magkahiwalay sila ng declaration ng variable

Chapter 7: Strings String  a string variable contains a collection of characters surrounded by double quotes.  to use strings, you must include an additional header file in the source code, the library.  can also be defined as character array. Example: string greeting = "hello"; char abc[5]=“hello”;

#include May mga certain string function for manipulations na kailangan talaga ng library file. Pero may mga string function na di na aliangan ng library file na string, parang pre-defined na siya sa mismo general structure ng program na pwede siyanv gamitin.

Arrays composed of multiple storages

Sa character array, laging may space na alotted kaya minus 1 start from index 0

String concatenation The + operator can be used between strings to add them together to make a new string. This is called concatenation. NOTE: C++ uses the + operator for both addition and concatenation. Numbers are added. Strings are concatenated.

String Concatenation Addition for integer purposes Concatenation for string purposes *pero pag nagcombine ka ng string at integer, mag eerror yon

String Functions Manipulation sa characters

  1. Length function FORMAT: strName.length();
  2. Input function FORMAT: getline(cin, strName, [charDelimiter]);

Difference between cin and getline function cin *for integer datatype *treats space or enter as terminator sa pagstore ng bawat characters dun sa variable name getline function

*more appropriate to use in string manipulations *lahat ng special characters tinetreat niya as string (like space, endl)

*para maging same sa output ng cin lagyan lang ng character delimiter (space) meaning magstop siya magstore pagdating sa space.

  1. Ignore function FORMAT: cin.ignore([noOfChar], [charDelimiter]); *yung cin.ignore ilalagay niyo siya lagi before mag input kasi kung after ng (first) input sa next input siya magmamatter *pag naglagay ng character delimeter tapos present siya sa first (n) characters na iignore na character, mas susundin yung character delimiter
  2. Substring function FORMAT: strName.substr(index,[count]); *substring function, kukuha ka lang ng portion of set of characters *index - starting point *count - from the index, magbibilang ka ng n characters *pag gumamit ka ng substring function doesnt mean magbabago yung length nung orig given string
  3. Insert function FORMAT: strName.insert(index, strToInsert); *insert function, meron kang given string tapos pwede ka mag-insert ng number of characters or string to insert *dahil string to insert, kailangan naka double quotations *pagkinuha yung length = given string+inserted string *pag nag-insert ng string, before ng index
  4. Replace function FORMAT: strName.replace(index, count, strToReplace); *replace function, pipili ka raw ng index number at count kung ilan yung sakop na irereplace mo than kung ano yung string na kailangang ireplace *sa string to replace, use double quotations uli *naguupdate din yung length
  5. Erase function FORMAT: strName.erase(index, count); *erase function, pipili ng index number at count kung ilan yung sakop ng ieerase *nag uupdate din yung length *yung index laging magstart before ng character(??)(CONTRADICTING)
  1. Find function FORMAT: strName.find(strToFind,[index]); *find function, magsesearch ng string *output is the index number *output is kung san lagi magstart yung character ng hinahanap na string *index purpose sa format, appropriate gamitin kapag maraming index yung pwedeng makuha sa hinahanap na string *pag gumamit ng index sa format, yun yung starting point ng pagsesearch *pag maraming index na possible makuha sa output, ang kukunin nung find function is yung pinakaunang masesearch niya *pagwalang nasearch, random numbers yung lalabas na output
    1. Assign function FORMAT: strName.assign(count, charToAssign); *assign function, is madalas dito na nagrerepresent ka lang sa character

Final example: Hangman activity

Get character functions  getch();  a pre-defined function in “conio.h”  reads a single character from the keyboard and it doesn’t echo to the console and doesn’t wait for an enter to store in a variable. Get Character function

- kung ano yung tinatype mo hindi makikita sa console

ASCII TABLE / Carriage Return enter - 13 backspace – 08

Mas malaki decimal value ng lower case letter kesa sa upper case letters

exam kasama din yung ascii codes

  1. If a and b are both integer data type and a=3, b=2. Evaluate a^b. a. 0 b. 1 c. 5 d. 6 e. 9
  2. A symbol (;) used for statement terminator.
  3. The 1’s complement of the binary number 10101 is 01010
  4. Base 10 refers to which number system? Decimal
  5. What is the output? 96 int x,y; x=25>>1; y=x<<3; cout<<y;
  6. Which of the following is not a legal variable name? 2ourWorld
  7. Convert the decimal number 151.75 to binary. 10010111.
  8. Convert 11001001 (binary) to decimal. Answer: 201
  9. MOD symbol in evaluating the remainder of two variables in Visual Logic.
  10. Flowchart symbols that is used to test a given condition.
  11. Which of the following are equivalent to (!(x<15 && y>=3))? a. (x>15 && y) b. (x>=15 && y < 3) c. (x>=15 || y < 3) d. (x>15 || y < 3) e. None of the Choices == != !(>) <= !(||) && !(<=) > !(&&) || !(<) >= !(>=) <
  12. What is the output in A? Answer: 4
  13. What is the output? Answer: 17
  14. What is the value of x after the following statements? double x; x = 0; x += 3.0 * 4.0; x - = 2.0; Answer: 10.
  15. Which of the following is the “not equal” operator in Visual Logic? <>
    1. Precedence is the order in which operators are evaluated in a compound expression.
    2. NOT Operator return true when its conditions is false, and vice versa.
    3. Given 101010 base 8, what will be its equivalent value in decimal? 33288
    4. What is the output of the following code fragment? Answer: 13,
    5. Convert the binary number 1011010 to hexadecimal. Answer: 5A.
    6. Suppose that x and y are int variables and the input is: 10 and 20.7. What is the value of x and y if the inputs are already executed: cout << x << y; Answer: x=10, y=
    7. What logic operation that will output zero if the two operands have same input only? XOR
    8. The 2’s Complement of - 17 Answer: 101110 To get the two's complement negative notation of an integer,
    9. You write out the number in signed 8-bit binary. *To get the signed 8-binary, just add 3 leading zeroes to the binary.
    10. Invert 1s to 0, and 0s to 1.
    11. Get the result starting from the third position.

If the number is negative then it is represented using 1’s complement. First represent the number with positive sign and then take 1’s complement of that number.

  1. Convert 11001001 (binary) to decimal. Answer: 201

A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace , you can define the context in which names are defined. In essence, a namespace defines a scope.