Computer Programming 1ST TERM REVIEWER
Computer Programming 1ST TERM REVIEWER
PROGRAMMING
multistep process for creating a program.
way of giving computer instructions (known as code) about what they should do
next.
written language make all the computer programs and software, that tells the
computer what to do.
Syntax, PL’s own grammar that consists of rules governing the structure and
content of the statement that should be followed.
Source Code
Language Translator
Machine Language
Assembly Language
Programming Paradigm
way of programming to solve problem using some programming language.
Procedure-Oriented Program
The programmer must instruct the computer every step of the way, from start
to its completion, concentrates on the major tasks the program needs to
perform.
the programmer focus on objects that the program can use to accomplish its
goal. The objects can take on many different forms.
[ex: In Windows-based program, it typically use objects suchas check boxes,
list boxes, and buttons]
1. Requirement Analysis
2. System Design
The program design process describes the algorithm for the solution of the
problem.
Example:
Example:
running various tests, such as desk checking and debugging- alpha testing
Type of Errors
float = w/ decimals
Plan the algorithm that will transform the problem’s input into its output.
Each instruction in the algorithm will describe an action that the computer needs
to take.
next, record instructions to process the input items to achieve the problem’s
output (processing typically involves performing one or more calculations
using the input items)
most ends with instruction to display, print, or store the output items
PSEUDOCODE
also called false code because, it has no syntax like any of the programming
language and this can’t be compiled or interpreted by the computer
Not standardized
Every programmer has his or her own version, but you will find some similarities
among other versions.
Writing a Pseudocode
KEYWORDS:
INPUT, ENTER, READ / GET: this is data retrieved from the user
through typing or through an input device.
COMMON OPERATORS:
KEYWORDS in CONDITIONAL:
IF — THEN - ENDIF
IF - ELSE IF — ENDIF
Example:
FLOWCHART
pictorial representation of an ordered, step-by-step solution to a problem.
FLOWCHART SYMBOLS
History of C++
developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill,
New Jersey, as an enhancement to the C language and originally named C with
Classes but later it was renamed C++ in 1983
STRUCTURE OF C++
Preprocessor Directives
Lines beginning with hash or pound sign (#) are directives for the processor.
They tell the compiler to preprocess the source code before compiling.
No white space should appear before the #, and semi colon (;) is NOT required
at the end.
Syntax:
#include<header_file>
Examples:
#include<iostream>
[this specific file iostream includes the declarations of the basic
standard input-output library in C++— ex: cin, cout]
#include<math.h>
[math.h header defines various mathematical functions and one
macro— ex: pow, sqrt]
#include<string>
Syntax:
#define identifier value
Examples:
#define TRUE 1
#define PI 3.14159
#define NAME “SAM”
All lines beginning with two slash signs (//) are considered comments and do not
have effect on the behavior of the program
/* block comments */
purpose is only to allow the programmer to insert notes or descriptions
embedded within the source code.
All the elements of the standard C++ library are declared within what is called
namespace, the namespace with the name std (standard).
int main ()
function named main is a special function in all C++ programs; it is the function
called when the program is run. Semi colon is NOT required at the end.
{ and }
open brace ({) indicates the beginning of main’s function definition and the
closing brace (}) indicated its end.
Everything between these braces is the function’s body that defines what
happens when main is called
All functions use braces to indicate the beginning and end of their definitions.
Program Statement
return 0;
terminates main( ) function and causes it to return the value 0 to the calling
process
ELEMENTS OF C++
Character Set - can be letters (lowercase & uppercase), digits and special
characters
do if static while
Data Types
Constants
Operators
DATA TYPES
All variables use data-type during declaration to restrict the type of data to be
stored. Therefore, we can say that data types are used to tell the varibales the
type of data it can store.
Whenever a variable is defined in C++, the compiler allocates some memory for
that variable based on the data-type with which it is declared.
Primitive Data Types are built-in or predefined data types and can be used
directly by the user to declare variables [ex: int, char, float, bool etc.]
Wide Character
Datatype Modifiers are used with the built-in data types to modify the length of
data that a particular data type can hold.
Data type modifiers in C++ are: Signed, Unsigned, Short, Long
Boolean (bool.) data type is for storing boolean or logical values. It can store
either true or false.
Floating Point (float.) data type is for storing single precision floating point
values or decimal values. It requires 4 byte of memory space.
Double Floating Point (double.) data type is used for storing double precision
floating point values or decimal values. Typically requires 8 bytes of memory
space.
Void datatype represents a valueless entity. Used for those function which does
not returns a value.
Wide Character (wchar_t) data type is also character data type but this data
type has size greater than the normal 8-bit data type. It is generally 2 or 4 bytes
long.
DECLARING VARIABLES
Syntax:
datatype variable_name;
datatype var1, var2, varn;’
Examples:
int value;
char letter; —> letter variable can only store single character input
float num;
int a, size, x;
double interest, profit;
char name[20]; —> name variable can store up to 20 character input
With the const keyword you can declare constants with a specific type in the
same way as you would do with a variable.
Example:
const int StartYear= 2005; —> #define StartYear 2005
const float pi = 3.14159;
const char name[4] = “FEU”;
Types of Operators
Arithmetic Operators`
ORDER OF
PRECEDENCE (Left to
Right Evaluation)
()
++, --
*, /, %
+, -
Example
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
Example: Assume x = 2;
Operator Meaning
== Equal to
Examples:
Expression Evaluated as
5 == 1 0 (false)
5≥1 1 (true)
5≠1 1 (true)
Example:
Assume x = 1, y = 5;
Test = x < = y ? 1 : 0;
Result: 1
MODULE 4:
C++ Basic Input/Output
In C++, input and output (I/O) operators are used to take input and display
output. The operator used for taking the input is known as the extraction or get
from operator (>>), while the operator used for displaying the output is known as
the insertion or put to operator (<<).
The standard C++ library includes the header file iostream, where the standard
input and output stream objects are declared.
syntax:
cout << varName; or
cout << “Some String”; or
cout << value/expression;
The standard output by default is the screen, and the C++ stream object defined
to access it is cout.
Example:
Assuming the age variable contains the value 39 and the zipcode variable
contains 1440, the output of the previous statement would be:
cout does not do automatically add line breaks at the end, unless instructed to
do so.
Example:
cout << “This is a sentence.”;
cout << “This is another sentence.”;
ESCAPE CODES
are used to represent certain special
characters within string literals and
character literals.
STANDARD INPUT
cin
syntax:
cin >> varName; or
cin >> varName1 >> varName2 >> … >> varNamen;
Standard input by default is the keyboard, and the C++ stream object defined to
access it is cin
getline() - standard library function in C++ and is used to read a string or a line
from input stream.
Takes the stream (cin) as first argument, and the string variable as second.
TYPE CONVERSION
process of converting one predefined type into another.
When variables of one type are mixed with variables of another type, a type
conversion will occur.
C++ facilitates the type conversion into the following two forms:
For example, to make sure that expression (x+y/2) evaluates to type float, write it
as:
(float) (x+y/2);
abs( ) and fabs( ) - used to retrieve or calculate the absolute value. abs( ) is
used to calculate the absolute value for integer type numbers whereas fabs( )
are used for floating type numbers.
floor( ) and ceil( ) - functions map a real number to the greatest (ceiling)
preceding or least (floor) succeeding integer. Part of <math.h> header.
trunc( ) - round (truncate) the value toward zero and returns the nearest integral
value.
MODULE 5:
RELATIONAL EXPRESSION
An expression created using a relational operator.
== Equal to
≠ (! =) Not equal to
LOGICAL EXPRESSION
More complex conditions can be created using the logical operations AND(&&),
OR(||), and NOT(!)
IF STATEMENT
The if statement allows you to control if a program enters a section of code or
not based on whether a given condition is true or false. One of the important
functions of the if statement is that it allows the program to select an action
based upon the user's input.
• if
• if-else
• if-ladder
• nested if
if (expression1)
statement1;
else if (expression2)
statement2;
else
statement3;
Note: Whether the indentation exists or not, the compiler will, by default, associate
an else with the closest previous unpaired if, unless braces are used to alter this
default pairing
POINTS TO REMEMBER
Expression evalueted by switch should match a case. This means that the
matching case must also be an integer or a constant expression which evaluates
to an integer.
The process of performing the same task over and over again is called iteration,
and C++ provides built-in iteration functionality.
while structure
for structure
do-while structure
The condition being tested can be evaluated at either (1) the beginning or (2) the
end of the repeating section of code.
Post-Tes Loop (exit-controlled loop) - test occurs at the end of the loop.
the control variable to be incremented (or decremented) each time through the
loop
FOR STATEMENT
used for repeating a statement or series of statements as long as a given
conditional expression evaluates to true. [pre-test loop]
Syntax:
for (initialization; condition; update statement) {
statement(s);
}
Syntax:
while (condition expression){
statement(s);
Syntax:
cin >> variable; //initialize loop control variable
Syntax:
do{
statements;
} while (conditional expression);
CONTINUE STATEMENT
used inside loops. When a continue statement is encountered inside a loop,
control jumps to the beginning of the loop for next iteration, skipping the
execution of statements inside the body of loop for the current iteration.
GOTO STATEMENT
It allows making an absolute jump to another point in the program. You should
use this feature carefully since its execution ignores any type of nesting
limitation.
Easier to maintain
USER-DEFINED FUNCTION
A user defined function is a programmed routine that has its parameters set by
the user of the system.
User defined functions are functions that perform specific tasks within a larger
system, such as a database or spreadsheet program.
If the function returns a value then the type of that value must be specified in
return_type. For the moment this could be int, float or char. If the function does
The parameter_list lists the formal parameters of the function together with their
types.
STRUCTURE OF A FUNCTION
FUNCTION HEADER
FUNCTION BODY
What ever is written with in { } in the above example is the body of the function.
Example:
int sum (int x, int y)
{ int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans; //return the answer
}
It provides the basic information about a function which tells the compiler that the
function is used correctly or not.
It can be used to check the calls to the function for the proper number of
parameters and the correct types of parameters.
To write the prototype, simply copy the header from the function to the beginning
of the program and append a semicolon to the end as a signal to the compiler
that this is not a function but a prototype.
The only difference between the header and the prototype is the semicolon ;
NON-VOID FUNCTION
A function that can be made to return a single value to the calling program is
referred to as non-void function.
VOID FUNCTION
Actual arguments are variables/values used within the function call while formal
parameters are variables used within the function header that receives the copy
of the actual argument values.
Note: The number of actual arguments must be the same as the number of formal
parameters
In some cases where you need to manipulate from inside a function the value of
an external variable we can use arguments passed by reference.
When a variable is passed by reference we are not passing a copy of its value,
but we are somehow passing the variable itself to the function. and any
modification that we do to the local variables will have an effect in their
counterpart variables passed as arguments in the call to the function.
RECURSION OVERVIEW
A function that calls itself is known as a recursive function.
Recursive call is a function call in which the function being called is the same as
the one making the call.
4. When a recursive function is executed, the recursive calls are not implemented
instantly. All the recursive calls are pushed onto the stack until the terminating
condition is not detected, the recursive calls stored in the stack are popped and
executed.
5. During recursion, at each recursive call new memory is allocated to all the local
variables of the recursive functions with the same name.