You are on page 1of 19

Comp 1 Computer Fundamentals and Programming

FINALS Lecture

A. INTRODUCTION TO C++ PROGRAMMING


Difference between Coding and Programming

“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

• The ++ in C++ means an increment operator. Therefore, C++ means C language is


incremented to its next level
• It was developed by Bjarne Stroustrup at Bell Labs in the early 1980s.
• It contains all elements of the basic C language.
• It was expanded to include numerous object-oriented programming features.
• It provides a collection of predefined classes, along with the capability of user-defined classes.
• The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and
C++17.

B. FLOWCHARTING and ALGORITHM

“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)

“A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed


to get the solution to a problem.” (Freeman, 2020)

Symbols used in Flowcharting

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

START In flowcharting, we’ll start by using


Step-1 Start the oval shape to represent the
start of the sequence.

INPUT 15 and 30 We’ll use a parallelogram to


Step-2 Input 15 and 30
represent our inputs which are 15
and 30.

Step-3 SUM = 15 + 30 SUM = 15 + 30 For processing the sum of 15 and


30, we will use a rectangle.

Step-4 Display SUM DISPLAY To display our output, we’ll use


SUM another parallelogram.

Step-5 Stop STOP Another oval to terminate the


sequence.

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>

Libraries/ Library/ Header File


- Contains identifiers that are needed in the program
- It is like a directory

using namespace std


- Include these statements for the predefined identifiers cout, cin, endl.
- std means standard

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.

Try these codes and observe the output:

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:

Try these for letters b and c.

PART II USER INPUT


Variable
- A way of naming and storing value for later use by the program.
- Case sensitive and does not start with a number
- To declare a variable, it is written as DataType Variable;
- Example: have a variable x that will store a number → int x;
- To assign a value and have an assignment statement, it is written as DataType Variable = Expression;
- Example: have a variable X that has a value of 10 → int X = 10;
Data Type
NAME BYTE DESCRIPTION RANGE
Integers are the primary datatype for the storage of signed: -32768 to 32767
int 2 unsigned: 0 to 65535
numbers without decimal points.
signed: -2147483648 to 2147483647
long 4 An integer 32-bit length unsigned: 0 to 4294967296
A datatype for floating-point numbers, or numbers The maximum number of decimal
float 4 places is 6 or 7.
that have a decimal point
The maximum number of decimal
double 8 Double-precision floating-point number places is 15.
The maximum number of decimal
long double 10 Long double-precision floating-point number places is 19.
Boolean value. It can take one of two values: true or
bool 1 TRUE or FALSE
false
Character data type for a single letter, digit, and
signed: -128 to 127
char 1 special character / symbol; uses single quotation unsigned: 0 to 255
mark
A sequence of zero or more characters. Usually
string used for words; uses double quotation mark. Needs
the header file ‘string’.

Arithmetic Operators

OPERATION OPERATOR EXAMPLE


Addition + Z = X + Y;
Subtraction - S = y – z;
Multiplication * a = 5 * x;
Division / q = s/4;
Modulus % Remainder = 11 % 2
Compound Assignments
OPERATION DESCRIPTION
x ++ Same as x = x + 1, or increments x by +1
x -- Same as x = x - 1, or decrements x by -1
x += y Same as x = x + y, or increments x by +y
x -= y Same as x = x - y, or decrements x by -y
x *= y Same as x = x * y, or multiplies x by y
x /= y Same as x = x / y, or divides x by y

Try the following codes:

Output:

To read input from the user, we need to use cin.


cin>>
- cin stands for common input and >> is called stream extraction operator
- Syntax: cin>>variable;
Example: Create a program that will ask the user for two numbers then will display it after
There are two ways to read the input.
First, two cin for the inputs:

Second, one cin for the inputs:

PART III CONDITIONAL STATEMENTS


Comparison Operators

OPERATOR DESCRIPTION EXAMPLE


== is equal to X == Y
!= is not equal to X != Y
< is less than X<Y
> is greater than X>Y
<= is less than or equal to X <= Y
>= is greater than or equal to X >= Y
Logical Operators

OPERATOR NAME DESCRIPTION


&& and Two statements/ expressions must be TRUE to have a TRUE output
|| or Either statement/ expression must be TRUE to have a TRUE output
!= not The expression must be FALSE to have a TRUE output
AND Truth Table (consider A and B are statements or expressions):

A B A && B
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE

OR Truth Table (consider A and B are statements or expressions):


A B A || B
FALSE FALSE FALSE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE TRUE

NOT Truth Table (consider A is a statement or expression):

A !A
TRUE FALSE
FALSE TRUE

FLOWCHART OF A CONDITIONAL STATEMENT

START

Input
Value

TRUE
Test
Expression Statement for TRUE

FALSE

Statement for FALSE END

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;
}

If…else if…else (three or more conditions)


If (condition1)
{
//if the condition is met or true, it will proceed in this block
Statement;
}
else if (condition2)
{
//if condition1 is false, condition2 will be checked, and if it’s true, it will proceed in this block
Statement;
}
else
{
//if none of the conditions were met, 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

Print "Your number is neither 1 nor 2." OUTPUT:

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)

case ‘a’ or Print message


case ‘A’

case ‘b’ or Print message


case ‘B’

OUTPUT:

Print " Your letter is neither a/A nor b/B."

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);

EXAMPLE: Create a program that will print ‘A’ 8 times in column 5.


First, we’ll create it without using any loop statement

Then, using different loop statements


FOR LOOP
WHILE LOOP

DO… WHILE LOOP

In trying these codes, you can remove the comments I have written. What did you observe?

PART V ARRAY
array[]

- use to store multiple values using one variable


- any value in the array may be called upon by calling the name of the array and the index number of the
value. Array position value always starts in 0.
- Needs to be declared and optionally assigned values before they can be used
- Syntax: datatype arrayname[];
Example:
int arrayname[];
float array_one[5];
int array_two[]={25,13,6,75};
Array are like boxes. When you declare in your program that you will use an array (using the example above)
this is what it looks like.
For array_one[5], you have 6 boxes with no values

array_one[0] array_one[1] array_one[2] array_one[3] array_one[4] array_one[5]

For array_two[]={25,13,6,75}, you have 4 boxes

array_two[0]=25 array_two[1]=13 array_two[2]=6 array_two[3]=75

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.

Walia, R. K. (n.d.). ALGORITHM & FLOWCHART MANUAL.

You might also like