Professional Documents
Culture Documents
FINALS Lecture
“Coding is basically the process of creating codes from one language to another one. It can also be called
as a subset of Programming since it actually implements the initial steps of Programming. It involves
writing codes in different languages as instructed. Programming is the process of developing an executable
machine level program that can be implemented without any error. It is the process of formally writing
codes so that the human inputs and corresponding machine outputs remain in sync. ” (Bailey, 2019)
C++ Programming
“An algorithm is a sequence of steps to solve a particular problem or algorithm is an ordered set of
unambiguous steps that produces a result and terminates in a finite time. ” (Walia)
1
To check for additional symbols, you can check the website https://www.edrawsoft.com/flowchart-definition.html#definition
Example:
Create an algorithm and flowchart that will find the sum of 15 and 30.
ALGORITHM FLOWCHART
C. C++ PROGRAMMING
PART I DISPLAYING OUTPUT
For you to start coding and create your program, you need to have an Integrated Development Environment
(IDE). You can download and install Dev C++. You can use other IDEs depending on your preference.
Dev C++ Interface:
To create a new file, go to File tab > select New > select New File or simply press Ctrl + N.
The first program we will create is to display “Hello World” in your console. Type the following codes in your
workspace.
PREPROCESSOR
Main Function
Preprocessor
- Where it executes the preprocessor directives and libraries to be used in the program. Libraries are
also called header files
- Must be the first line of your program
- Written as #include <HeaderFileName>
Function
- Collection of statements or instructions.
- Has parenthesis and braces for the statements or instructions
- An example is the main function where it includes all of the instruction of the program created
cout<<
- an identifier used to display or print the statement inside the quotation marks (“ ”) at the current cursor
position
- cout stands for common output and << is called stream insertion operator
return 0
- In this program, it is used as an end of the program code.
Note: In the English language we use a period (.) to end a statement. In C++ programming, we use a semicolon
(;) to end a statement
To compile and run your program, go to Execute Tab > select Compile and Run or press F11 in your keyboard.
The output of the code earlier:
“endl” means end line, and “\n” means next line. These are used to print or display the next statement on the
next line.
Comment
- To insert a single line comment, use //
- To insert a block comment or multiple line comment, use /* */
- Comments are used for the reader and/ or programmer.
1.
2.
3.
POSITIONING
The window that shows the output of the program has 89 columns and 299 rows.
0---------------------------------------------------------89
0
299
For the output above, it was printed at 0 column and 0 row. 0,0 is the default cursor position of our console. What
are we going to use if we will print Hello World in the following positions: a. 3rd column and 5th row, b. 10th column,
7th row, c. column 20, row 20
We need to use the library windows.h and create the function gotoxy. Where x will be the value of the column
and y will be for the row. Syntax will be gotoxy(column, row);
a. For Hello World printed at 3rd column and 5th row:
Output:
Arithmetic Operators
Output:
A B A && B
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE
A !A
TRUE FALSE
FALSE TRUE
START
Input
Value
TRUE
Test
Expression Statement for TRUE
FALSE
There are two conditional statements usually used – if…else, and switch-case.
Syntax:
If…else (two conditions)
If (condition)
{
//if the condition is met or true, it will proceed in this block
Statement;
}
else
{
//if the condition was not met or considered as false, it will proceed in this block
Statement;
}
Switch-case
switch (variable)
{
case value1:
//if the value of the variable is equal to value1, it will proceed in this block
Statement;
break;
case value2:
//if the value of the variable is equal to value2, it will proceed in this block
Statement;
break;
default:
//if none of the cases has the same value as the variable
Statement;
}
EXAMPLE:
Using if…else, create a program that will display if the input number is 1 or 2.
START
Input Value of x
TRUE
Print “Your
Is x==1 number is 1”
FALSE
TRUE
Print “Your
Is x==2 number is 2”
FALSE
END
EXAMPLE:
Using switch, create a program that will display if the input letter is among a, A, b, or B.
START
Input Value of x
Switch (x)
OUTPUT:
END
PART IV LOOP STATEMENTS
Looping or repetition statements are used for situations in which it is necessary to repeat a set of statements.
There are three looping statements or repetition structures – for, while, and do…while.
FOR LOOP
- The for statement is used to repeat a block of statements enclosed in curly braces a specified number
of times. An increment counter is often used to increment and terminate the loop
Syntax:
for(initialization; condition; increment/decrement)
{
statement;
}
WHILE LOOP
- While loops will loop continuously, and infinitely until
the expression inside the parenthesis becomes
false. Something must change the tested variable in
the condition, or the while loop will never exit. It can
be an incremented or decremented variable.
Syntax:
initialization;
while(condition)
{
statement;
increment/decrement;
}
DO…WHILE LOOP
- The do loop is a bottom driven loop that works in the same
manners as the while loop, with the exception that the
condition is tested at the end of the loop, so the do loop will
ALWAYS run at least once
Syntax:
initialization;
do
{
statement;
increment/decrement;
} while(condition);
In trying these codes, you can remove the comments I have written. What did you observe?
PART V ARRAY
array[]
When you access array_two[0] and ask to provide the value inside it, it will provide 25. When you call
array_two[2] to provide its value, it will print out 6.
In the example above, 25, 13, 6, 75 are called elements. When you want to access an array element, you use
its index. The index value specifies the position of the component in the array.
Example:
Create a program that displays the value of the declared array.
LABORATORY ACTIVITY
These are consolidated activities. A Laboratory Report will be given for each. Video lecture for these lecture
notes will be posted. If you have questions don’t hesitate to ask.
Deadline: end of the semester (January 15, 2021)
PART 1 ACTIVITY
Create a program that will print out the following with your information.
Example output:
PART 2 ACTIVITY
Create a program that will ask the user for two numbers. Using the two numbers, it will provide the sum,
difference, product, quotient, and average.
Example output:
PART 3 ACTIVITY
Using IF…ELSE (laboratory 3) and SWITCH – CASE (laboratory 4) statement, create a program that will ask
the user for two numbers and which operation they would like to do – addition, subtraction, multiplication, division,
or average.
Example output:
\
PART 4 ACTIVITY
Using ALL the loop statements, create a program that will provide the following outputs
4.1 12345 4.2 1
2345 22
345 333
45 4444
5 55555
Example output using for loop:
4.1
4.2
Given the following example of codes, you still have to create 4 programs using while and do…while loops.
PART 5 ACTIVITY
Create a program that will store and calculate the sum of 5 numbers entered by the user using arrays.
Example output:
REMINDER: These are consolidated activities. A Laboratory Report will be given for each. If you
have questions don’t hesitate to ask.
References
Bailey, E. (2019, September 12). What Is The Difference Between Coding & Programming? Retrieved from Codementor
Community: https://www.codementor.io/@edwardbailey/coding-vs-programming-what-s-the-difference-
yr0aeug9o
Freeman, J. (2020, February 28). Flow Chart Definition. Retrieved from Edraw Max:
https://www.edrawsoft.com/flowchart-definition.html#definition
Malik, D. (2017). C++ Programming: From Problem Analysis to Program Design, (Eighth ed.). (D. Malik, Trans.) Boston:
Cengage Learning.