Sunday, 17 June 2012

Sir Asdullah Bhatti The 8th lecture



LECTURE#08
IMPLEMENTATION OF ARITHMATIC OPERATORS

REFERENCES


PBB

3

#include<conio.h>
#include<stdio.h>
main()
{
      //implementation of arithmatic operators
      int x,y;
      printf("Enter first value:");scanf("%d",&x);
      printf("Enter second value:");scanf("%d",&y);
      printf("\n\n");
      printf("\nSum = %d",x+y);
      printf("\nDiff = %d",x-y);
      printf("\nProduct = %d",x*y);
      printf("\nDivision = %d",x/y);
      getch();}



4



#include<conio.h>
#include<stdio.h>
main()
{
      //use of %d - Format Specifier in printf function
     
      int x,y;
     
      printf("Enter first value:");scanf("%d",&x);
      printf("Enter second value:");scanf("%d",&y);
     
      printf("\n\n");
      printf("\n %d + %d = %d",x,y,x+y);
      printf("\n %d - %d = %d",x,y,x-y);
      printf("\n %d * %d = %d",x,y,x*y);
      printf("\n %d / %d = %d",x,y,x/y);
     
     
      getch();}



6



#include<conio.h>
#include<stdio.h>
main()
{
      //Precedence of operator
     
      printf(" %d ",4+5*6);
      printf("\n");
      printf(" %d ",(4+5)*6);
     
     
      getch();}

Sir Asdullah Bhatti The 7th lecture


LECTURE#07
CONSOLE I/O IN C LANGUAGE

SUMMARY
·         Output on console
·         Input @ console

Printf () is the builtin library function for sending something on the screen and it is provided by stdio.h header file, and on the other hand scanf( ) is another function for scanning input from the user on the console screen
<Stdio.h> stands for standard input and output, you must include this file on the top to use console i/o printf and scanf functions.

Both functions use some format specifiers for different data types like

For integers %d
For Float %f
For Char %c
For String %s
For Hexa decimal %x
For unsigned int %u
For double %ld


How to use printf() and scanf() you must take the Reference of  P#1, P#3 & P#4 of PPB


PPB

01

#include<conio.h>
#include<stdio.h>
main()
{
      printf("Hello World");
     
      getch();}




02



#include<conio.h>
#include<stdio.h>
main()
{
     
      printf("Integer occupies %d bytes",sizeof(int));
      printf("\n");
      printf("float occupies %d bytes",sizeof(float));
      printf("\n");
      printf("char occupies %d bytes",sizeof(char));
     
     
      getch();}




03




#include<conio.h>
#include<stdio.h>
main()
{
      //use of %d - Format Specifier in printf function
     
      int x,y;
     
      printf("Enter first value:");scanf("%d",&x);
      printf("Enter second value:");scanf("%d",&y);
     
      printf("\n\n");
      printf("\n %d + %d = %d",x,y,x+y);
      printf("\n %d - %d = %d",x,y,x-y);
      printf("\n %d * %d = %d",x,y,x*y);
      printf("\n %d / %d = %d",x,y,x/y);
     
     
      getch();}




04




#include<conio.h>
#include<stdio.h>
main()
{
      //implementation of arithmatic operators
      int x,y;
      printf("Enter first value:");scanf("%d",&x);
      printf("Enter second value:");scanf("%d",&y);
      printf("\n\n");
      printf("\nSum = %d",x+y);
      printf("\nDiff = %d",x-y);
      printf("\nProduct = %d",x*y);
      printf("\nDivision = %d",x/y);
      getch();}
 

Sir Asdullah Bhatti The 6th lecture



LECTURE#06
INTRODUCTION TO C LANGUAGE

SUMMARY
·         Introduction to c language
·         IDE – Integrated development environment
·         Write first program in C Language
·         Compiling & Linking process
·         Errors and their types

