Vous êtes sur la page 1sur 37

CS1L001 : Programming and Data Structures

Introduction

Joy Mukherjee

School of Electrical Sciences


Computer Science and Engineering
Indian Institute of Technology Bhubaneswar

1 / 37
Books

1 The C Programming Language by Brian W. Kernighan, Dennis M.


Ritchie, Second Edition, Prentice Hall India.
2 Programming with C by Byron S. Gottfried, Second Edition,
Schaums Outline Series, Tata McGraw-Hill.
3 C The Complete Reference by Herbert Schildt, Fourth Edition, Tata
McGraw-Hill.
4 A Book on C by Al Kelley Al and Ira Pohl, Fourth Edition,
Addison-Wesley.
5 Data Structures with C, by Seymour Lipschutz, Schaums Outline
Series, Tata McGraw-Hill.
6 Fundamentals of Data Structures in C by Ellis Horowitz, Sartaj Sahni,
Susan Anderson-Freed, Second Edition, Silicon Press.
7 Introduction to Algorithms by Charles E. Leiserson, Clifford Stein,
Ronald Rivest, and Thomas H. Cormen, Third Edition, MIT Press.
2 / 37
PDS Time TableSection III

Venue: 1st Year Laboratory Complex

Day PDS Room No. Time


Monday PDS Lab (Gr 1) 104 1:30 P.M. - 4:25 P.M.
Tuesday PDS Lab (Gr 2) 104 9:00 A.M. - 11:55 A.M.
Tuesday PDS Tutorial (Gr 2) 303 1:30 P.M. - 2:25 P.M.
Tuesday PDS Tutorial (Gr 1) 302 2:30 P.M. - 3:25 P.M.
Wednesday PDS Lecture 203 10:00 A.M. - 10:55 A.M.
Friday PDS Lecture 203 8:00 A.M. - 9:55 A.M.
Table: B.Tech. 1st Semester (Section III) for 2017 Autumn Semester

3 / 37
Evaluation Procedure in the PDS Theory

Examination % of Marks
Attendance 10%
Class test 10%
Mid-semester 30%
End-semester 50%
Table: Evaluation: B.Tech. 1st Year (Section III) for 2017 Autumn Semester

Regular Attendence is mandatory.


Any student with less than 85% attendance may be deregistered from
the course.
Leave due to medical reasons must be certified by the Hospital.

4 / 37
Evaluation Procedure in the PDS Laboratory

Examination % of Marks
Attendance 10%
Regular Performance 50%
Class Test 40%
Table: Evaluation: B.Tech. 1st Year (Section III) for 2017 Autumn Semester

Regular Attendence is mandatory.


Any student with less than 90% attendance may be deregistered from
the course.
Leave due to medical reasons must be certified by the Hospital.

5 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

6 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

7 / 37
Digital Computer & Computation

A computer is a machine that can perform computation.


A computation involves the following three components:
Input : The user gives a set of input data.
Processing : The input data is processed by a well-defined and
finite sequence of steps.
Output : Some data available from the processing step are
output to the user.
A computer is a device that can solve some meaningful problems. A
digital computer accepts input, processes input and outputs data in
digitized forms (as opposed to analog forms).

8 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

9 / 37
Types of Problems

Functional problems
Decision problems
Search problems
Optimization problems

10 / 37
Functional Problems

A set of arguments a1 , a2 , . . . , an constitute the input. Some function


f(a1 , a2 , . . . , an ) of the arguments is calculated and output to the user.
Polynomial root finding
Input: A polynomial with real coefficients
Output: One (or all) real roots of the input polynomial
Processing: One involves a numerical method (like the
Newton-Raphson method) for computing the real roots of a
polynomial.

11 / 37
Decision Problems

These form a special class of functional problems whose outputs are


yes and no (or true and false, or 1 and 0, etc).
Primality testing
Input: A positive integer
Output: The decision whether the input integer is prime or not
Processing: For checking the primality of n, it is an obvious strategy

to divide n by integers between 2 and n. If a divisor of n is found, n
is declared composite (no), else n is declared prime (yes).
The first known (theoretically) fast algorithm is due to three Indians
(Agarwal, Kayal and Saxena) from IIT Kanpur.

12 / 37
Search Problems

