CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and...
-
Upload
dana-wells -
Category
Documents
-
view
221 -
download
0
Transcript of CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and...
![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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/16.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/20.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/21.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/22.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/23.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/24.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/25.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/26.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/27.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/28.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/29.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062300/56649e575503460f94b5028c/html5/thumbnails/30.jpg)
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.