july lecture _1_ introduction

53
COL 100 Introduction to Computer Science

Upload: kartik-choudhary

Post on 12-Jan-2016

223 views

Category:

Documents


0 download

DESCRIPTION

ascasdasac

TRANSCRIPT

Page 1: July Lecture _1_ Introduction

COL 100

Introduction to Computer Science

Page 2: July Lecture _1_ Introduction

Instructor

Prof K. K. Biswas Deptt of  Computer Science & EngineeringRoom no. 423,  Bharti [email protected]

Page 3: July Lecture _1_ Introduction

Course Contents

• Introduction to computer Programming using ‘C’ language

• Algorithms• Problem solving using computers

Page 4: July Lecture _1_ Introduction

Reference Books

1. Programming with C (Schaum’s Outlines Series, )– Byron Gottfried, McGraw Hill 

2. The C Programming LanguageBrian W Kernighan, Dennis M Ritchie

Page 5: July Lecture _1_ Introduction

Lab Work

• Labs will be held in Computer Services Centre • TAs will be around to look into your problems• Try to visit the lab at other times as well• More you practice, better you will learn programming

• Cheating Policy

Page 6: July Lecture _1_ Introduction

Brief Introduction to Computers

Page 7: July Lecture _1_ Introduction

7

What does a Computer do?

Computers can perform four general operations,which comprise the information processing cycle.

InputProcess DataOutputStorage (Programs and Data) 

Page 8: July Lecture _1_ Introduction

8

Why is a Computer so Powerful?

• The ability to perform the information processing cycle with amazing speed.

• Reliability (low failure rate).• Accuracy.• Ability to store huge amounts of data and information.

• Ability to communicate with other computers.

Page 9: July Lecture _1_ Introduction

9

How does a Computer knowwhat to do?

• It must be given a detailed list of instructions, called a compute program or software, that tells it exactly what to do.

• The computer program corresponding to that job must be stored in memory.

• The computer can start the operation by executing the program instructions in the specified sequence.

Page 10: July Lecture _1_ Introduction

Primary Components Of A Computer ?

Input devices.Central Processing UnitMemory.Output devices.Storage devices.

Page 11: July Lecture _1_ Introduction

Data Representation

• Data is stored inside the memory in the form of 1’s and 0s, Bits (Binary Digits) as shown below  (ON/OFF positions)

1100 0111     (referred to as a byte )0011 11110101 1110

Page 12: July Lecture _1_ Introduction

Units of Memory

• 1 KB (1 KiloByte) contains 1024 Bytes. • 1 MB (1 MegaByte) consists of 1024 KB.• 1 GB (1 GigaByte) has 1024 MB.• 1 TB  (1 Terabyte) consists of 1024 GB.

Page 13: July Lecture _1_ Introduction
Page 14: July Lecture _1_ Introduction

Secondary Storage Devices

• Secondary storage devices are used to store data when they are not being used in memory. The most common types of secondary storage used on personal computers PCs are

• hard disks• CD‐ROM drives• Flash Drives (USB, pen drives)

Page 15: July Lecture _1_ Introduction

CPU

• Central Processing Unit (CPU)– it executes computer programs. – Instructions in the program tell the processor when and 

what to read from a keyboard; – what to display on a screen; – what to store and retrieve from a disk drive and so on. 

• It can do arithmetic, compare numbers and perform input/output. (read information and display or store it). 

Page 16: July Lecture _1_ Introduction

CPU• Central Processing Unit (CPU)

– it executes computer programs. – Instructions in the program tell the processor when and what to 

read from a keyboard; – what to display on a screen; – what to store and retrieve from a disk drive and so on. 

• It can do arithmetic, compare numbers and perform input/output. (read information and display or store it). 

• It has no magical powers. It is instructive to bear in mind that all computer programs are constructed from sequences of instructions based on  primitive operations

Page 17: July Lecture _1_ Introduction

CPU operation

• The CPU can read from and write to a specified memory location. 

• While writing a byte into a given location, the previous bit pattern is destroyed and the new contents (bit pattern) are saved for future use.

• While reading a byte the contents of that location are NOT changed. 

• CPU has number of on chip storage locations called registers, usually 8,16, or  32 bits long

