Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lec 01 Introduction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

CS10003:

Programming & Data Structures

Dept. of Computer Science & Engineering


Indian Institute of Technology Kharagpur

Autumn 2020
Introduction
The Evolution of Electronic Computing

Communication
becoming
Free !!

Storage
became
Free !!

Computation
became
Free !!

1980 1990 2005 2020


Home Computer @ 2004: Predicted versus Real

Predicted in 1954

Reality
Storage has become free!!

Magnetic Tape Drive

128 GB Pen Drive

5 MB Hard Disk [1956]


Communication
Interfaces
A Bluetooth Laser
Virtual Keyboard
From punch cards
to paper tapes
to keyboards
Architecture

Typical system
architecture
for a desktop PC
CPU (Central Processing Unit)
• Computations take place to perform a designated task.
• Large number of registers to temporarily store data and programs
(instructions).
• Functional units (circuitry) to carry out arithmetic and logic
operations

• Retrieves instructions from the memory, interprets (decodes) them, and


performs the requested operation
• Fetch  Decode  Execute cycle

• CPU is also referred to as the processor


• Computers may have multiple processors
• Modern processors are multi-core (multiple processors in one chip)
CPU: A first cut

PC
R1
IR
R2
ALU MAR
R3 MDR
R4 FLAGS
Main Memory
• Uses semiconductor technology
• Allows direct access

• Memory sizes in the range of 256 MegaBytes (MB) to 8


GigaBytes (GB) are typical today.

• Some measures to be remembered


• 1 K = 210 (= 1024)
• 1 M = 220 (= one million approx.)
• 1 G = 230 (= one billion approx.)
Memory: Address and Values
I/O and Peripherals
• Input Device
• Keyboard, Mouse, Scanner, Digital Camera
• Output Device
• Monitor, Printer
• Storage Peripherals
• Magnetic Disks: hard disk, floppy disk
• Allows direct (semi-random) access
• Optical Disks: CDROM, CD-RW, DVD
• Allows direct (semi-random) access
• Flash Memory: pen drives
• Allows direct access
• Magnetic Tape: DAT
• Only sequential access
How does a computer work?
• Stored program concept.
• Main difference from a calculator.

• What is a program?
• Set of instructions for carrying out a specific task.

• Where are programs stored?


• In secondary memory, when first created.
• Brought into main memory, during execution.
Number System – The Basics

• We are accustomed to using the so-called decimal number


system.
• Ten digits :: 0,1,2,3,4,5,6,7,8,9
• Every digit position has a weight which is a power of 10.

• Examples:
234 = 2 x 102 + 3 x 101 + 4 x 100
250.67 = 2 x 102 + 5 x 101 + 0 x 100 + 6 x 10-1 + 7 x 10-2
Computer works with Binary Numbers

• Binary number system:


• Two digits :: 0,1
• Every digit position has a weight which is a power of 2.

• Examples:
101 (Binary) = 1 x 22 + 0 x 21 + 1 x 20 = 5 (Decimal)
11001 (Binary)
= 1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20
= 25 (Decimal)
Bits and Bytes
• Bit
• A single binary digit (0 or 1).

• Nibble
• A collection of four bits (say, 0110).

• Byte
• A collection of eight bits (say, 01000111).

• Word
• Depends on the computer.
• Typically 4 or 8 bytes (that is, 32 or 64 bits).
Number System (Contd.)
• A k-bit decimal number
• Can express unsigned integers in the range
• 0 to 10k – 1
• For k=3, from 0 to 999.

• A k-bit binary number


• Can express unsigned integers in the range
• 0 to 2k – 1
• For k=8, from 0 to 255.
• For k=10, from 0 to 1023.
Classification of Software
Two categories:

1. Application Software
• Used to solve a particular problem.
• Editor, financial accounting, weather forecasting, etc.

2. System Software
• Helps in running other programs.
• Compiler, operating system, etc.
Computer Languages
• Machine Language
• Expressed in binary and 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.

