Programming and Data Structure Instructors: Sujoy Ghosh (1,2,3), Anupam Basu (4,5) Pabitra Mitra (6,7) sujoy, anupam, Dept.

Slides:



Advertisements
Similar presentations
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Advertisements

Lecture 1: Overview of Computers & Programming
 Computer hardware components are the physical pieces of the computer.  The major hardware components of a computer are: – The central processing.
ENGR2216 FORTRAN PROGRAMMING FOR ENGINEERS. Chapter 1 The computer CPU MEMORY INPUT/OUTPUT DEVICES DATA REPRESENTATION BINARY SYSTEM OCTAL & HEXADECIMAL.
COSC 120 Computer Programming
Chapter 1: An Overview of Computers and Programming Languages J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program.
Programming and Data Structure
Main Memory Lecture 2 CSCI 1405, CSCI 1301 Introduction to Computer Science Fall 2009.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Computing Components 01/26/11. Announcements & Reminders Programs 1 due Friday, 9/2/11 What is my late policy? Proxy Codes for Labs  You should be able.
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
Topic 1: Introduction to Computers and Programming
Introduction to Computers and Programming. Some definitions Algorithm: Algorithm: A procedure for solving a problem A procedure for solving a problem.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Introduction to Computers Essential Understanding of Computers and Computer Operations.
CS102 Introduction to Computer Programming
Introduction to Computers and Python. What is a Computer? Computer- a device capable of performing computations and making logical decisions at speeds.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
Introduction to Programming Dr Masitah Ghazali Programming Techniques I SCJ1013.
Chapter 1 Introduction to Programming and the Java Language.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 1.
Chapter 1 An Overview of Computers and Programming Languages.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CSCI 125 & 161 Lecture 2 Martin van Bommel. Hardware vs Software Hardware - physical components you can see and touch –e.g. processor, keyboard, disk.
Topics Introduction Hardware and Software How Computers Store Data
COP1220/CGS2423 Introduction to C++/ C for Engineers Professor: Dr. Miguel Alonso Jr. Fall 2008.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Lesson 1b: Computer Systems and Program Development CPS118.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
An Introduction to Computers August 12, 2008 Mrs. C. Furman.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Introduction Lecture 01.
Introduction to Computer Systems and the Java Programming Language.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages.
Mrs. Ulshafer August, 2013 Java Programming Chapter 1.
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
Beginning Snapshots Chapter 0. C++ An Introduction to Computing, 3rd ed. 2 Objectives Give an overview of computer science Show its breadth Provide context.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
The Computer System CS 103: Computers and Application Software.
Introducing block scheme programming March 17. Algorithm / Flow chart An algorithm or a flowchart is a step-by-step procedure for solving a particular.
A.Abhari CPS1251 Topic 1: Introduction to Computers Computer Hardware Computer components Connecting Computers Computer Software Operating System (OS)
Computer Systems. Bits Computers represent information as patterns of bits A bit (binary digit) is either 0 or 1 –binary  “two states” true and false,
Computer Programming (1) Code & No.: CS 102 CREDIT HOURS: 5 UNIT Lecture 3.0 hours/week Lab: 2.0 hour/on every week a. This course introduces the students.
CS 125 Lecture 2 Martin van Bommel. Hardware vs Software Hardware - physical components you can see and touch –e.g. processor, keyboard, disk drive Software.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Chapter 1 An Overview of Computers and Programming Languages.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Introduction to Computers and Computing.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming
Java Programming: From the Ground Up
BASIC PROGRAMMING C SCP1103 (02)
Chapter 1: An Overview of Computers and Programming Languages
Topics Introduction Hardware and Software How Computers Store Data
BASIC PROGRAMMING C SCP1103 (02)
Introduction to Programming
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Chapter 1: An Overview of Computers and Programming Languages
C++ Programming: From Problem Analysis to Program Design
Computer Science I CSC 135.
Topics Introduction Hardware and Software How Computers Store Data
ICS103 Programming in C 1: Overview of Computers And Programming
Presentation transcript:

