You are on page 1of 31

Computer Programming I

For Pre Engineering Students


What is Computer?
• Computer is programmable device for processing,
storing and displaying information.

• Computers process data under the control of


sets of instructions called computer programs
Computer system
Computer System
 Hard ware: Is physical component that
computer is made of it.

 Soft ware: refers to the programs that run on a


computer.
Software
– Operating System (OS)
• A set of programs that manages a computer’s hardware
devices and controls their processes.
• Most modern operating systems are capable of running
multiple programs at once.
• UNIX, Linux, Mac OS X, and Windows are examples.
– Application Software
• Programs that make the computer useful for the user
• Solve specific problems or supply a service
• Word processors, spreadsheets, databases, etc.
• This is what we will be developing in this class.
Problem solving Cycle
i. Analyze
ii. Design
iii. Code
iv. Debugging and Testing
v. Complete All Documentation and Organize.
Analyze
– Make sure that you understand what the
program should do. What should the user
be able to enter? How? How does the
program come up with an answer?
What does the program output? How?
Design
– Develop a precise sequence of steps to solve the
problem
An algorithm is a precise sequence of steps to solve a
problem
The algorithm should be:
• Precise and unambiguous
• Simple
• Correct
• Efficient
Programming tools
• Flowcharts – A chart that consists of symbols
connected by arrows. Within each symbol
is a phrase presenting the activity at that
step. The shape of the symbol indicates
the type of operation that is to occur.
• Pseudocode – an abbreviated plain English
version of actual computer code. Kind of a
mix between English and code. There is no
Official syntax for Pseudocode.
Questions
• For each of the problems below, develop a flow chart
• 1) Receive a number and determine whether it is odd
or even.
• 2) Obtain two numbers from the keyboard, and
determine and display which (if either) is the larger of
the two numbers.
• 3) Receive 3 numbers and display them in ascending
order from smallest to largest
• 4) Add the numbers from 1 to 100 and display the sum
Cont…
• 5) Add the even numbers between 0 and any
positive integer number given by the user.
• 6) Find the average of two numbers given by
the user.
• 7) Find the average, maximum, minimum, and
sum of three numbers given by the user.
• 8) Find the area of a circle where the radius is
provided by the user.
Coding

Implement a solution
– The instructions in a programming language
collectively called code.
– Your code should be a translation of your algorithm
developed into the programming language.
• In this class we use C++, but there are many other
programming languages: C, C++, C#, Ruby, Python, Visual
Basic, etc.
Testing and Debugging
Locate and remove any errors in the program
– Testing is the process of finding errors in a
program
– Debugging is the process of removing errors in a
program.
• An error in a program is called a bug.
Complete All Documentation and Organize

Documentation is any material whose purpose is to


allow another person or programmer to use or
understand the program
– Two kinds of documentation:
External Documentation – Material outside of the code files
that describe the program.
Internal Documentation Lines inside of a code file that do
nothing except describe details of the program. In Java,
these are called comments.
Variables and Constants.
Variables
• A variable is a reserved place in memory to
store information in.
• A variable will have three components:
• Variables are used for holding data values so
that they can be used in
• various computations in a program.
Declaring Variables
• Variables can be created in a process known as
declaration.
• Syntax: Datatype Variable_Name;
int age;
• ƒ The declaration will instruct the computer to
reserve a memory location with the name and
size specified during the declaration.
Initializing Variables
• When a variable is assigned a value at the time of
declaration, it is called variable initialization.

• This is identical with declaring a variable and then


assigning a value to the variable immediately after
declaration. ƒ
• The syntax:
• DataType variable name = initial value;
• e.g. int Age=20;
Scope of Variables
• ƒ Scope of a variable is the boundary or block
in a program where a variable can be
accessed.
• The boundary or block is identified by the left
and right French brackets.
• {
int x;
}
Variables
Based on the place where they declared:

Global variables: are variables that can be


