Chap 1 Abhiram Ranade

40
1 CS 101 Computer Programming and Utilization Lecture 1: Our first program Autumn 2015 Varsha Apte Slides based on Book Slides Reference: Chapter 1 ITP-C++ by Abhiram Ranade

description

Chapter 1 lecture notes by Varsha Apte based on the book Introduction to simplecpp by Abhiram G. Ranade. Used in IIT Bombay for the course CS101.

Transcript of Chap 1 Abhiram Ranade

1

CS 101 Computer Programming and

UtilizationLecture 1: Our first program

Autumn 2015Varsha Apte

Slides based on Book SlidesReference: Chapter 1

ITP-C++ by Abhiram Ranade

2

Introduction-me

•I am Varsha Apte•Professor in the Department of Computer Science and Engineering

3

Introduction - the Course•Introduction to programming

for complete novices•Language Platform: C++ on

Linux•Will start with simple version

of C++ called simplecpp

4

Course Details• Lectures: twice a week, 1.5 hours

each• LABS: ONCE a week AT NIGHT;

830pm-1030pm in OSL (Maths Department)

• In batches of 90-100 students per night

• You will write programs during lab time

5

Evaluation• Theory exams and quizes

– Homework will be given, but just for practice

• Labs, Lab quizes and Lab exams– Extra practice assigments will also be

given • Individual Mini-project/large project

(at the end of the semester)• Details IN NEXT CLASS. NOT TODAY.

PLEASE HOLD YOUR QUESTIONS.

6

Contact In Case of Problems• FOR NOW: Find your STA/JTA from

the lab batch information.• In case of ANY problems send e-mail

to JTA with cc to STA, [email protected] and me ([email protected])

• LATER: we will make a form for submitting concerns.

7

Enough Logistics.Let's Start Learning

Programming!

8

Computers and You• So far almost all of

you have used a computer– Desktop– Laptop– Smartphone (is a

computer)• Computer: an

electronic device that can be "programmed" to do multiple functions

9

"Applications" or "Apps"

Applications are "programs"

Web Browser

Audio editor

Whatsapp Games

10

A Computer Program• A program is a sequence of instructions that a

computer can "execute"– The programmer is the person who creates this

sequence of instructions - i.e. "writes the program" • So far you have been a user of these programs• In this course you will learn how to be the

programmer of these programs or applications, or apps. No we will not learn Android App programming. They are just an example. But you will learn enough principles of programming that learning to program in a slightly different environment will be easy

11

So Let's Start Programming!

First program in a simplified version of the "C++"

language- called simplecpp

12

Our first program•Use “Turtle Simulator*” contained in simplecpp•We “drive” a “turtle” on the screen!•To drive the turtle you write a C++ program.•Turtle has a pen, so it draws as it moves.•Basic goal: draw interesting, intricate pictures.

*From Logo: A language invented for teaching programming by Seymour Pappert et al.

13

Get Started• Open a file for

editing• Write turtleSim() in

the main_program• Compile-and-

execute• A red triangle - our

"turtle" should be seen facing east

#include<simplecpp>main_program {

turtleSim();wait (10);

}

14

How to run this program•Download and Install simplecpp on your computer, by itself or using the codeblocks IDE.

–URL: http://www.cse.iitb.ac.in/~ranade/simplecpp/ •Type in the program into a file, save it. Call it first_program.cpp•“Compile” it:

–On unix run: s++ draw_square.cpp–On code blocks: use compile button

•Execute it:–On unix, run: ./a.out–On code blocks: use run button

15

Some Instructions that TurtleSimulator Can Execute

• penUp()– Will not "draw" while moving

• penDown()– Will "draw" while moving

• forward (x): Move forward x pixels– E.g. forward(50) moves the turtle

forward 50 pixels• right (x): turn right by x degrees• left(x): turn left by x degrees• wait (x): do nothing for x seconds

16

Your First Task• With these instructions, make the

turtle move in such a way that we will draw a square of side length 200

• Note: by default, in the beginning, the turtle faces towards "east" and the "pen" is "down"

17

Program to draw square• Instructions:

– forward(x)– right (x)– left(x)– penUp()– penDown()– wait(x)

#include <simplecpp>main_program{ turtleSim(); forward(200); right(90); forward(200); right(90); forward(200); right(90); forward(200); wait(20);}

18

Demo• Let us see the execution of the

program– Or see-it- yourself (if watching video)

19

Explanation#include <simplecpp>

main_program{

turtleSim();

forward(200); right(90);

forward(200); right(90);

forward(200); right(90);

forward(200);

wait(20);

}

the program will use the simplecpp package.

Your commands within these braces {...}package.

Start the turtle simulator (open a window)

Move forward 200 units

Turn right 90 degrees

Do nothing for 20 seconds

Program exits and window closes

20

The C++ programming language

• Designed by Bjarne Stroustrup, 1980s. Derived from the C programming language.

• Substantial evolution. Still continues.• Early part of the book: C++

augmented with a package called simplecpp

• Simplecpp: More fun and easier to use than bare C++. Built-in graphics.

21

General Ideas#include <simplecpp>

main_program{

turtleSim();

forward(200); right(90);

forward(200); right(90);

forward(200); right(90);

forward(200);

wait(20);

}

This sequence of commands in C++ is the program