INTRODUCTION TO C LANGUAGE
What should I say C language is a high level programming language, developed by Denis Ritchie @ bell laboratories, actually I am not concern about the history but as a student you must know so it is your assignment to search the history of C language

IDE – INTEGRATED DEVELOPMENT ENVIORNMENT
An integrated development environment is a software application that provide comprehensive facilities to computer programmers for software development and IDE can consist of
1.       Source code editor
2.       Debugger
3.       Language translator
There are lots of IDEs are available in the market for C language development, for example Dev C++, Turbo C++ 4.5 etc you can download any of this from internet

Write first program in C Language

#include<conio.h>                                                                          Header files (pre-processors)
#include<stdio.h>

Void main ()                                                                                       main function – entry point of program
{                                                                                                              start main function
                clrscr();                                                                                 clear screen function  - previous screen

cout<<”Hello, World”;                                                                   for sending stream to console screen

}                                                                                                              end main function










COMPILING & LINKING PROCESS
The program you types in editor is source file, it has a CPP file extension. Remember that source file is not an executable program, it is just the instruction written in high level language and these instructions are not understandable by micro processor. Transferring your source program file into executable program requires two steps.

First you should compile the source file into object file, object file contains a machine language instructions that is executable by micro processor. However these instructions are not complete. Second step in called linking is required because might be your program required library routines.

This picture depict the compilation and linking process




  




















ERRORS
As a computer programmer I believe that no one can write error-free computer program, followings are some type of errors

1.      Compiler errors
These are the errors generated during compile time due to mistyping or forgotten something for example you forget the semi colon after the “cout”, when you compile the source code compiler will show you the error.

2.      Linker Errors
You can experience errors also in linking process, for example you have used the math library in your program but forget to declare the library file <math.h>

3.      Run time errors
You experienced some errors after executing program like divide by zero

4.      Logical errors
I love to see this type of errors, compiler or linker will not inform you about these types of errors because you program was syntactically correct and you have provide all the preprocessor of library files you used.

Read this sentence, I am sure you will understand what the logical error means

“I am a human and I am flying with my wings”

What do you think?  Sentence is grammatically 100% correct but logically completely wrong

So this is the most critical errors to debug

In simple words we can say “errors in output”, or “errors in you logic transformation”;



Sir Asdullah Bhatti The 5th lecture


LECTURE#05
Algorithms Designing

SUMMARY
·         What is algorithm?
·         Why do we have need of algorithm
·         Algorithm v/s flow charts

What is algorithm?
Algorithm is set of finite steps that represent any process and each step takes the process more close to its solution or termination.
Unlike flow charts algorithm steps are written in native language like English, for example
Problem Statement: write an algorithm that takes a number and tell us either the number is “EVEN” or “ODD”

Description: X is a variable for getting input from user; Remainder is variable for holding answer of modulus 
STEP-01:          Get X
STEP-02:          Set Remainder: = X Mod 2
STEP-03:          [Start if block]

If Remainder = 0 then
                                    Print “number is even”
                        Else
                                    Print “number is odd”

                        [End if block]
STEP-04:          Exit


Consider another example for grading system of BISE SUKKUR

Description: variable TOT-OBT is used for storing total obtained marks of student; MAX is used for storing maximum marks and PER is used to hold the percentage






STEP-01:          Get TOT-OBT
STEP-02:          Get MAX
STEP-03           :           Set PER: =TOT-OBT*100/MAX
STEP-04:          [start if block]
                        If PER>=80 then
                                    Print “A-Grade”
                        Else if PER>=70 then
                                    Print “B-Grade”
                        Else if PER>=60 then
                                    Print “C-Grade”
                        Else if PER>=50 then
                                    Print “D-Grade”
                        Else if PER>=40 then
                                    Print “E-Grade”
                        Else
                                    Print “Fail”
                        [End if Block]
STEP-05:          Exit

                                   
                               
Consider another example if a mobile manufacturer HTC decided to increase in salary of employees but according to their grades

