Download - TA ZC142-L2
-
7/28/2019 TA ZC142-L2
1/47
TA ZC142 Computer ProgrammingLecture 2Date: 28/07/2012
-
7/28/2019 TA ZC142-L2
2/47
Last Lecture
Introduction to Computer Representation of Data
Programming LanguageC Programming Language
Basic Structure
Simple C Program
-
7/28/2019 TA ZC142-L2
3/47
Today s Lecture
-
7/28/2019 TA ZC142-L2
4/47
-
7/28/2019 TA ZC142-L2
5/47
Structure of a C Language Program
/* Program to compute . */ #include /*Library files inclusion */
int main(void){
/* local variable declarations *//* initialization */
/* statements */
return(0);}
Returns 0 to the OS after execution.
Void indicate that main( )receives no data from OS
-
7/28/2019 TA ZC142-L2
6/47
Simple C Program
#include#define KMS_PER_MILE 1.609
int main (void){
float miles, kms;/* Read distance in miles */printf( Enter distance in miles \n);scanf (%f, &miles);
/*Convert the distance to kms*/
kms = KMS_PER_MILES * miles;
printf( Distance in kms = \t %f ,kms ) ; // Display the resultant distance in Kms
return(0);}
Preprocessor directive
Standard Header File
constant
Reserved Words
Variables or User defined identifiers
StandardIdentifier
Comments
-
7/28/2019 TA ZC142-L2
7/47
C Character Set
Uppercase letters A to Z , lower case letters a toz, digits 0 to 9
Special characters.e.g. + , - , *, &, ?, /, \ , #, (, ), {, },[, ], , ==, =, ;, :, ,etc
Escape sequencese.g. \n (new line character), \t (horizontal tab), \v(vertical tab)
-
7/28/2019 TA ZC142-L2
8/47
Variables & Constantsin Programming Language
Variable
It s a name associated with a memory cellwhose value can be changed. All variables must be declared before theycan appear in executable statements
It consists of a data type, followed by oneor more variables.E.g. double miles, kms;
-
7/28/2019 TA ZC142-L2
9/47
Variables
Invalid Remark Valid
5thplayer First char is number player5x First char is not letter xOrder-no - is not allowed Order_nodouble Double is a reserved
worddoubletype
count value Blank is not permitted Count_value
-
7/28/2019 TA ZC142-L2
10/47
Variables & Constantsin Programming Language
ConstantIt s a name associated with a memory cell whose
value can not be changed.Declared by #define directive.
E.g. #define KMS_PER_MILE 1.609 const int x = 300;
const char c = A ;
-
7/28/2019 TA ZC142-L2
11/47
Data Types
It is a set of values and a set of operations onthose values.
Primary Data typesDerived Data types (discussed later)User defined data types (discussed later)
-
7/28/2019 TA ZC142-L2
12/47
Primary Data Type
Data type Description Memory Requirementint Integer quantity 2 bytes
char Single character 1 byte
float Floating pointnumber
4 bytes
double Double precision 8 bytes
-
7/28/2019 TA ZC142-L2
13/47
Assignment Statement
It stores a values or a computation resultin a variable
It is used to perform arithmeticoperations in a programe.g. kms = KMS_PER_MILES * miles;
Assignmentoperator
Multiply operator
-
7/28/2019 TA ZC142-L2
14/47
printf() Function
printf( Distance in kms = \t %f ,kms ) ;
Function namefunctionarguments
Format Stringprintlist
Escapesequence
placeholder
-
7/28/2019 TA ZC142-L2
15/47
scanf() Function
scanf ( %d %d, &age, &year);
Function name
Format String
Addressof operator
Variables
-
7/28/2019 TA ZC142-L2
16/47
Sample Program#include /* program to display use of I/O and data types */int main(){ int marks;
float average;char grade;
printf(Enter your marks: \ n); scanf(%d, &marks); printf(Enter average marks: \ n);
scanf(%f, &average); printf(Enter your grade: \ n);
scanf(%c, &grade); printf(Marks = %d \ n,marks); printf(Average = %f \ n,average); printf(Grade = %c \ n,grade);
return (0);
}
InputEnter your marks:70Enter average marks:50.5Enter your grade:
A
Output
Marks = 70 Average = 50.5Grade = A
-
7/28/2019 TA ZC142-L2
17/47
Arithmetic Operators
Operator Meaning+ Addition or unary plus
- Subtraction or unary minus* Multiplication
/ Division
% Modulo Division
-
7/28/2019 TA ZC142-L2
18/47
Arithmetic Expression Let int x = 15; int y = 6;int z;Example:
z = x+yz = x-y
z = x*yz = x/y (decimal part truncated)z = x%y (remainder of the division)
Q. If x and y are declared as float then?Q. If x is integer and y is float then?
-
7/28/2019 TA ZC142-L2
19/47
Example 2 :Program to compute area and circumference of acircle
#include #define PI 3.14
int main(void){
int radius;float area,circum;
printf("Enter radius of a circle\n");scanf("%d", &radius);
area = PI * radius * radius;circum = 2 * PI * radius; printf("Area = %f \t Circumference =
%f ",area, circum);return(0);
}
-
7/28/2019 TA ZC142-L2
20/47
Precedence & Associativity of
Arithmetic OperatorsOperator Precedence Associativity
() 1 Left to Right
Unary +, - 2 Left to Right
*, /, % 3 Left to Right
+, - 4 Left to Right
-
7/28/2019 TA ZC142-L2
21/47
Assignment Operators
Simple AssignmentOperatorsa = a+1a = a-1a =a*(b+1)a = a/(b+1)a = a%b
Shorthand Operators a+= 1
a-= 1a*=b+1a/=b+1
a%=b
Short hand statement is more conciseand easier to read
-
7/28/2019 TA ZC142-L2
22/47
Example
Let int x, a, b, c;
1. x = a b / 3 + c * 2 12. x = ( (a * b) + (-b / c * a) ) / c3. x = (a c / 5 + b) % 100 + 26
-
7/28/2019 TA ZC142-L2
23/47
Type Conversion
Implicit Type ConversionExplicit Type Conversion
-
7/28/2019 TA ZC142-L2
24/47
Implicit Type Conversion
C automatically converts anyintermediate values to the proper data
typeLower type is automatically converted tothe higher type before operationproceeds
-
7/28/2019 TA ZC142-L2
25/47
-
7/28/2019 TA ZC142-L2
26/47
Implicit Type Conversion
int i, x;float f;
double d;long int l;
x = l / i + i * f - d;
-
7/28/2019 TA ZC142-L2
27/47
Explicit Type Conversion
Also called Type castingExplicit conversion of the data type of some variable or expression locally.Syntax
(type-name) expressionE.g
int female_no, male_no;float ratio;ratio = (float) female_number /male_number;
-
7/28/2019 TA ZC142-L2
28/47
More Examples on type
castingExample ResultX = (int)7.5 X = 7
A = (int)21.3/(int)4.5 A = 5
B = (double)sum/n Results is in double
Y = (int)a+b A is converted to int andthen added to b
Y = (int)(a+b) Result of a+b isconverted to int
-
7/28/2019 TA ZC142-L2
29/47
Final result of an expression is converted to thetype of variable on the left of the assignment =before assigning result to it.
float to int truncation of fractional part.double to float rounding of digitslong int to int dropping of excess higher order bits
-
7/28/2019 TA ZC142-L2
30/47
More Examples:
Write a equivalent C expression for given algebraic expressions.
1. (a+b)(c+d)2. ax 2+bx+c3. p r 2 +2 p rh
4. s = ut + 1/2at2
5. T = (m1m2/m1+m2)
-
7/28/2019 TA ZC142-L2
31/47
Relational OperatorsOperator Meaning
< Is less than
Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
-
7/28/2019 TA ZC142-L2
32/47
Examples:
5 = 0 FALSE
-
7/28/2019 TA ZC142-L2
33/47
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Q. Are !(a>b) and a
-
7/28/2019 TA ZC142-L2
34/47
Exercise 1. Write a program which takes marks of five
students as input and computes the averagemarks. Print the average mark of five students.
2. Write a program to compute roots of a quadraticequation
ax 2 + bx + c = 0
-
7/28/2019 TA ZC142-L2
35/47
Exercise
3 . Write a program to solve two linear equations
2x + 8y 10 = 05x + 6y 5 = 0
-
7/28/2019 TA ZC142-L2
36/47
Increment and DecrementOperators
++ Adds one to the operand-- Subtracts one from the operandBoth are unary operators
++i is equivalent to: i = i+1--i is equivalent to: i = i-1
Is ++i and i++ are same??
-
7/28/2019 TA ZC142-L2
37/47
What are Postfix and Prefix Expressions?
In postfix, the expression is evaluated first using the originalvalue of the variable and then the variable is incremented (or decremented) by one.
Ex. m = n++; or m = n--;
In prefix, the variable is incremented (or decremented) firstand then the expression is evaluated using the new value of the variable.
Ex. m = --n; or m = ++n;
-
7/28/2019 TA ZC142-L2
38/47
Postfix and Prefix Examples
j =10;
i =j++;What is the values of i and j?
j =10;i=--j;
What is the values of i and j?
Output
i = 10 j = 11
Outputi = 9 j = 9
-
7/28/2019 TA ZC142-L2
39/47
Conditional Operator
Also called Ternary operator Syntax
Variable = expression1 ? expression2
: expression3
Example:
a=5; b=10;
x =(a>b) ? a : b; What will be the value of x??
Output
X = 10
-
7/28/2019 TA ZC142-L2
40/47
Bitwise Operators
Manipulation of data at bit levelNot be applied to float or double values
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
> Shift right
-
7/28/2019 TA ZC142-L2
41/47
Special Operators
The Sizeof Operator Returns the number of bytes the operandoccupies
Examples: p=sizeof(value);
q=sizeof(float);
r=sizeof(long int);
-
7/28/2019 TA ZC142-L2
42/47
Comma Operator
Used to link the related expressionstogether.
Evaluation starts from left to right. And the value of the right mostexpression is the value of thecombined expression.Example:
1. value = (a = 6, b = 7, c = 8,a+b+c);
Value contains 21
Operators Associativity Rank
-
7/28/2019 TA ZC142-L2
43/47
(),[] L to R 1
+, -, ++, --, !, ~, *, &, sizeof,(type)
R to L 2
*, /, % L to R 3Binary plus(+) and Binary minus (-) L to R 4
L to R 5
= L to R 6
==, != L to R 7
& L to R 8
^ L to R 9
| L to R 10
&& L to R 11
|| L to R 12
?: R to L 13
=, *=, /=, %=, +=, -=, &=, ^= R to L 14
, L to R 15
-
7/28/2019 TA ZC142-L2
44/47
Algorithms An Algorithm is just a detailed sequence of simple stepsthat are needed to solve a problem
Flow ChartsFlow Charts are maps or graphical representations of aprocess.Steps in a process are shown with symbolic shapes, and
the flow of the process is indicated with arrowsconnecting the symbols
ALGORITHMS AND FLOW CHARTS
-
7/28/2019 TA ZC142-L2
45/47
Symbols in Flowchart
START , END
INPUT
PROCESS INPUT DATA
DECISION
CONNECTOR
CONNECTOR
-
7/28/2019 TA ZC142-L2
46/47
Example Flow Chart
Marks = 50Avg = 45
Marks > avg
END
START
YES
NO
Flow chart to find
whether the the marks of a student is above avg or below avg
Print below Avg
Print above Avg
-
7/28/2019 TA ZC142-L2
47/47
Any Doubts or Questions?