PF Lecture 1&2
-
Upload
aniqabhutto8767 -
Category
Documents
-
view
216 -
download
0
description
Transcript of PF Lecture 1&2
![Page 1: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/1.jpg)
Programming Fundamentals
Lecture 01
Introduction
![Page 2: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/2.jpg)
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
![Page 3: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/3.jpg)
A program is a precise sequence of steps to solve a particular problem.
Program
![Page 4: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/4.jpg)
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
![Page 5: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/5.jpg)
Analysis Critical Thinking Attention to Detail
Critical Skills
![Page 6: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/6.jpg)
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
![Page 7: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/7.jpg)
Computers are
STUPID
![Page 8: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/8.jpg)
Humans are even more…….
![Page 9: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/9.jpg)
Think Reuse Think User Interface Comments liberally
Skills Needed for Programming
![Page 10: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/10.jpg)
Think Reuse
![Page 11: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/11.jpg)
Area of the Ring
Area of Outer Circle = Area of the Ring
![Page 12: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/12.jpg)
System software
Communicates with computer hardware and controls different aspects operations. Sub categories are:
System Software Device DriversUtilities
Software categories
![Page 13: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/13.jpg)
Application softwareA group of program design for end user.e.g. Payroll Systems, MS Word, Accounting Systems.
Software categories
![Page 14: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/14.jpg)
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
![Page 15: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/15.jpg)
What is difference between Compiler and interpreter ?
Home work
![Page 16: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/16.jpg)
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.)
![Page 17: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/17.jpg)
Programming Fundamentals Lecture 02
![Page 18: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/18.jpg)
# include <iostream.h>main(){cout << "Welcome to Indus University ";}
Basic Syntax
![Page 19: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/19.jpg)
# 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
![Page 20: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/20.jpg)
<< 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.
![Page 21: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/21.jpg)
Name of locations in memory for storing data.
Variables
X
![Page 22: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/22.jpg)
Picture of the memory
Variable
25
10323
nameof the variable
![Page 23: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/23.jpg)
Variable
Variable starts with1. Character2. Underscore _ (Not Recommended)
![Page 24: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/24.jpg)
Small post box
Variable
X
![Page 25: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/25.jpg)
In a program every variable has
Name Type Size Value
Variables
![Page 26: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/26.jpg)
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
![Page 27: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/27.jpg)
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
![Page 28: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/28.jpg)
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
![Page 29: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/29.jpg)
Assignment Operator
=x = 2
X 2
![Page 30: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/30.jpg)
L.H.S = R.H.S.
X+ 3 = y + 4 WrongZ = x +4x +4 = Z Wrong
Assignment Operator
![Page 31: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/31.jpg)
X = 10 ;
X = 30 ;
X 10
X 30
![Page 32: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/32.jpg)
X = X + 1;
X 10 + 1=
X11
![Page 33: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/33.jpg)
A data type determines what type of values an object can have and what operations can be performed.
Reserve words.
Data types
![Page 34: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/34.jpg)
Data type int i ; ->
Declaration line i
![Page 35: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/35.jpg)
Type KeywordBoolean boolCharacter charInteger intFloating point floatDouble floating point doubleValueless void
Data types
![Page 36: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/36.jpg)
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)
![Page 37: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/37.jpg)
#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 ;
}
![Page 38: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/38.jpg)
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
![Page 39: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/39.jpg)
Plus + Minus - Multiply *
Divide / Modulus %
Arithmetic operators
![Page 40: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/40.jpg)
i + jx * ya / ba % b
Arithmetic operators
![Page 41: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/41.jpg)
5 % 2 = 12 % 2 = 0
% = Remainder
![Page 42: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/42.jpg)
4 / 2 = 25 / 2 = ?
![Page 43: PF Lecture 1&2](https://reader036.fdocuments.us/reader036/viewer/2022062502/5695d3bc1a28ab9b029efed9/html5/thumbnails/43.jpg)
Highest: ( ) Next: * , / , % Lowest: + , -
Precedence