Programming and Data Structure Instructors: Sujoy Ghosh (1,2,3), Anupam Basu (4,5) Pabitra Mitra (6,7) sujoy, anupam, Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Spring Semester 2015Programming and Data Structure1

Some General Announcements Spring Semester 2015Programming and Data Structure2

About the Course L-T-P rating of There is a separate laboratory of – Grading will be separate. Tutorial classes (one hour per week) will be conducted on a “per section” basis during laboratory classes. Evaluation in the theory course: (Absolute Grading) – Mid-semester:30% (25% + 5% for attendance till mid term ) – End-semester: 50% (45% +5% for attendance post midterm ) – Two class tests:20% Spring Semester 2015Programming and Data Structure3

Course Materials The slides for the lectures will be made available on the web. Also register at All important announcements will be put up on the intinno page and the course web page. Spring Semester 2015Programming and Data Structure4

ATTENDANCE IN THE CLASSES IS MANDATORY Students having poor attendance will be penalized in terms of the final grade / deregistration. Any student with less than 75% attendance would be debarred from appearing in the examinations. Spring Semester 2015Programming and Data Structure5

Text/Reference Books 1.C Programming : Kernighan and Ritchie 2.Programming with C B.S. Gottfried, Schaum’s Outline Series, Tata McGraw-Hill, OR Any other book on C Spring Semester 2015Programming and Data Structure6

Introduction Spring Semester 2015Programming and Data Structure7

What is a Computer? Spring Semester 2015Programming and Data Structure8 Central Processing Unit (CPU) Input Device Output Device Main Memory Storage Peripherals It is a machine which can accept data, process them, and output results.

CPU – All computations take place here in order for the computer to perform a designated task. – It has a large number of registers which temporarily store data and programs (instructions). – It has circuitry to carry out arithmetic and logic operations, take decisions, etc. – It retrieves instructions from the memory, interprets (decodes) them, and perform the requested operation. Spring Semester 2015Programming and Data Structure9

Main Memory – Uses semiconductor technology Allows direct access – Memory sizes in the range of 256 Mbytes to 4 Gbytes are typical today. – Some measures to be remembered 1 K = 2 10 (= 1024) 1 M = 2 20 (= one million approx.) 1 G = 2 30 (= one billion approx.) Spring Semester 2015Programming and Data Structure10

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 Spring Semester 2015Programming and Data Structure11

Typical Configuration of a PC CPU:Pentium IV, 2.8 GHz Main Memory:512 MB Hard Disk:80 GB Floppy Disk:Not present CDROM:DVD combo-drive Input Device:Keyboard, Mouse Output Device:17” color monitor Ports:USB, Bluetooth, Infrared Spring Semester 2015Programming and Data Structure12

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. Spring Semester 2015Programming and Data Structure13

Memory map Spring Semester 2015Programming and Data Structure14 Address 0 Address 1 Address 2 Address 3 Address 4 Address 5 Address 6 Address N-1 Every variable is mapped to a particular memory address

Instructions & Variables in Memory Spring Semester 2015Programming and Data Structure Memory location allocated to a variable X X = 10 X = 20 X = X + 1 X = X * 5 Instruction executed TimeTime

Instr. & Variables in Memory (contd.) Spring Semester 2015Programming and Data Structure Variable X Y X = 20 Y = 15 X = Y + 3 Y = X / 6 Instruction executed ? 15 3 TimeTime

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. Spring Semester 2015Programming and Data Structure17

Operating Systems Makes the computer easy to use. – Basically the computer is very difficult to use. – Understands only machine language. Operating systems make computers easy to use. Categories of operating systems: – Single user – Multi user Time sharing Multitasking Real time Spring Semester 2015Programming and Data Structure18

Contd. Popular operating systems: – DOS: single-user – Windows 2000/XP: single-user multitasking – Unix: multi-user – Linux: a free version of Unix The laboratory class will be based on Linux. Question: – How multiple users can work on the same computer? Spring Semester 2015Programming and Data Structure19

