PF Lecture 1&2
-
Upload
aniqabhutto8767 -
Category
Documents
-
view
216 -
download
0
description
Transcript of PF Lecture 1&2
Programming Fundamentals
Lecture 01
Introduction
Policy for the distribution of marks and examination is as follows:
Class performance & attendance 10 marks Presentation 05
marks Assignment 05
marks Midterms 30 marks Final 50 marks
Course Policy
A program is a precise sequence of steps to solve a particular problem.
Program
It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical.
Alan Perlis – Yale University
Analysis Critical Thinking Attention to Detail
Critical Skills
To design a program properly, we must: Analyze a problem statement, typically
expressed as a word problem. Express its essence, abstractly and with
examples. Formulate statements and comments in a precise
language. Evaluate and revise the activities in light of
checks and tests.
Design Recipe
Computers are
STUPID
Humans are even more…….
Think Reuse Think User Interface Comments liberally
Skills Needed for Programming
Think Reuse
Area of the Ring
Area of Outer Circle = Area of the Ring
System software
Communicates with computer hardware and controls different aspects operations. Sub categories are:
System Software Device DriversUtilities
Software categories
Application softwareA group of program design for end user.e.g. Payroll Systems, MS Word, Accounting Systems.
Software categories
Compiler & interpreter
Compiler & interpreter are the translators to translate programming language into machine language.
Interpreters translates the program line by line meaning it reads one line of program and translates it, then it reads second line, translate it and so on.
The Compiler read the whole program and translates it into machine language completely.
Tools of Trade
What is difference between Compiler and interpreter ?
Home work
EditorsIt is a tool for writing the code of program.
DebuggerCorrects Logical errors during the execution of program.
LinkerLinks the program with routines and functions located on other file.
LoaderLoad the program in main memory and instruct the processor to execute the program from first instruction.
Tools of Trade (cont.)
Programming Fundamentals Lecture 02
# include <iostream.h>main(){cout << "Welcome to Indus University ";}
Basic Syntax
# include <iostream.h> # include is pre-processor directive. It is an instruction to compiler to
add file iostream.h <iostream.h> Name of the library definition file for all Input Output Streams. main() main is actually the one which is run when your program is used. {} Braces allows to group together pieces of a program. cout cout takes data from computer and sends it to the output. << The sign << indicates the direction of data. Here it is towards cout and the
function ofcout is to show data on the screen.
Basic Syntax
<< The sign << indicates the direction of data. Here it is
towards cout and the function ofcout is to show data on the screen.
Welcome to Indus University The thing between the double quotes (“ ”) is known as
character string. In C++ programming character strings are written in double quotes.
; All C statements end with semicolon (;). Missing of a
semicolon (;) at the end of statement is a syntax error and compiler will report an error during compilation.
Name of locations in memory for storing data.
Variables
X
Picture of the memory
Variable
25
10323
nameof the variable
Variable
Variable starts with1. Character2. Underscore _ (Not Recommended)
Small post box
Variable
X
In a program every variable has
Name Type Size Value
Variables
A scope is a region of the program, where variables can be declared:
There are three scopes to declare a variable
◦ Inside a function or a block which is called local variables.◦ In the definition of function parameters which is called formal
parameters.◦ Outside of all functions which is called global variables.
Scope of variable
Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.
#include <iostream.h>void main () {
// Local variable declaration: int a, b; int c; // actual initialization a = 10; b = 20; c = a + b; cout << c;
}
Local Variable
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.
include <iostream.h> // Global variable declaration:
int g; void main () {
// Local variable declaration: int a, b; // actual initialization a = 10;
b = 20; g = a + b;
cout << g; }
Global variable
Assignment Operator
=x = 2
X 2
L.H.S = R.H.S.
X+ 3 = y + 4 WrongZ = x +4x +4 = Z Wrong
Assignment Operator
X = 10 ;
X = 30 ;
X 10
X 30
X = X + 1;
X 10 + 1=
X11
A data type determines what type of values an object can have and what operations can be performed.
Reserve words.
Data types
Data type int i ; ->
Declaration line i
Type KeywordBoolean boolCharacter charInteger intFloating point floatDouble floating point doubleValueless void
Data types
Data types their size and rangeType Typical Bit Width Typical Rangechar 1byte -127 to 127 or 0 to 255unsigned char 1byte 0 to 255signed char 1byte -127 to 127int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767unsigned short int Range 0 to 65,535signed short int Range -32768 to 32767long int 4bytes -2,147,483,647 to 2,147,483,647
signed long int 4bytes same as long intunsigned long int 4bytes 0 to 4,294,967,295float 4bytes +/- 3.4e +/- 38 (~7 digits)double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
#include <iostream.h>main ( ){
int x ;int y ;int z ;x = 10 ;y = 20 ;z = x + y ;cout << " x = " ;cout << x ;cout << " y = " ;cout << y ;cout << " z =x + y = " ;cout << z ;
}
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators
Operators
Plus + Minus - Multiply *
Divide / Modulus %
Arithmetic operators
i + jx * ya / ba % b
Arithmetic operators
5 % 2 = 12 % 2 = 0
% = Remainder
4 / 2 = 25 / 2 = ?
Highest: ( ) Next: * , / , % Lowest: + , -
Precedence