• The greater the number, the more powerful the CPU

Page 18: July Lecture _1_ Introduction

CPU operation

• The CPU can read from and write to a specified memory location. 

• While writing a byte into a given location, the previous bit pattern is destroyed and the new contents (bit pattern) are saved for future use.

• While reading a byte the contents of that location are NOT changed. 

• CPU has number of on chip storage locations called registers, usually 8,16, or  32 bits long

• greater the number, the more powerful the CPU

Page 19: July Lecture _1_ Introduction

Memory speeds

• on chip registers      – Fastest• Main memory • Secondary memory – slowest

Page 20: July Lecture _1_ Introduction

Software• Software is the set of programs and applications that tell the 

computer what to do.

• Computer programmers write the codes/instructions that make‐up software applications/programs.

• Programs and Data are saved in the same manner in the computer

• Set of instructions is also referred to as lines of code.

Page 21: July Lecture _1_ Introduction

Two Types of Software

Application Software

Operating system Software

Page 22: July Lecture _1_ Introduction

Operating System Software

• Complex set of programs which directs all the activities and sets all the rules for how the hardware and software will work together.

• Manages input devices, output devices, memory units, CPU

• The OS is always present when the computer is running• Responsible for running application programs

Examples : Windows XP, VistaSolarisUnix, Linux 

Page 23: July Lecture _1_ Introduction

Application Software

1. Business software: word processors, spreadsheets, and database programs.

2. Communication software: allows computers to communicate with other computers: fax software, Novell NetWare, AOL, Modem Software.

3. Graphics software: software that allows users to create and manipulate graphics...Photoshop, Print Shop, etc.

Page 24: July Lecture _1_ Introduction

4. Education and Reference software: Programs that help teach new material and ideas, and programs that can be used to find information

5. Entertainment and Leisure software… games

6. Scientific software: Programs to solve scientific problems

Page 25: July Lecture _1_ Introduction

Introduction to ‘C’ Programming

Page 26: July Lecture _1_ Introduction

26

Variables

• Very important concept for programming• An entity that has a value and is known to the program by a 

name • Can store any temporary result while executing a program• Can have only one value assigned to it at any given time 

during execution• The value of a variable can be changed during the 

execution

Page 27: July Lecture _1_ Introduction

27

Contd.• Variables stored in memory• Remember that memory is a list of storage locations, each having a unique address

• A variable is like a bin– The contents of the bin is the value of the variable– The variable name is used to refer to the value of the variable

– A variable is mapped to a location of the memory, called its address

Page 28: July Lecture _1_ Introduction

28

Example of variables#include <stdio.h>void main( ){

int x;int y;x = 1;y = 3;printf("x = %d, y= %d\n", x, y);

}

Page 29: July Lecture _1_ Introduction

29

Variables in Memory

Instruction executed Memory location allocatedto a variable X

Ti

me

X = 10

10X = 20

X = X +1

X = X*5

Page 30: July Lecture _1_ Introduction

30

Variables in Memory

Instruction executed Memory location allocated to a variable X

Ti

me

X = 10

20X = 20

X = X +1

X = X*5

Page 31: July Lecture _1_ Introduction

31

Variables in Memory

Instruction executed Memory location allocated to a variable X

Ti

me

X = 10

21X = 20

X = X +1

X = X*5

Page 32: July Lecture _1_ Introduction

32

Variables in Memory

Instruction executed Memory location allocated to a variable X

Ti

me

X = 10

105X = 20

X = X +1

X = X*5

Page 33: July Lecture _1_ Introduction

33

Variables (contd.)

20

?

X

Y

X = 20

Y=15

X = Y+3

Y=X/6

Page 34: July Lecture _1_ Introduction

34

Variables (contd.)

20

15

X

Y

X = 20

Y=15

X = Y+3

Y=X/6

Page 35: July Lecture _1_ Introduction

35

Variables (contd.)

18

15

X

Y

X = 20

Y=15

X = Y+3

Y=X/6

Page 36: July Lecture _1_ Introduction

36

Variables (contd.)

18

3

X

Y

X = 20

Y=15

X = Y+3

Y=X/6

Page 37: July Lecture _1_ Introduction

37

Data Types

