cs 101: arrays abhiram ranade. computers manipulate many objects given the position, masses, and...

30
CS 101: Arrays Abhiram Ranade

Upload: dana-wells

Post on 28-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

CS 101: Arrays

Abhiram Ranade

Page 2: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Computers manipulate many

objects Given the position, masses, and velocities of all

stars, where will they be after the 100 years?

Given information about all train routes in India,

say which are earning a profit.

Who gets the maximum marks in CS 101?

Thousands of objects : Do we need thousands of

names in our programs?

Page 3: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Arrays int a[1000]; : Reserves 1000 locations in

memory. The first location is referred to as a[0],

next as a[1], and so on till a[999].

In one simple statement we have effectively

defined 1000 variables!

a: array, a[0], a[1], ..., a[999]: array elements

index of an element: what appears inside [ ].

float b[500]; // array of 500 float elements.

Page 4: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Array Elements

Everything that can be done with a usual

integer variable can be done with elements of

an array declared as int ... [...]:

cin >> a[0]; // reads from keyboard into a[0]

a[7] = 1; // b gets the value of a[7].

int b = a[7];

a[i*2] = j; // index: arithmetic expression OK

Page 5: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Example: Mean and Variance

Suppose we have a sequence of numbers:

x1, x2, x3, x4, ..., xn

and we want their mean and variance.

How do we represent this on a computer:

Mathematical sequences ==> Arrays

Page 6: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Mean and Variance: Contd.

Mean = sum of numbers/number of numbers.

Variance =

Page 7: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Example: Mean and Variance

int sqtbl[100];

for(int i=0; i<100; i++){

sqtbl[i] = i*i;

}

for(int i=0; i<100; i++)

cout << i << “ : “ << sqtbl[i] << endl;

Page 8: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Another way

int sqtbl[100];

sqtbl[0] = 0;

for(int i=0; i<100; i++)

sqtbl[i] = sqtbl[i-1] + 2*i - 1;

// Using x2 = (x-1)2 + 2(x-1) + 1 = (x-1)2 + 2x - 1

Page 9: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Example 2: Scatter plot

Statisticians like to ask questions such as: is

high parental income likely to imply more years

of schooling for the children?

Such questions can answered by calculating

correlation.

They can also be understood by drawing

scatter-plots.

Page 10: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Scatter plots

Given the height, weight, and JEE rank say

which are likely to be related.

Height and weight will be related.

Height and rank will be unrelated.

Page 11: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

More detailed example

We have a system of n stars. Each has a given

position in space, velocity and mass. Using

Newton's law of gravitation we are to compute

the force on each star.

Eventual goal: Draw the particles on the screen

and simulate their motion.

Page 12: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Declarations

Page 13: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

What is a computer (contd.)

INPUT DEVICE: reads information from the

external world into memory. e.g. keyboard

OUTPUT DEVICE: sends data from memory to

the external world, e.g. the computer screen

CONTROL UNIT: Decides what the other units

are supposed to do.

Page 14: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Control Unit

Has a “program memory”, in which each cell

contains an “instruction”

“Add content of cell 35 and cell 45 and put the

result in cell 57 of data memory”

“Read one digit from the keyboard and put in

cell 63 of data memory.”

Page 15: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Control Unit (contd.)

Instructions are normally fetched from program

memory in order, i.e from cell 1, then cell 2, ...

“Jump” instructions:

“If data memory cell 35 has value > 0, then

fetch next instruction from cell 379 of program

memory.”

Page 16: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

3 Incarnations of a Program

Algorithm: “Add the product of the acceleration

and time to the velocity.”

C++ program: “V = U + AT;”

Program in computer memory:

Put product of locations 34,35 into location 37.

Put sum of locations 37,38 into location 39.

Page 17: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Algorithm

Informal description of what is to be computed.

Many algorithms might exist for solving the

same problem, e.g. GCD

factor numbers and pick common factors

Euclid's algorithm ... much faster!

How to design algorithms: this course

How to design fast algorithms: advanced

courses.

Page 18: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

C++ Programs from Algorithms

Main concern of our course.

Key questions:

How to represent real life objects such as bridges,

cars, pictures, games, language/sentences using

numbers?

How to describe the required computation?

How to reason about computations/programs?

Page 19: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

C++ Programs to Machine

Instructions “Automatically” done by a program called a

“compiler”. ... “g++”

“How does the compiler do it?” Advanced CSE

topic.

This course: how to use compilers.

Page 20: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Let us have some fun!

Your TA's have developed a “Turtle simulator”.

The turtle can move, and has a pen which can

be raised or lowered.

If the pen is down, and the turtle moves, a line

is drawn.

You get to write programs that control the turtle.

Page 21: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Program to draw a square

procedure tcontrol(){

forward(10);

right(90);

forward(10);

right(90);

forward(10)

right(90);

forward(10);

}

Page 22: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Procedures and Procedure Calls

turtlec : Procedure you wrote. The simulator

will execute this procedure after it sets up the

the window on the screen etc.

forward(10) : procedure you are asking to

execute, “procedure you called”.

Other numbers can be given instead of 10.

Turtle moves forwards that many steps.

right(90): turn right 90˚. Another procedure.

Page 23: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

How to run this program

Log in to an OSL computer.

Open an editor and type in the program call it

turtle1.cpp

Compile it.

Run it

Click the “X” to remove the picture.

Page 24: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

How to draw a square 2

procedure turtlec(){

repeat(4){

forward(10);

right(90);

}

}

Page 25: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

How to draw a polygon

procedure turtlec(){

cout << “How many sides?”

int nsides;

cin >> nsides;

repeat(nsides){

forward(10);

right(360/nsides);

}

}

Page 26: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Explanation of statements

“int nsides;” : Reserve a cell for me in memory

in which I will store some integer value, and call

that cell “nsides”.

“cout << ...”: Print that message on the screen.

“cin >> nsides;” Read an integer value from the

keyboard and put it in the cell nsides.

nside: Variable taking integer values. Can be

used wherever numbers may appear.

Page 27: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

More procedures for you

penup(): Causes the pen to be raised.

pendown(): Causes the pen to be lowered.

(): Unlike forward which needs one number to

decide how much forward to move, penup/down

does not need any numbers.

Page 28: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Repeat within repeat

repeat(4){

repeat(3){

forward(10); penup(); forward(10); pendown();

}

right(90);

}

Page 29: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Some necessary Mantras

Put following lines in your file

#include <iostream>;

using namespace std;

Allow you to use cin, cout

#include <turtlesim>;

Allow you to use the turtle simulator

Page 30: CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the

Homework

Draw a 5 pointed star.

Draw a 7 pointed star. How many different 7

pointed stars can you have?

Draw 7 identical circles, with 6 touching the

central circle. Circle = polygon of large no of

sides, say 360.

Draw 4x4 array of tiles slightly seperated from

each other.