Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction-me
I am Varsha Apte
Professor in the
Department of Computer
Science and Engineering
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
Enough Logistics.
Let's Start Learning
Programming!
7
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
10
So Let's Start
Programming!
First program in a simplified
version of the "C++"
language- called simplecpp
11
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
Execute it:
On unix, run: ./a.out
On code blocks: use run button
14
penDown()
Will "draw" while moving
15
16
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();
forward(200); right(90);
forward(200); right(90);
forward(200); right(90);
forward(200);
wait(20);
}
General Ideas
#include <simplecpp>
main_program{
turtleSim();
forward(200); right(90);
forward(200); right(90);
forward(200); right(90);
forward(200);
wait(20);
}
21
Compiling the
program: translating it
into a form that your
computer can
understand.
More on this in chapter 2.
Commands
seem quite
repetitive?
There's a
better way!
23
"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
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);
}
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(3){
forward(200); penUp();
forward(200);
penDown();
}
right(90);
}
29
repeat(3){
forward(200); penUp();
It will draw a
square with
dashed lines.
forward(200);
penDown();
}
right(90);
}
31
Answer
The program will print
abccdbccdbccdbccdbccd
33
36
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