Programming for Problem Solving_intro_pptx
Programming for Problem Solving_intro_pptx
ES CS201
MAKAUT-Syllabus
Course Outcomes
• To formulate simple algorithms for arithmetic and logical
problems.
Out
Computer
A computer
Central
Input Output
Processing
Peripherals Peripherals
Unit (CPU)
Main
Memory
Storage
Peripherals
How does a computer work?
• Stored program
• A program is a coded form of an Algorithm
• A program is a set of instructions for
carrying out a specific task.
• Programs are stored in secondary memory,
when created.
• Programs are in main memory during
execution.
CPU
• Central Processing Unit (CPU) is where
computing takes place in order for a
computer to perform tasks.
• CPU’s have large number of registers
which temporarily store data and programs
(instructions).
• The CPU receives stored instructions,
interprets them and acts upon them.
Computer Program
• A program is ultimately
– a sequence of numeric codes stored in memory which is
converted into simple operations (instructions for the
CPU).
This type of code is known as machine code.
• The instructions are retrieved from
– consecutive (memory) locations
unless the current instruction tells it otherwise (branch /
jump instructions).
Programming Languages
• Machine language
• Assembly Language
– Mnemonics (opcodes)
¨ Start
¨ Read M 0: Start
¨ Write M 1: Read 10
¨ Load Data, M 2: Read 11
¨ Copy M1, M2 3: Add 10, 11, 12
¨ Add M1, M2, M3 4: Write 12
¨ Sub M1, M2, M3 5: Halt
¨ Compare M1, M2, M3
¨ Jump L
¨ Halt
Programming Languages
• Machine language
– Only the machine understands.
– Varies from one class of computers to another.
– Not portable.
• High-level language
– Easier for the user to understand.
– Fortran, C, C++, Java, Cobol, Lisp, etc.
– Standardization makes these languages portable.
• For example, C is available for DOS, Windows, UNIX, Linux, MAC
platforms.
High-Level Language
• Machine language and assembly language
are called low-level languages.
– They are closer to the machine.
– Difficult to use.
• High-level languages are easier to use.
– They are closer to the programmer.
– Examples:
• Fortran, Cobol, C, C++, Java.
– Requires an elaborate process of translation.
• Using a software called compiler.
To type your C program you need another program called Editor.
Once the program has been typed it needs to be converted to
machine language (0s and 1s) before the machine can execute it.
To carry out this conversion we need another program called
Compiler. Compiler vendors provide an Integrated Development
Environment (IDE) which consists of an Editor as well as the
Compiler.
Executable
code
HLL
Compiler Object code Linker
program
C
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 10
m
e X = X +1
X = X*5
Variables in Memory
Memory location allocated
Instruction executed to a variable X
X = 10
T
i X = 20 20
m
e X = X +1
X = X*5
Variables in Memory
Memory location allocated
Instruction executed to a variable X
X = 10
T
i X = 20 21
m
e X = X +1
X = X*5
Variables in Memory
Memory location allocated
Instruction executed to a variable X
X = 10
T
i X = 20 105
m
e X = X +1
X = X*5
Variables (contd.)
X = 20
20 X
Y=15
X = Y+3 ? Y
Y=x/6
Variables (contd.)
X = 20
20 X
Y=15
X = Y+3 15 Y
Y=x/6
Variables (contd.)
X = 20
18 X
Y=15
X = Y+3 15 Y
Y=x/6
Variables (contd.)
X = 20
18 X
Y=15
X = Y+3 3 Y
Y=X/6
The C Programming Language
Why learn C ?
• C was originally developed in the 1970s, by Dennis Ritchie
at Bell Telephone Laboratories, Inc.
• C is a High level , general –purpose structured
programming language. Instructions of C consists of terms
that are very closely same to algebraic expressions,
consisting of certain English keywords such as if, else,
for ,do and while
• C contains certain additional features that allows it to be
used at a lower level , acting as bridge between machine
language and the high level languages.
• This allows C to be used for system programming as well
as for applications programming
The first C program
#include <stdio.h>
void main ()
{
printf ("Hello, World! \n") ;
}
All programs run from the main function
printf is a function in the library stdio.h
To include any library use #include (called
preprocessor directive)
Description
• stdio.h - a header file is a special type file which
contains information that must be included in the
program when it is compiled. The inclusion of this
required information will be handled automatically
by the compiler.
• The function main with empty parentheses, ()
following the name of the function indicate that
this function does not include any arguments.
Comments
• Any string of symbols placed between the
delimiters /* and */.
• Can span multiple lines
• Can not be nested! Be careful.
• /* /* /* Hi */ is an example of a comment.
• /* Hi */ */ is going to generate a parse error
Second C program
#include <stdio.h>
void main()
{
int x = 1, y;
int sum;
y = 3;
sum = x + y; /* adds x to y, places
value in variable sum */
printf( “%d plus %d is %d\n”, x, y, sum );
}
format
specifier
Different types of token are used in C
Type Description
.
Integer constants
• A integer constant is a numeric constant (associated with number)
without any fractional or exponential part. There are three types of
integer constants in C programming:
Floating-point constants
• A floating-point constant is a numeric constant that has either
a fractional form or an exponent form. For example:
2.0,0.0000234,-0.22E-5
Character constants
• A character constant is a constant which uses single quotation
around characters. For example: 'a', 'l', 'm', 'F'
String constants
• String constants are the constants which are enclosed in a pair
of double-quote marks. For example: "good" ,"x","Earth is
round\n"
Escape Sequences
• Sometimes, it is necessary to use characters which cannot be typed or has
special meaning in C programming. For example: newline(enter), tab, question
mark etc. To use these characters, escape sequence is used.
Escape Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
Input and Output
•
printf : performs output to the standard output
device (typically defined to be the monitor)
– It requires a format string to which we can provide
• The text to print out
• Specifications on how to print the values
printf ("The number is %d.\n", num) ;
The format specification %d causes the value listed
after the format string to be embedded in the
output as a decimal number in place of %d.
Input
•
scanf : performs input from the standard input
device, which is the keyboard by default.
– It requires a format string and a list of
variables into which the value received
from the input device will be stored.
•
scanf ("%d", &size) ;
•
scanf ("%c", &nextchar) ;
•
scanf ("%f", &length) ;
C Program # 3
• #include <stdio.h>
main ()
{
int num_of_students ;
scanf ("%d", &num_of_students) ;
printf ("%d \n", num_of_students) ;
}
• &-address of operator
C Instructions
• There are three types of instructions in C:
(a) Type Declaration Instruction
(b) Arithmetic Instruction
(c) Control Instruction
Type Declaration Instruction
• This instruction is used to declare the type
of variables being used in the program.
• The type declaration statement is written at
the beginning of main( ) function.
Ex.: int bas ;
float rs=2.5; initialization
5/2 2 2/5 0
In the first statement, since both 2 and 9 are integers, the result is
an integer, i.e. 0. This 0 is then assigned to k.
In the second statement 9 is promoted to 9.0 and then the division
is performed. Division yields 0.222222. However, this cannot be
stored in k, as k being an int. Hence it gets demoted to 0 and then
stored in k.
Hierarchy of Operations
The hierarchy of commonly used operators
i= 3 / 2 * 4 + 3 / 8 + 3
Follow BODMAS that tells algebra students in which order
does an expression evaluate
Assignment operator
int x = 4 ;
x=x+9;
1. The right hand side is evaluated.
2. The left hand side is set to the value of the right
hand side.
All expressions evaluate to a value of a particular
type.
x + 9 evaluates to the integer value of 13.
How does C handle any complex expression?
• Let check with some examples
Associativity of Operators
• Associativity can be of two types—Left to Right
or Right to Left.
• Consider expression
a = 3 / 2 * 5; - between / and * which will
execute first? Or
a = b = 3 ; - the second = is performed earlier.
Why?
Exercise
What would be the output of the following programs:
1. If the marks obtained by a student in five different subjects are input through
the keyboard, find out the aggregate marks and percentage marks obtained by
the student. Assume that the maximum marks that can be obtained by a
student in each subject is 100.
2. Temperature of a city in Fahrenheit degrees is input through the keyboard.
Write a program to convert this temperature into Centigrade degrees.
3. The length & breadth of a rectangle and radius of a circle are input through
the keyboard. Write a program to calculate the area & perimeter of the
rectangle, and the area & circumference of the circle.
4. If a four-digit number is input through the keyboard, write a program to obtain
the sum of the first and last digit of this number.
5. If a five-digit number is input through the keyboard, write a program to print a
new number by adding one to each of its digits. For example if the number that
is input is 12391 then the output should be displayed as 23402.
Increment and Decrement Operators