referred/accessed
any where in the code, within any function, as long as it is
declared first. A variable declared before any function
immediately after the include statements are global
variables.
Local Variables: the scope of the local variable is limited
to the code level or block within which they are declared.
Gv and Lv
• #include<iostream>
• using namespace std;
• int fun(int);
• int glv=10;
• int main()
• {
• int lv=8;
• cout<<"local variable accessed in a block only "<<lv<<endl;
• fun(lv);
• return 0;
• }
• int fun(int)
• {
• cout<<"global variable accessed every where "<<glv;
• }
Data Types
• int
• long
• short
• float
• double
• bool
• char
• string
Operators
• ƒ
An operator is a symbol that makes the machine to take an
action. ƒ
• Different Operators act on one or more operands and can also
have
different kinds of operators.
C++ provides several categories of operators, including the
following:
• Assignment operator
• Arithmetic operator
• Relational operator
Cont…
• Logical operator
• Increment/decrement operator
• Conditional operator
• Comma operator
• The size of operator
• Explicit type casting operators, etc
Assignment operator (=)
• ƒ The assignment operator causes the operand on the left side of the
assignment statement to have its value changed to the value on the
right side of the statement.
• ƒ Syntax: Operand1=Operand2;
• ƒ Operand1 is always a variable
• ƒ Operand2 can be one or combination of:

• A literal constant: Eg: x=12;


• •
• A variable: Eg: x=y;
• •
• An expression: Eg: x=y+2;
Cont…
• Compound assignment operators (+=, -=, *=, /=, %=). ƒ
• Compound assignment operator is the combination of the assignment
operator with other operators like arithmetic and bit wise operators. ƒ
• The assignment operator has a number of variants, obtained by
combining it with other operators.
• E.g
value += increase; is equivalent to value = value + increase;
a -= 5; is equivalent to a = a – 5;
a /= b; is equivalent to a = a / b;
price *= units + 1 is equivalent to price = price * (units + 1);

• And the same is true for the rest.


Cont…
• Arithmetic operators (+, -, *, /, %).
• ƒ Except for remainder or modulo (%), all other arithmetic operators
can accept a mix of integers and real operands. Generally, if both
operands are integers then, the result will be an integer. However,
if one or both operands are real then the result will be real.
• When both operands of the division operator (/) are integers, then
the division is performed as an integer division and not the normal
division we are used to.
• Integer division always results in an integer outcome.
• ƒ Division of integer by integer will not round off to the next integer
• Example int x=10,y=4;
• x/y=2 not 2.5 or rounded to 3
Logical Operators (!, &&, ||)
• ƒ Logical negation (!) is a unary operator, which negates the logical
value of its operand. If its operand is non zero, it produce 0, and if it is 0 it produce
1.
• ƒ Logical AND (&&) produces 0 if one or both of its operands evaluate to 0
otherwise it produces 1.
• ƒ Logical OR (||) produces 0 if both of its operands evaluate to 0 otherwise, it
produces 1.
• E.g.:
!20 //gives 0
10 && 5 //gives 1
10 || 5.5 //gives 1
10 && 0 // gives 0
• N.B. In general, any non-zero value can be used to represent the
logical true, whereas only zero represents the logical false.
Increment/Decrement Operators: (++) and
(--)
• ƒ The auto increment (++) and auto decrement
(--) operators provide a convenient way of,
respectively, adding and subtracting 1 from a
numeric variable.
• E.g
• if a was 10 and if a++ is executed then a will
automatically changed to 11.
Prefix and Postfix
ƒ The prefix type is written before the variable. Eg (++ myAge), whereas the
postfix type appears after the variable name (myAge ++).

• Prefix and postfix operators can not be used at once on a single


variable: Eg: ++age-- or --age++ or ++age++ or - - age - - is invalid .
ƒ
• In a simple statement, either type may be used. But in complex
statements, there will be a difference.

• The prefix operator is evaluated before the assignment, and the


postfix operator is evaluated after the assignment.

Examples…
Conditional Operator (?:)
The conditional operator takes three operands. It has the general
form:
Syntax:
operand1 ? operand2 : operand3
ƒ
• First operand1 is a relational expression and will be evaluated.
• If the result of the evaluation is non zero (which means TRUE),
then operand2 will be the final result. Otherwise, operand3 is
the final result.
Examples
• E.g.: General Example
• Z=(X<Y)? X : Y
This expression means that if X is less than Y the value
of X will be assigned to Z otherwise (if X>=Y) the
value of Y will be assigned to Z.
• E.g.:
• int m=2,n=1,min;
• min = (m < n )? m : n;
• The value stored in min is 1.
Some Easy Questions
• T/F Every c++ program must have main
function.
• // describe single comment line.
• /* */ refers double comment line
• Float refers decimal numbers data type.
• A given problem can have many solution and
all solution may not be efficient.

You might also like