• Each variable has a type, indicates what type of values the variable can hold

• Four common data types in C– int ‐ can store integers (usually 4 bytes)– float ‐ can store  floating point numbers (54.27, 0.05, etc.)

– double ‐ can store double‐precision floating point numbers

– char ‐ can store a character (1 byte)

Page 38: July Lecture _1_ Introduction

38

Contd.

• Must declare a variable (specify its type and name) before using it anywhere in your program

• All variable declarations should be at the beginning

Page 39: July Lecture _1_ Introduction

39

Variable Names

• Sequence of letters and digits• First character must be a letter or ‘_’• No special characters other than ‘_’• No blanks in between• Names are case‐sensitive (max, MAX and Max are different names)

• Examples of valid names: k   rank1   MAX    max   Min    class_rank

• Examples of invalid names:a’s      class rank       2sqroot       class,rank

Page 40: July Lecture _1_ Introduction

More Valid and Invalid Identifiers

• Valid identifiersXabcsimple_interesta123LISTstud_nameEmpl_1Empl_2avg_empl_salary

• Invalid identifiers10abcmy‐name“hello”simple interest(area)rate%

Page 41: July Lecture _1_ Introduction

C Keywords

• Used 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…  

Page 42: July Lecture _1_ Introduction

42

Constants• Integer constants

– Consists of a sequence of digits, with possibly a minus sign before it

• Floating point constants– Two different notations:– Decimal notation: 25.0,  0.0034,  .84,  ‐2.234– Exponential (scientific) notation

3.45e23,  0.123e‐12,  123e2

e means “10 to the power of”

Page 43: July Lecture _1_ Introduction

#include <stdio.h>

int main(void) {

printf(“My name is Kanad Biswas.")

return 0;}

Page 44: July Lecture _1_ Introduction

Compiler

A "translator" which converts a program writtenin a high‐level computer language

into machine language, 001101001010

understood by the computer.

Page 45: July Lecture _1_ Introduction

1) Edit your program2) Compile your program3) Execute your program4) Go to step 1, if you are not satisfied withyour results.

Page 46: July Lecture _1_ Introduction

#include <stdio.h>

int main(void) {

printf(“My name is Kanad Biswas.");

return 0;}

Execution:

My name is Kanad Biswas

Page 47: July Lecture _1_ Introduction

UNDERSTANDING A PROGRAMLINE BY LINE

Page 48: July Lecture _1_ Introduction

#include <stdio.h>

A directive to include a standard C header file. There are some pre-written functions in C that we commonly use.

All the functions that control standard input and output are included in the file stdio.h.

When we include this file, we are allowed to use these functions.

These functions allow us to print information to the screen and read in information from the user.

All of your programs will include this line.

Page 49: July Lecture _1_ Introduction

int main(void) {

All code in C resides in functions.This line signifies the beginning of the function main.

main is a special function.

A program may be made up on many functions, but the computer only DIRECTLY executes main.

Page 50: July Lecture _1_ Introduction

int main(void) {printf(“My name is Kanad Biswas.")return 0; }

Main calls other functions.

All functions are signified by

-a return type, (which is int for this function),

-a name (which is main for this function)

-a parameter list in parentheses (void here)

-curly brace { signifies the beginning of the function.

curly brace } ends the definition of the function.

Page 51: July Lecture _1_ Introduction

printf(“My name is Kanad Biswas.")

This line calls a function printf which resides in stdio.h.

It prints out any string contained in between two sets of double quotes.

Some special characters are also used in printf.

All such characters start with a backslash: \

The code \n stands for a newline character.

This simply tells the computer to advance the cursor to the new line.

Page 52: July Lecture _1_ Introduction

/* My 1st C Program */ COMMENT STATEMENT

#include <stdio.h>int main(void) {

printf("This is my first C Program.");

printf(“ My name is Kanad Biswas.");

return 0;}

This is my first C Program. My name is Kanad Biswas

Page 53: July Lecture _1_ Introduction

/* My 2nd C Program – using new line*/

#include <stdio.h>int main(void) {

printf("This is my first C Program.\n");

printf(“ My name is Kanad Biswas.\n");

return 0;}

This is my first C Program. My name is Kanad Biswas.