Contd. Computers connected in a network. Many users may work on a computer. – Over the network. – At the same time. – CPU and other resources are shared among the different programs. Called time sharing. One program executes at a time. Spring Semester 2015Programming and Data Structure20

Multiuser Environment Spring Semester 2015Programming and Data Structure21 Computer Printer User 1User 2User 4User 3User 4 Computer Network

Computer Languages 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. Spring Semester 2015Programming and Data Structure22

Contd. Assembly Language – Mnemonic form of machine language. – Easier to use as compared to machine language. For example, use “ADD” instead of “ ”. – Not portable (like machine language). – Requires a translator program called assembler. Spring Semester 2015Programming and Data Structure23 Assembler Assembly language program Machine language program

Contd. Assembly language is also difficult to use in writing programs. – Requires many instructions to solve a problem. Example: Find the average of three numbers. MOVA,X; A = X ADDA,Y; A = A + Y ADDA,Z; A = A + Z DIVA,3; A = A / 3 MOVRES,A ; RES = A Spring Semester 2015Programming and Data Structure24 In C, RES = (X + Y + Z) / 3

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. Spring Semester 2015Programming and Data Structure25

Contd. Spring Semester 2015Programming and Data Structure26 CompilerObject code Linker Library HLL program Executable code

To Summarize Assembler – Translates a program written in assembly language to machine language. Compiler – Translates a program written in high-level language to machine language. Spring Semester 2015Programming and Data Structure27

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. Example: 234 = 2 x x x = 2 x x x x x Spring Semester 2015Programming and Data Structure28

Contd. A digital computer is built out of tiny electronic switches. – From the viewpoint of ease of manufacturing and reliability, such switches can be in one of two states, ON and OFF. – A switch can represent a digit in the so-called binary number system, 0 and 1. A computer works based on the binary number system. Spring Semester 2015Programming and Data Structure29

Concept of 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, ). Word – Depends on the computer. – Typically 4 or 8 bytes (that is, 32 or 64 bits). Spring Semester 2015Programming and Data Structure30

Contd. A k-bit decimal number – Can express unsigned integers in the range 0 to 10 k – 1 For k=3, from 0 to 999. A k-bit binary number – Can express unsigned integers in the range 0 to 2 k – 1 For k=8, from 0 to 255. For k=10, from 0 to Spring Semester 2015Programming and Data Structure31

Basic Programming Concepts Spring Semester 2015Programming and Data Structure32

Some Terminologies Algorithm / Flowchart – A step-by-step procedure for solving a particular problem. – Should be 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, etc. Spring Semester 2015Programming and Data Structure33

Variables and Constants Most important concept for problem solving using computers. All temporary results are stored in terms of variables and constants. – The value of a variable can be changed. – The value of a constant do not change. Where are they stored? – In main memory. Spring Semester 2015Programming and Data Structure34

Contd. How does memory look like (logically)? – As a list of storage locations, each having a unique address. – Variables and constants are stored in these storage locations. – Variable is like a house, and the name of a variable is like the address of the house. Different people may reside in the house, which is like the contents of a variable. Spring Semester 2015Programming and Data Structure35

Memory map Spring Semester 2015Programming and Data Structure36 Address 0 Address 1 Address 2 Address 3 Address 4 Address 5 Address 6 Address N-1 Every variable is mapped to a particular memory address

Variables in Memory Spring Semester 2015Programming and Data Structure Memory location allocated to a variable X X = 10 X = 20 X = X + 1 X = X * 5 Instruction executed TimeTime

Variables in Memory (contd.) Spring Semester 2015Programming and Data Structure Variable X Y X = 20 Y = 15 X = Y + 3 Y = X / 6 Instruction executed ? 15 3 TimeTime

Data types Three common data types used: – Integer :: can store only whole numbers Examples: 25, -56, 1, 0 – Floating-point :: can store numbers with fractional values. Examples: , 5.0, – Character :: can store a character Examples: ‘A’, ‘a’, ‘*’, ‘3’, ‘ ’, ‘+’ Spring Semester 2015Programming and Data Structure39