• 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) and requires a translator
program called assembler.
Computer Languages (Contd.)
Assembly Machine
language Assembler language
program program

• Assembly language is difficult to use in writing programs.


• 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 In C,
DIV A,3 ; A=A/3 RES = (X + Y + Z) / 3
MOV RES,A ; RES = A
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.
• They are portable across platforms.
From HLL to executable

HLL
Program

Compiler Object Linker Executable


Code(s) Code

Libraries
Operating Systems
• Makes the computer easy to use.
• Basically the computer is very difficult to use.
• Understands only machine language.
• Categories of operating systems:
• Single user
• Multi user: Time sharing, Multitasking, Real time
• Popular operating systems:
 Windows 10: single-user multitasking
 Unix: multi-user
 Linux: a free version of Unix
• Question:
 How multiple users can work on the same computer?
Some Terminologies
• Algorithm / Flowchart
• A step-by-step procedure for solving a particular problem.
• Independent of the programming language.

• Program
• A translation of the algorithm/flowchart into a form that can
be processed by a computer.
• Typically written in a high-level language like C, C++, Java,
Python, etc.
Programming and Software

• Computer needs to be programmed to do such tasks

• Programming is the process of writing instructions in a


language that can be understood by the computer so that a
desired task can be performed by it

• Program: sequence of instructions to do a task, computer


processes the instructions sequentially one after the other

• Software: programs for doing tasks on computers


Three steps in writing programs

Step 1: Write program in a high-level language


(in your case, C)

Step 2: Compile the program using a C compiler

Step 3: Run the program


(as the computer to execute it)
First C Program: first.c
 Type in the following C program exactly as it is in the file, and
then save it

/* The first C program */

#include <stdio.h>
int main( )
{
printf(“Welcome to IITKGP\n”);
printf(“\tYou are doing PDS Theory.”);
return 0;
}
Structure of a C program

• A collection of functions (we will see what they are later)

• Exactly one special function named main must be present.


Program always starts from there

• Each function has statements (instructions) for declaration,


assignment, condition check, looping etc.

• Statements are executed one by one


Making a Mistake

 Remove the ) (right bracket) after main

/* The first C program */

#include <stdio.h>
int main(
{
printf(“Welcome to IITKGP\n”);
printf(“\tYou are doing PDS Theory.”);
return 0;
}

!! ... To Bug is Human, To Debug Divine ... !!


Configuring and Executing the Program
• Save the file
• Compile the file
• You will see an error printed out:
first.c:4 : error: Syntax error ……..

• Go back and correct the error

• Save the file again


• Compile the file again
• Should show no errors this time

• Run the file and verify that Welcome to IITKGP is printed in a


line and in the next line some (tab) space is left and You are
doing PDS Theory. is printed.
Reading values from keyboard
#include <stdio.h>
int main()
{
int num ;
printf(“Enter How many Students: ”);
scanf("%d", &num);
printf(“No. of students is %d.\n”, num);
return 0;
}

Output:
Enter How Many Students: 180
No. of students is 180.
The C Character and Keyword Set
• The C language alphabet ! # % ^ & * ( )
• Uppercase letters ‘A’ to ‘Z’
- _ + = ~ [ ] \
• Lowercase letters ‘a’ to ‘z’
• Digits ‘0’ to ‘9’ | ; : ‘ “ { } ,
• Certain special characters: . < > / ? blank

C program should not contain anything else!

• Used Keywords by the C language, cannot be used as variable


names
• Examples:
int, float, char, double, main, if, else, for, while, do, struct,
union, typedef, enum, void, return, signed, unsigned, case,
break, sizeof, …
• There are others, see textbook …
Computer Program is Everywhere!
• Determining if a given integer is a prime number
• A Palindrome recognizer
• Airline Reservation System
• Journey Route Determination
• Telephone pole placement
• Patriot Missile Control
• Autonomous vehicles
• Finger-print recognition
• Chess Player
• Speech Recognition
• Language Recognition
• Discovering New Laws
• Automatic drug discovery
• …
• …
Increasing Program Size!
Thank You!

You might also like