• No results found

Information representation in a computer

N/A
N/A
Protected

Academic year: 2022

Share "Information representation in a computer"

Copied!
44
0
0

Loading.... (view fulltext now)

Full text

(1)

CS101

Computer programming and utilization

Dr Deepak B Phatak

Subrao Nilekani Chair Professor

Department of CSE, Kanwal Rekhi Building

IIT Bombay

Lecture 2, Introduction to programming Friday 29 July 2010

IIT BOMBAY

Dr Deepak B Phatak Lecture 2 Rules of the game 1

(2)

IIT BOMBAY

Overview

• Computer – the real machine

• An example of Problem solving

• some rules of the game for C++,

• Naming Conventions

• Data Types

• Expression evaluation

• Course organization for CS101

• Announcements

• Special lectures

• Lab Organization

(3)

IIT BOMBAY

• Digital circuits capable of representing two states

• off and on (like a switch), 0 and 1

• Information represented using these 2 symbols

• Called Binary digITs, or BITs

Dr Deepak B Phatak Lecture 2 Rules of the game 3

Information representation in a computer

(4)

IIT BOMBAY

A simplified computer architecture

BUS MEMORY

PROCESSOR

TERMINAL

(5)

IIT BOMBAY

0 is 0 1 is 1

2 is 10 (This is not “Ten”) 3 is 11

4 is 100 5 is 101 etc.

Dr Deepak B Phatak Lecture 2 Rules of the game 5

Decimal and Binary numbers

(6)

IIT BOMBAY

Names for some numbers in base 2

• 210 is called 1Kilo (1024 ), Approximate decimal value 103

• 220 is called 1Mega (1024 kilo) ~ decimal value 106

• 230 is called 1Giga (1024 Mega) ~ decimal value 109

• 240 is called 1Tera (1024 Giga) ~ decimal value 1012

• 250 is called 1Peta (1024 Tera) ~ decimal value 1015

(7)

IIT BOMBAY

Internal representation ...

• Value range depends upon the number of bits allocated to the location containing that value

• The smallest addressable unit of memory is a Byte (8 bits)

smallest Value 00000000 [0]

Largest Value 11111111 [255]

Dr Deepak B Phatak Lecture 2 Rules of the game 7

(8)

IIT BOMBAY

Internal representation

• Value which can be stored in a byte is too small

• not useful in solving computational problems.

• computers process larger numerical values by using consecutive bytes. (2 , 4, 8, …)

(9)

IIT BOMBAY

Internal representation of signed integers

• 4 consecutive bytes (32 bits) together can represent a value ranging from 0 to 232 -1

• The first „bit‟ of these four bytes may be used to represent sign: 0 is +, 1 is -

• The range of values which can be represented Max positive number: + 231-1

Min negative number: - 231-1

Dr Deepak B Phatak Lecture 2 Rules of the game 9

(10)

IIT BOMBAY

Internal representation …

• actual representation is in 2‟s complement form

• Four bytes can represent - 231 to + 231-1

[ Decimal -2147483648 to 2147483647]

• 2 bytes: -32768 to 32,767

• 1 Byte: - 128 to 127

Unsigned Integer in one byte will range from 0 to 255

(11)

IIT BOMBAY

Types of values

Dr Deepak B Phatak Lecture 2 Rules of the game 11

• Circuits are designed to interpret and process different type of values

• Symbols

• Fractional, large and small values

(12)

IIT BOMBAY

Representing symbols

• Representation of symbols

• 256 different symbols by 1 Byte

• ASCII Code used for text symbols 43 „+‟

77 „M‟

109 „m‟

110 „n‟

61 „=‟

49 „1‟

32 SP [ A blank space in appearance]

(13)

IIT BOMBAY

Representing Text

Dr Deepak B Phatak Lecture 2 Rules of the game 13

• Text is represented by a sequence of symbols Sequence 77 43 109 will represent M+m Sequence 109 32 43 32 77

What does the following sequence represent?

112 32 61 32 109 32 110 59 10 77 61 49 10

(14)

IIT BOMBAY

Back to Programming in C++

• We will use only decimal numbers and normal text symbols in our programs and in I/O operations

• We presume that the C++ compiler can generate machine instructions to handle conversions

(15)

IIT BOMBAY

Fractions and Floating point representation

Dr Deepak B Phatak Lecture 2 Rules of the game 15

• 0.765, -1230000000, 0.00000832

• Equivalent of scientific notation is used [0.832E-5]

• 0.832 is called the mantissa

• -5 is the exponent

• Space is allocated for these two components in a single location comprising multiple bytes

(16)

IIT BOMBAY

Problem: Find the cost of painting tanks

• A large Indian petroleum company has erected a number of

cylindrical tanks on a rectangular field. External surfaces of these are to be painted, including the flat circular cover on the top.

• . . . .

• Dimensions and the number of tanks are given as input,

• For a given price of painting work per sq. meter, find the cost

1 2 N h

r

(17)

IIT BOMBAY

Problem Analysis

Lecture 2 Rules of the game

Dr Deepak B Phatak 17

