Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the."— Presentation transcript:

1 CS 101: Arrays Abhiram Ranade

2 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?

3 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.

4 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

5 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

6 Mean and Variance: Contd. Mean = sum of numbers/number of numbers. Variance =

7 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;

8 Another way int sqtbl[100]; sqtbl[0] = 0; for(int i=0; i<100; i++) sqtbl[i] = sqtbl[i-1] + 2*i - 1; // Using x 2 = (x-1) 2 + 2(x-1) + 1 = (x-1) 2 + 2x - 1

9 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.

10 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.

11 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.

12 Declarations

13 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.

14 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.”

15 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.”

16 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.

17 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.

18 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?

19 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.

20 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.

21 Program to draw a square procedure tcontrol(){ forward(10); right(90); forward(10); right(90); forward(10)‏ right(90); forward(10); }

22 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.

23 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.

24 How to draw a square 2 procedure turtlec(){ repeat(4){ forward(10); right(90); }

25 How to draw a polygon procedure turtlec(){ cout << “How many sides?” int nsides; cin >> nsides; repeat(nsides){ forward(10); right(360/nsides); }

26 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.

27 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.

28 Repeat within repeat repeat(4){ repeat(3){ forward(10); penup(); forward(10); pendown(); } right(90); }

29 Some necessary Mantras Put following lines in your file #include ; using namespace std; Allow you to use cin, cout #include ; Allow you to use the turtle simulator

30 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.


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

Similar presentations


Ads by Google