Grade = 20, 20%
Grade = 19, 19%
Grade = 18, 18%
Grade = 17, 17%
………
………
Grade 1, 1%

We have to write an algorithm that calculates salary for next month for this situation, so

Description: GRADE is holding for grade of employee; SAL is holding current salary of employee, INCREASED is used to hold increment in RUPEES and NEW-SAL is used to store new calculated salary of employee, which he/she will withdraw from next
Month

STEP-01:               INCREASED: = GRADE * SAL/100
STEP-02:               NEW-SAL: = SAL + INCREASED
STEP-03:               Print NEW-SAL
STEP-04:               Exit


You was thinking that condition flow if/else structure should be used to tackle this situation, but we got solution without using condition flow, is my readers confused, am I right?

Probably “yes”

Do you know why you are confused because you forget the first step to keep in mind while programming and that is

1.       Detail analysis of problem

So when you are going to write any solution for a particular problem, first try to understand the problem and just not understand but detailed analysis is required for example what is input, what type of factors are involved in input, what output is required and how to transform the input into output to get exact solution etc

 Suppose again consider the same problem but this time for different conditions

 Grade 15 to 20, 10%
Grade = 10 to 14, 5%
Grade = 5 to 9, 3%
Grade below 5, 2%

Now this problem is depend on complex condition, therefore we use logical AND operator to check the range of grade between specific values like 15 to 20

STEP-01:               [IF BLOCK START]
If GRADE >=15 AND GRADE<=20 then
INC–SAL = 10xSAL/100
Else if GRADE>=10 AND GRADE<=14 then
                INC-SAL = 5*SAL/100
Else if GRADE>=5 AND GRADE<=9 then
                INC-SAL = 3*SAL/100
Else
                INC-SAL = 2*SAL/100
[END IF BLOCK]
STEP-02:               NEW-SAL = SAL + INC-SAL;
STEP-03:               print NEW-SAL
STEP-04:               EXIT



Assignment for tomorrow: Draw flow chart for above algorithm





Calculator example, we have to draw an algorithm that takes two numbers and a binary arithmetic operator to perform operation between these two


Description: Consider X and Y are two numbers; OP is used for storing operator symbol and ANS is used to hold the result of operation

STEP-01:               GET X
STEP-02:               GET Y
STEP-03:               GET OP
STEP-04:               [IF BLOCK STARTS]
If OP = ‘+’ then
                ANS = X+Y
Else if OP=’-‘then
                ANS = X-Y
Else if OP=’*’ then
                ANS=X*Y
Else if OP=’/’ then
                ANS=X/Y
Else
                Print “Invalid Operator”
                EXIT
[END IF BLOCK]
STEP-05:               Print ANS
STEP-06:               EXIT


Why do we have need of algorithm?
I have one reason algorithm represent process in a very high level language, that’s more easily understandable by the programmer; any programmer can implement algorithm into a code that are specific to its language, so in short we can say logic is easily transferable to any language specific to your needs.

Algorithm v/s flow chart
Both represent finite process no doubt but the way they represent is different, flow charts depicts process by using graphical symbols while algorithm represent process in English like or any native language.  OK






Sir Asdullah Bhatti The 4th lecture

 

LECTURE#04
Flow Charts

SUMMARY
·         Flow charts
·         Flow chart’s building blocks (symbols)

Flow Charts
Do you know that, So far we have designed more than 5 flow charts without starting formal lecture?
Yes, it’s true we did it already. Now it’s time to show to you a proper understanding of flow charts.

“Flow chart is a diagram that represents a process, by using varieties of symbols. Flow charts help to visualize the logic and show what is going on, it shows how inputs are processed and generate outputs”


Flow Chart Building blocks
Unfortunately there is no any standard symbols used but followings are the most common used building blocks

    
1.      











Problem statement: we have three students having different ages; draw a flow chart that tells us the older student than 2 others








Saturday, 16 June 2012

Sir Asdullah Bhatti The 3rd lecture