Given an input object, one tries to locate some particular


configuration pertaining to the object and outputs the located
configuration, or failure if no configuration can be located.
4 4 Puzzle
Input: Any configuration A={1, 2, . . . , 15} of the standard 4 4
board and the player has to reach a sorted rowwise configuration
B={1, 2, . . . , 15}.
Output: A sequence of moves that takes from configuration A to
configuration B, if existent, or failure
Processing: In general, finding a sequence of moves that takes from
configuration A to configuration B is a very difficult problem. The
trouble is that one may have to explore an infinite number of
possibilities. Even when the total possibilities are finite in number,
that number is so big that one cannot expect to complete exploration
of all of these possibilities in a reasonable time.

13 / 37
Optimization Problems

Objective: Minimize or Maximize one or more than one goals subject


to some conditions
Traveling salesman problem (TSP)
Input: A set of cities, the cost of traveling between each pair of
cities, and the criterion of cost minimization
Output: A route through all the cities with each city visited only
once except the first city (i.e. the last city) and with the total cost of
travel as small as possible
Processing: Since the total number of feasible routes for n cities is
(n 1)!/2, a finite quantity, checking all routes to find the minimum
is definitely a strategy to solve the TSP. However, (n 1)!/2 grows
very rapidly with n, and this brute-force search is impractical.

14 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

15 / 37
Basic Components of a Digital Computer
John von Neumann first gives a concrete idea of stored program
computers.

Figure: The John von Neumann architecture


16 / 37
Input/Output

Input devices
These are the devices using which the user provides input instances.
In a programmable computer, input devices are also used to input
programs. Examples: keyboard, mouse.
Output devices
These devices notify the user about the outputs of a computation.
Example: screen, printer.

17 / 37
Central Processing Unit (CPU)

CPU: Brain of the computing device and performs the basic


processing steps. It consists of:
An arithmetic and logical unit (ALU): This provides the basic
operational units of the CPU. It is made up of units (like adders,
multipliers) that perform arithmetic operations on integers and real
numbers, and of units that perform logical operations (logical and
bitwise AND, OR etc.).
A control unit: This unit is responsible for controlling flow of data and
instructions.
General purpose registers: A CPU usually consists of a finite number of
registers to store intermediate results and values.

18 / 37
External Memory

Limited Number of CPU registers is inadequate to accommodate


programs and data.
Out-of-the-processor memory provides the desired storage space.
External memory is classified into two categories:
Main (or primary) memory:
High-speed memory that stays close to the CPU
Programs are first loaded in the main memory from secondary memory
and then executed.
Volatile memory: Contents are lost after power-down.
Secondary memory:
This is relatively inexpensive, bigger and low-speed memory.
It is normally meant for off-line storage, i.e., storage of programs and
data for future processing.
Permanent storage: Contents should last even after shut-down.
Examples of secondary storage: pen drive, hard disks and CDROM
disks.
19 / 37
Buses

Buses: A bus is a set of wires that connect the above components.


Buses are responsible for movement of data from input devices to
output devices and from/to CPU and memory.

20 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

21 / 37
Data Area

Program: Set of instructions executed to do a computational task.


Data: Input or Intermediate data values
Instructions reside in the main memory during execution.
In order to separate data from instructions the memory is divided into two
parts:
Data area:
1 Data area stores the variables needed for the processing stage, that
can be read, written and modified by the CPU.
2 It is divided into two parts: a stack part and a heap part.
3 Stack holds all statically allocated memory (local variables),
4 Heap is used to allocate dynamic memory to programs during
run-time.

22 / 37
Instruction Area

Instructions reside in the main memory during execution.


Instruction area:
1 It stores a sequence of instructions that define the steps of the
program.
2 Under the control of a clock, the computer carries out a
fetch-decode-execute cycle in which instructions are fetched
one-by-one from the instruction area to the CPU, decoded in the
control unit and executed in the ALU.
3 Instruction Set: The CPU understands only a specific set of
instructions. The instructions stored in memory must conform to this
specification.

23 / 37
Execution of a Program

Figure: Execution of a Program

24 / 37
The Fetch-Decode-Execute Cycle

For starting the execution of a program,