Commands or "statements" terminated by semicolon ";"

Some commands need additional information called "arguments" .

"90" is the argument to the command "right" "200" is the argument to the command "forward"

22

General Ideas (contd)

• “Compiling” the program: translating it into a form that your computer can “understand”.– More on this in chapter 2.

• Executable file: result of compilation. On unix the file has the name a.out by default.

#include <simplecpp>

main_program{

turtleSim();

forward(200); right(90);

forward(200); right(90);

forward(200); right(90);

forward(200);

wait(20);

}

Commands are, generally, executed from top to bottom, left to right.

23

Our next program: draw an octagon

• Commands seem quite repetitive?

• There's a better way!

#include <simplecpp>main_program{ turtleSim(); forward(100); right(45); forward(100); right(45); forward(100); right(45); forward(100); right(45); forward(100); right(45); forward(100); right(45); forward(100); right(45); forward(100); wait(10);}

24

A better way ...#include <simplecpp>main_program{ turtleSim(); repeat(8){ forward(100); right(45); } wait(10);}

"repeat" statement:repeat (n) {

some commands }is a command that can be compiled by simplecppThe instructions within {...} are repeated n timesEach execution is called an iteration

25

How to draw a polygon

• We have removed repetition of a command

• Now can we generalize?

• To drawing a polygon of any number of sides??

• Yes! requires use of "variable"s!

#include <simplecpp>main_program{ turtleSim(); cout << “How many sides?”; int nsides; cin >> nsides; repeat(nsides){ forward(10); right(360.0/nsides); } wait(10); }

26

Explanation#include <simplecpp>main_program{ turtleSim(); cout << “How many sides?”; int nsides; cin >> nsides; repeat(nsides) { forward(200); right(360.0/nsides); } wait(10); }

Print the sentence within the quotes on the screen

Tell the computer: I want a reserved space in your memory where I can store an integer (int). I will refer to this integer by the label "nsides"

Read the number that the user types and store it into the space in memory labeled "nsides"Use the integer written in the space in memory which is labelled "nsides"

Divide the number 360 by the number stored in the area labeled "nsides" and pass this as an argument to this command

27

More commands/statements•sqrt(x) : square root of x.•sine(x), cosine(x), tangent(x) : trigonometric functions, x is in degrees.•sin(x), cos(x), tan(x) : x is in radians.•Also commands for arcsine, arccosine, arctangent…

28

Remarks• You can use commands without

worrying about how exactly they do their work. – sqrt(17.35) : will get calculated

somehow.– forward(100) : may require calculation

which will happen. (What calculation?)

29

Repeat within repeatrepeat(4){

repeat(3){ forward(200); penUp(); forward(200); penDown();}right(90);

}

30

Nested repeat statements• Basic rule: repeat(n){ yyy } means

– Statements yyy to be executed n times.• If yyy contains repeat (m) {zzz}, then the

zzz is executed m times in each iteration of outer repeat.

• Thus zzz will get executed n X m times.• What will the program fragment on previous

slide do?

31

Nested repeat statements• It will draw a

square with dashed lines.

repeat(4){repeat(3){ forward(200); penUp(); forward(200); penDown();}right(90);

}

32

What will the following program do?

#include <simplecpp>main_program{ cout << “a”; repeat(5){ cout << “b”; repeat(2){ cout << “c”; } cout << “d”; }}

33

Answer•The program will printabccdbccdbccdbccdbccd

34

Remarks: Some terms• “Control is at statement w”: Computer is currently

executing statement w.• “Control flow”: The order in which statements get

executed.– Execution starts at top and goes down. Retraced if there

is a repeat statement.• Variable: used for storing data.

– Computer memory: blackboard– Variable: Region on the board in which you can write a

value.– Variables have names, e.g. nsides. We can use the

name to refer to the value written in the variable.– Details later.

35

Why picture drawing?• Picture drawing requires calculation:

– e.g. 360.0/nsides.• “Draw a triangle of side length 3, 4,

5.”– You will need to do trigonometric

calculations to decide how much to turn.• More interesting calculations will be

needed to draw more interesting drawings.

36

Pattern with 36 repetitions. You know enough to write a program to do this! Try it.

37

Why picture drawing (contd)• Interesting calculations of any kind (not

necessarily picture drawing) contain patterns. The pattern in the calculations must be mirrored by patterns in program.– example: if a certain sequence of computations

is repeated, then do not repeat it textually, but put it in a repeat statement.

– very important for large programs.• Interesting pictures contain patterns. Get

some practice at representing patterns in your programs.

38

More reasons• Graphical input and output is very

convenient and useful.– “A picture is worth a thousand words.”– “Data Visualization” : upcoming area of

CS• Drawing is fun!

39

Spirit of the course• Learn C++ statements/concepts. We

have covered a lot of ground in this chapter, even if it doesn’t seem so.

• Learn how to express problems you want to solve using C++.

• Goal: if you can solve a problem by hand, possibly taking an enormous amount of time, by the end of the book, you should be able to write a program for it.

• Learn new ways of solving problems!

40

Spirit of the course• Do not be afraid of using the

computer.• “What if I write xyz in my program

instead of pqr?” : Just do so and find out.– Be adventurous.

• Exercise your knowledge by writing programs – that is the real test.