LECTURE#03
Operators & their types

SUMMARY
·         Operators
o   Arithmetic operators
o   Relational Operators
o   Logical operators
o   Increment (++) and decrement (--) operators
o   Binary v/s unary operators

Arithmetic operators
In C-Language we have usual arithmetic operators, these all are binary operators it means they need two operands to perform an operation.

Plus
Minus
Multiplication
Division
Modulus
+
-           
*
/
%

So you all are familiar with these except last one modulus. Modulus operator operates on two integers, it return remainder after division.

Consider an example, we take input from user and tell the user that the number is either even or odd.

As we know that when we divide any number by 2, if the remainder is 1 then the number is odd, if the remainder is 0 then the number is even.

So

X%Y == 0 then number is even
X%Y == 1 then number is odd


Precedence of operators
The arithmetic operators in an expression are evaluated according to their precedence power; precedence power means which operator will be evaluated first and which will be evaluated after and so on. You can force the evaluation by using parenthesis.

Consider an example 2+3*4 =?

What should be the answer?  Guess 20?

No it’s not 20; actual answer is 14 because multiplication has more precedence power than plus so first 3*4 = 12 then 2+12 = 14

But if you want to force to do addition first then you can use parenthesis like this
(2+3)*4
So now 2+3 is evaluated first that 5 and then 5*4 = 20 

Precedence is depicted in picture below

Operators
( )
*,/
+,-
Precedence
1
2
3


Relational operators
Relational operators actually statements that either could be true of false but not both. In real life we making decisions based on conditions for example students are eligible to take admission in BS (CS) if he/she has a percentage greater than 70 in intermediate exams.

Consider another example; we have result of all students stored in list, obtained and max marks of each subject is mentioned in list, now we have to make two list one that passed in examination and another list that fails in examinations. Moreover if any student gains percentage less than 50 then the student is fail otherwise passed in exam.






















Following table depicts the relational operators
Greater than
Less than
Equal
Not equal
Greater than or equal
Less than or equal
==
!=
>=
<=
 
Nested conditional flow
C language provides if-else structure to make conditional flow. Consider an example of grading system according to the prospectus

Marks
Grade
87 – 100
A – Grade
72 – 86
B – Grade
60 - 71
C – Grade
Below 60
F - Grade

Flow chart for grading system



 










































Logical operators
Logical operators are very helpful when making decision based on more than one conditions or simple decision making on complex conditions. For example my friend ask to my mother yesterday

Q#: Can I go outside to play cricket?
ANS: Yes you can, if your room is clean and you have completed your home work.


   See the flow chart


 



























Logical Operators
And
OR
NOT
&&
||
!


Consider an example for logical OR operator.

Suppose you are going to Cinema Theater for watching film, when you reached at the gate, a gate keeper said to you
“You can only go inside Cinema Theater if you have PASS or you have purchased a ticket”

Consider another example for logical AND operator

Suppose you are going to bank for withdrawal of some cash, but you can only get money from bank

“Either you have check OR you have ATM card with you”

 Consider more real life complex example of AND operator, in a news paper a NOKIA announces job vacancy for Shikarpur but they will only call the peoples for interview who has Masters Degree and 4 years experience and living in Shikarpur district so how we will design flow chart for this scenario

Suppose we have list of applications available, now we have to filter the list and make two new lists, one that are suitable candidate for interview and second for not eligible




 
















 










 

 

 














Increment (++) & decrement (--) operators
These both are the unary operators it means they work with single operand or we can say with single variable like

int x = 10;
x++;    

x++  means x = x+1
it means x is increased by 1

similarly decrement operator (--) decreased the value of the variable by 1 like

int x = 10;
x--;

so the value of x becomes 9

Binary v/s Unary operators
Binary operator works with two operand at least like +, - , * etc and unary operators work with only single operand (value) or single variable only like x++;

So increment (--) and decrement (++) operators are best examples of unary operators in c language