Data Types (contd.) How are they stored in memory? – Integer :: 16 bits 32 bits – Float :: 32 bits 64 bits – Char :: 8 bits (ASCII code) 16 bits (UNICODE, used in Java) Spring Semester 2015Programming and Data Structure40 Actual number of bits varies from one computer to another

Problem solving Step 1: – Clearly specify the problem to be solved. Step 2: – Draw flowchart or write algorithm. Step 3: – Convert flowchart (algorithm) into program code. Step 4: – Compile the program into object code. Step 5: – Execute the program. Spring Semester 2015Programming and Data Structure41

Flowchart: basic symbols Spring Semester 2015Programming and Data Structure42 Computation Input / Output Decision Box Start / Stop

Contd. Spring Semester 2015Programming and Data Structure43 Flow of control Connector

Example 1: Adding three numbers Spring Semester 2015Programming and Data Structure44 READ A, B, C S = A + B + C OUTPUT S STOP START

Example 2: Larger of two numbers Spring Semester 2015Programming and Data Structure45 START STOP READ X, Y OUTPUT Y IS X>Y? OUTPUT X STOP YESNO

Example 3: Largest of three numbers Spring Semester 2015Programming and Data Structure46 START READ X, Y, Z IS LAR > Z? IS X > Y? LAR = XLAR = Y OUTPUT LAROUTPUT Z STOP YES NO

Example 4: Sum of first N natural numbers Spring Semester 2015Programming and Data Structure47 START READ N SUM = 0 COUNT = 1 SUM = SUM + COUNT COUNT = COUNT + 1 IS COUNT > N? OUTPUT SUM STOP YESNO

Example 5: SUM = N 2 Spring Semester 2015Programming and Data Structure48 START READ N SUM = 0 COUNT = 1 SUM = SUM + COUNT*COUNT COUNT = COUNT + 1 IS COUNT > N? OUTPUT SUM STOP YESNO

Example 6: SUM = to N terms Spring Semester 2015Programming and Data Structure49 START READ N SUM = 0 COUNT = 1 SUM = SUM + COUNT * (COUNT+1) COUNT = COUNT + 1 IS COUNT > N? OUTPUT SUM STOP YESNO

Example 7: Computing Factorial Spring Semester 2015Programming and Data Structure50 START READ N PROD = 1 COUNT = 1 PROD = PROD * COUNT COUNT = COUNT + 1 IS COUNT > N? OUTPUT PROD STOP YESNO

Example 8: Computing e x series up to N terms Spring Semester 2015Programming and Data Structure51 START READ X, N TERM = 1 SUM = 0 COUNT = 1 SUM = SUM + TERM TERM = TERM * X / COUNT COUNT = COUNT + 1 IS COUNT > N? OUTPUT SUM STOP YESNO

Example 9: Computing e x series up to 4 decimal places Spring Semester 2015Programming and Data Structure52 START READ X TERM = 1 SUM = 0 COUNT = 1 SUM = SUM + TERM TERM = TERM * X / COUNT COUNT = COUNT + 1 IS TERM < ? OUTPUT SUM STOP YESNO

Example 10: Roots of a quadratic equation Spring Semester 2015Programming and Data Structure53 ax 2 + bx + c = 0 TRY YOURSELF

Example 11: Grade computation MARKS  90  Ex 89  MARKS  80  A 79  MARKS  70  B 69  MARKS  60  C 59  MARKS  50  D 49  MARKS  35  P 34  MARKS  F Spring Semester 2015Programming and Data Structure54

Grade Computation (contd.) Spring Semester 2015Programming and Data Structure55 START READ MARKS OUTPUT “Ex” MARKS  90?MARKS  80?MARKS  70? OUTPUT “A”OUTPUT “B” STOP A YES NO

Spring Semester 2015Programming and Data Structure56 MARKS  60? STOP OUTPUT “C” A MARKS  50?MARKS  35? OUTPUT “D” OUTPUT “P”OUTPUT “F” STOP YES NO