• Surface area of a tank to be painted (radius r and height h)

= area of the outer surface + area of the top circular cover

• Area of surface = 2πrh, Area of circular cover = πr 2

• For N tanks, the total area would be N(2πrh + πr 2 )

(18)

IIT BOMBAY

Design of the algorithm

• Identification of computational objects

• Values which will be given as input

• Radius r

• Height h

• Number of tanks Ntanks

• Price for painting price

• Other values needed

• Value of pi pi

• Output to be computed

• Cost of painting cost

(19)

IIT BOMBAY

Design …

Dr Deepak B Phatak Lecture 2 Rules of the game 19

• Our algorithm will

• First read the input values

• Calculate the cost using the formula price x N(2πrh + πr 2 )

• Output the value of cost

(20)

IIT BOMBAY

Program to calculate cost of painting

/* PaintingCost.cpp, written for cs101, August 2011 */

#include <iostream>

using namespace std;

int main() {

float r, h, pi = 3.14159, price; int Ntanks; float cost;

cout << “give the radius and height of cyllinder: ”; cin >> r >> h;

cout << “give number of tanks: ”; cin >> Ntanks;

cout << “give price per sq meter for painting: ”; cin >> price;

cost = price * Ntanks * (2 * pi * r * h + pi *r *r);

cout << “Cost of painting is: ” << cost;

return 0;

(21)

IIT BOMBAY

Names

• Names (called identifiers) can consist of letters, digits, and underscore (_)

• A name must not contain a blank or tab

• It must begin with a letter or underscore

• Names can be of any length

• first 31 symbols should be unique

• Keywords in C++ cannot be used for our names namespace, using, return, …

• Meaningful names must be chosen by us.

Dr Deepak B Phatak Lecture 2 Rules of the game 21

(22)

IIT BOMBAY

What is there in a name?

• Roll number of a student

• Value of temperature in degree Centigrade

• Marks obtained in a quiz

(23)

IIT BOMBAY

Names …

Dr Deepak B Phatak Lecture 2 Rules of the game 23

• Marks obtained in the semester-end examination

(24)

IIT BOMBAY

Objects in C++

• In C++, names are used to represent „objects‟

• Each object can have a value of certain type

• Thus every name must have an associated type

• Various constants used are objects of a certain type

(25)

IIT BOMBAY

Types of values in C++

• C provides for several types of values

char, int, float, double, void (valueless) bool, and wchar_t

• we will only look at integer and floating point

• Types associated with numerical values

• int type is used to represent integer numbers

• float type used to represent fractional numbers

• Also used to represent very large integers

Dr Deepak B Phatak Lecture 2 Rules of the game 25

(26)

IIT BOMBAY

Numerical values

• We write numerical values in some standard way.

For example:

25 -7389 1240000000 87.669 3.14159 -0.0000123

• Very large and very small values are written in

exponential notation [a mantissa and an exponent]

1.24E9, -1.23E-5, 6.023E23, 124.0E7, 0.124E10, etc.

• C++ permits us to write such constant values in our programs, and also accepts these as input.

(27)

IIT BOMBAY

Declarations of object names

• Class or type of name for any object must be explicitly declared in a program

• The compiler will use such declaration to allocate appropriate memory based on the type

• int i, j, Count, NumberOfTanks,

• There exist other „qualifiers‟ to int type

• short (2 bytes), unsigned (no sign bit),

• long (4 or 8 bytes)

Dr Deepak B Phatak Lecture 2 Rules of the game 27

(28)

IIT BOMBAY

Integer Values

• Decimal numbers

• Must begin with a nonzero digit, except when value is 0

5 0 253 -1415261

• The value must be within the prescribed range

• 0ctal (base 8) numbers

• written beginning with 0

• 045 0100 07775

• Hexadecimal (base 16) numbers

• Written beginning with 0x

(29)

IIT BOMBAY

Floating point values

• Standard decimal notation 4.7816 -0.00046

• exponential notation with mantissa and exponent 1.2e9 -2E20

• Mantissa precision is 6 digits minimum

• exponent range is -37 to +37 minimum

• Decimal floating point values used in our programs

Dr Deepak B Phatak Lecture 2 Rules of the game 29

(30)

IIT BOMBAY

Floating point objects

• Defined using keyword float

float x, y, val, radius, circle_area;

• 4 bytes are allocated

• 24 bits used for mantissa, 8 used for exponent

• „double‟ is like a long float

• 8 bytes, with larger mantissa and exponent

• Example:

double CircleArea, Savings_Account_Balance;

(31)

IIT BOMBAY

Character objects

Dr Deepak B Phatak Lecture 2 Rules of the game 31

• Character values are written as

„P‟ „*‟ „ ‟

„\t‟ „\n‟

• There exists a type char for character objects (one byte – stores ASCII codes)

• Declaration of character objects int sym, first_letter_of_name;

(32)

IIT BOMBAY

Assignment operation

int m;

m = -35;

• Symbol „=„ is called the assignment operator

• the value on the right hand side (rhs) of this

operator is stored in the location for the object m named on the left hand side (lhs)

(33)

IIT BOMBAY

Expression

• RHS need not be a single value

• It can be an expression int m, n;

n = 3;

m = 35 * n - 167;

• Expression on rhs is evaluated. The resulting single value is called value of the expression.

• This value which is assigned to the object on LHS

Dr Deepak B Phatak Lecture 2 Rules of the game 33

(34)

IIT BOMBAY

Expression evaluation

• Arithmetic operators

- + * / % (modulo operator, it gives remainder)

• Precedence rule

* / % have higher precedence than - + For example: a+b*c

[b*c calculated first, result added to a]

• Associative rule

• Within the same precedence, associativity is left to right x*y/z

[x*y calculated first, result divided by z ]

(35)

IIT BOMBAY

Operands are objects with different types

Dr Deepak B Phatak Lecture 2 Rules of the game 35

• If both operands are of same type

• result value is of the same type 9.0/2.0 will result in 4.5

9/2 will result in 4

• When this operation is carried out. the resulting value replaces this part of expression and the evaluation of remaining expression continues.

• possible problems in expression evaluations

• while two values participating in an operation can individually be within the stipulated range, the result may not be in the range

• In such a case, an erroneous value will result

(36)

IIT BOMBAY

Type conversion during assignment

int i, j; float x, y;

i=-25; x=2.147;

j=3.2; // float values converted to int // (fractional part is truncated) j=1.24E30;

Complete loss of precision, value too large y=29;

value converted to float, equivalent to .29E2 y=123456789;

(37)

IIT BOMBAY

Parenthesis

• parenthesis override any other precedence.

m + n * p (m + n) * p

• Eexpressions and their C++ equivalent (a+b)x (a + b) * x

(1/(x+1/(x+1)))

1

x + 1 x + 1

Dr Deepak B Phatak Lecture 2 Rules of the game 37

(38)

IIT BOMBAY

Conversions

• Conversion occurs during expression evaluation.

• if both operands are of same type;

• result is of that type

• If only one of the two operands is of int type, then it is converted to float before evaluation. The result is float type

(39)

IIT BOMBAY

Assignment statement revisited

• general form of an assignment operation is name = expression;

y = 3.14159*r*r+h*w -2*3.14159*rdash;

• The entire assignment operation is also treated logically as an expression, whose value is same as the one finally assigned to the name on lhs

• m=n=p=10;

• This is same as m=(n=(p=10));

Dr Deepak B Phatak Lecture 2 Rules of the game 39

(40)

IIT BOMBAY

Quiz

• What will be the values printed by the following program?

int main (){

int m, n; float x, y;

m=x=n=y=8.79;

cout << m << x << n << y;

(41)

IIT BOMBAY

Quiz

• What is the value output by this program?

float x, y, avogadro;

avogadro = 6.023E23; y = 7.3463;

x=y-8.6597+avogadro;

Dr Deepak B Phatak Lecture 2 Rules of the game 41

(42)

IIT BOMBAY

CS101 organization

• 2 lecture sessions and 2 lab hours per week

• Each student is expected to work for an additional 4 hrs per week for this course

• This additional work will include

• General reading

• Problem solving and programming practice

• Home assignments

• Discussions with other students and TAs

• Preparing reports/diaries as required

• There will be a course home page which will be activated on Monday morning, in addition to the moodle interface

http://www.cse.iitb.ac.in/~cs101

(43)

IIT BOMBAY

Lecture 2 Rules of the game

Evaluation pattern

• Evaluation of students‟ performance on various aspects will be conducted continuously during this course

The percentage weight for each compponent is

• Lab assignments 10

• Quizzes 15

• Mid-semester examination 20

• Course project 25

[5 marks reserved for self evaluation with peer review]

• End-semester examination 30

• Based on the total score out of 100, letter grades will be awarded. Score of 40 is required to secure a pass grade (DD)

Dr Deepak B Phatak 43

(44)

IIT BOMBAY

Warning

• Using unfair means for scoring marks is considered an unpardonable activity in our course

• There may be occasions when you are tempted

• to use a short cut

• You may copy an assignment, for example

• Any such unfortunate incident will result in an immediate award of a fail grade (FR)

• Apart from being ethically correct, it is safer to submit an incomplete or even a blank assignment, or a program

Kindly avoid such incidents

References

Related documents

Transformation into 2-player partial observation game with B¨ uchi winning condition. I exponential blowup of

„ Native American Images - American Indian North America Tribe Map.. „ American Indian North America

Course Computer Application Extension Lecture..

Of those who have used the internet to access information and advice about health, the most trustworthy sources are considered to be the NHS website (81 per cent), charity

In this work, techniques based on CGR for nucleotide sequence analysis are conceived, which include a fast algorithm for identifying all local alignments between two

The candidates bearing the following Roll Numbers are declared to have passed the 2ND SEMESTER B.A.. College 198. Centre

CS230: Digital Logic Design and Computer Architecture.. Lecture 6: MIPS

* In the event of any discrepancy detected by any candidate, he/she has to bring it to the notice of the Controller of Examinations within 1 (one) month from the date of declaration