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

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
COSC 120 Computer Programming
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Complexity Analysis (Part I)
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
High-Level Programming Languages: C++
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
CS 101: Numerical Computing Abhiram Ranade. Representing Integers “int x;” : reserves one cell in memory for x. One cell: “One word” has 32 capacitors.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
CS 101: “If” and Recursion Abhiram Ranade. This week’ lab assignment: Write a program that takes as input an integer n and a sequence of numbers w1,w2,...,wn.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
CS 101: Introduction to computer programming and utilization Abhiram Ranade.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Basic Elements of C++ Chapter 1.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Working with arrays (we will use an array of double as example)
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
A Sample Program #include using namespace std; int main(void) { cout
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Engineering Problem Solving With C An Object Based Approach
What Actions Do We Have Part 1
Computing Fundamentals
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Arrays & Functions Lesson xx
Engineering 1020 Introduction to Programming
Today in CS161 Week #3 Learn about… Writing our First Program
Programming Funamental slides
Programming Funamental slides
What Actions Do We Have Part 1
Chapter 2 part #1 C++ Program Structure
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

CS 101: Arrays Abhiram Ranade

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?

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.

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

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

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

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;

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

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.

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.

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.

Declarations

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.

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

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

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.

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.

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?

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.

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.

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

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.

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.

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

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

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.

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.

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

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

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.