1 A sequence of machine instructions is copied to the instruction area
of the memory.
2 Global variables and input parameters are copied to the data area of
the memory.
3 Program Counter (PC): PC is loaded with the address of the first
instruction of the program.
4 The CPU fetches the instruction from that location in the memory
that is currently stored in the PC register to Instruction Register (IR).
5 The instruction is decoded by the control unit of the CPU.

25 / 37
The Fetch-Decode-Execute Cycle (cont.)

5 The instruction may require one or more operands. An operand may


be either a data or a memory address.
6 A data may be either a constant or a value stored in the data area of
the memory or a value stored in a register.
7 An address may be either immediate or a resident of the main
memory or available in a register.
8 An immediate operand is available from the instruction itself. The
content of a register is also available at the time of the execution of
the instruction. Finally, a variable value is fetched from the data part
of the main memory.
9 If the instruction is a data movement operation, the corresponding
movement is performed. For example, a load instruction copies the
data fetched from memory to a register, whereas a store instruction
sends a value from a register to the data area of the memory.

26 / 37
The Fetch-Decode-Execute Cycle (cont.)

8 If the instruction is an arithmetic or logical instruction, it is executed


in the ALU after all the operands are available in the CPU registers.
The output from the ALU is stored back in a register.
9 If the instruction is a jump instruction, the instruction must contain a
memory address to jump to. The PC is loaded with this address. A
jump may be conditional, i.e., the PC is loaded with the new address
if and only if some condition(s) is/are true.
10 If the instruction is not a jump instruction, the address stored in the
PC is incremented by one.
11 If the end of the program is not reached, the CPU goes to Step 3 and
continues its fetch-decode-execute cycle.

27 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

28 / 37
Machine Language

Expressed in binary.
Directly understood by the computer.
Not portable; varies from one machine type to another. Program
written for one type of machine will not run on another type of
machine.
Difficult to use in writing programs.

29 / 37
Assembly Language

Mnemonic form of machine language.


Easier to use as compared to machine language. For example, use
ADD instead of 10110100.
Not portable (like machine language).
Assembler translates an assembly program to machine program.
Assembly language is also difficult to use in writing programs. It
requires many instructions to solve a problem.
Example: Find the average of three numbers.
MOV A,X ; A=X
ADD A,Y ; A=A+Y
ADD A,Z ; A=A+Z
DIV A,3 ; A=A/3
MOV RES,A ; RES = A
In C,RES = (X + Y + Z) / 3

30 / 37
HLL Executable

High Level Language (HLL) Assembler Assembly Code


Assembly Code Compiler Object Code
Object Code + Libraries Linker Executable Code

31 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

32 / 37
Write-Compile-Execute

Problem solving with computers involves three steps:


1 Write the program in a high-level language like C using a text editor.
2 Save the program.
3 Compile your program using a C compiler (cc or gcc).
gcc -Wall myprog.c
If your program compiles successfully, a file named a.out (an
abbreviation of assembler output) is created. This file stores the
machine instructions that can be understood by the particular
computer. If compilation fails, you should check your source code for
syntax errors.
4 Run the machine executable file by typing
./a.out
and then hitting the enter button at the command prompt.

33 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

34 / 37
Some Programmer Jargons
Language: The core part of C central to writing C code such as
keyword, syntactic construction.
Source code: The C program you type through your keybord.
Object code: Taking source code and making a program that the
computer can understand (object code). Object code is the input to
the linker.
Library: Added functions for C programming which are used to do
certain tasks.
Header file: Files ending in .h which are included at the start of
source code.
Linker: A program that links separately compiled modules into one
program. It also combines the functions in the Standard C library
with the code that you wrote. The output of the linker is an
executable program.
Executable: The compiled program that the computer can run.
Compile time: The time during which your program is being compiled.
Run time: The time during which your program is executing.
35 / 37
Outline

1 Digital Computer & Computation

2 Types of problems

3 Basic Components of a Digital Computer

4 How does a program run in a computer?

5 How can one program?

6 Write-Compile-Execute

7 Some Programmer Jargon

8 First C Program

36 / 37
First.c

This program takes no input, but outputs the string Hello world! in a
line.
#include <stdio.h>
int main ()
{
printf(Hello world!\n);
return 0;
}

37 / 37

Vous aimerez peut-être aussi