Você está na página 1de 40

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

Introduction-me

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

Introduction - the Course


Introduction to programming
for complete novices
Language Platform: C++ on
Linux
Will start with simple version
of C++ called simplecpp
3

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
4

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

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,
sandipg@cse.iitb.ac.in and me
(varsha@cse.iitb.ac.in)
LATER: we will make a form for
submitting concerns.
6

Enough Logistics.
Let's Start Learning
Programming!
7

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
8

"Applications" or "Apps"

Web
Browser

Audio editor

Applications
are
"programs"
Whatsapp

Games
9

A Computer Program
A program is a sequence of instructions that a
computer can "execute"
The programmer is the person who creates
sequence of instructions - i.e. "writes the program"

this

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

10

So Let's Start
Programming!
First program in a simplified
version of the "C++"
language- called simplecpp
11

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.

12

Get Started
Open a file for
editing
Write turtleSim() in
the main_program
Compile-andexecute
A red triangle - our
"turtle" should be
seen facing east

#include<simplecpp>
main_program {
turtleSim();
wait (10);
}

13

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

14

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

15

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"

16

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

17

Demo
Let us see the execution of the
program
Or see-it- yourself (if watching video)

18

Explanation
the program will use the simplecpp
package.

#include <simplecpp>
main_program{
turtleSim();

Your commands within these braces


{...}package.

forward(200); right(90);
forward(200); right(90);

Start the turtle simulator (open a


window)
Move forward 200 units

forward(200); right(90);
forward(200);
wait(20);
}

Turn right 90 degrees

Do nothing for 20 seconds


Program exits and window closes
19

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

General Ideas
#include <simplecpp>

This sequence of commands in C++ is


the program

main_program{
turtleSim();
forward(200); right(90);
forward(200); right(90);
forward(200); right(90);
forward(200);
wait(20);

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"

}
21

General Ideas (contd)


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

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

Our next program: draw an


octagon
#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);
}

Commands
seem quite
repetitive?
There's a
better way!

23

A better way ...


#include <simplecpp>

"repeat" statement:

main_program{

repeat (n) {

turtleSim();
repeat(8){
forward(100);
right(45);
}
wait(10);
}

some commands
}
is a command that can be
compiled by simplecpp
The instructions within {...}
are repeated n times
Each execution is called an
iteration
24

How to draw a polygon


#include <simplecpp>

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!

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

25

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 26
pass this as an argument to this command

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

27

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

28

Repeat within repeat


repeat(4){

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

right(90);
}
29

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

Nested repeat statements


repeat(4){

repeat(3){
forward(200); penUp();

It will draw a
square with
dashed lines.

forward(200);
penDown();
}

right(90);
}
31

What will the following program do?


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

Answer
The program will print
abccdbccdbccdbccdbccd

33

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

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

Pattern with 36 repetitions. You know


enough to write a program to do this! Try it.

36

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

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!

38

Spirit of the course


Learn C++ statements/concepts. We
have covered a lot of ground in this
chapter, even if it doesnt 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!
39

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

Você também pode gostar