Sei sulla pagina 1di 30

An Introduction to Programming

Through C++
Abhiram G. Ranade

Ch. 1: Introduction
Outline
• Introduction to computing
• Some simple programs
• Remarks on programming
• Spirit of the book
Computers need no introduction
• Cell phones, tablets, desktops, game consoles,
e-book readers, cameras, televisions contain a
computer
• Computers used to:
– Book train/plane/bus tickets
– Search the internet
– Predict the weather
–…
How computers work
• Whatever is to be done is expressed as a
sequence of mathematical calculations.
• Instruction sequence = Program
• By feeding different programs to a computer you
can make it do different things.
• This book tells you how to construct (“write”)
programs.
• Special notation is to be used to write programs:
“Programming Language”
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.
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.
Some questions you might have…
• Drawing pictures??? Isn’t programming about
doing calculations?
• Isn’t drawing pictures a bit frivolous?

Will get answered soon.


Program to draw a square
#include <simplecpp>
main_program{
turtleSim();
forward(10); right(90);
forward(10); right(90);
forward(10); right(90);
forward(10);
wait(5);
}
Explanation
• #include <simplecpp> : the program will use the
simplecpp package.
• main_program{ ..Your program goes here.. }
• turtleSim() : command to open a window, with turtle at
the center.
• forward(100) : command to move turtle forward by 100
pixels.
• right(90) : turn right 90˚. Similarly left.
• wait(5): do nothing for 5 seconds.
How to run this program
• Install simplecpp on your computer, by itself or using
the codeblocks IDE.
• Type in the program into a file. Call it turtle1.cpp
• “Compile” it:
– On unix run: s++ turtle1.cpp
– On code blocks: use compile button
• Execute it:
– On unix, run: ./a.out
– On code blocks: use run button
General Ideas
• C++ program = sequence of commands.
main_program{… written here …}
• Statement/command: terminated by “;”
• Arguments: additional data needed by
command to do its work.
– forward: how much forward?
– right: what angle?
– () if no arguments, e.g. turtleSim()
General Ideas (contd)
• Commands are executed from top to bottom,
left to right.
– Usually..
• “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.
A better way to draw a square
#include <simplecpp>
main_program{
turtleSim();
repeat(4){
forward(10); right(90);
}
wait(10);
}
Repeat Statement
• repeat (x) { yyy } : causes the statements
yyy inside { } to be executed x times.
• Each execution of yyy is called an iteration.
How to draw a polygon
#include <simplecpp>
main_program{
turtleSim();
cout << “How many sides?”;
int nsides;
cin >> nsides;
repeat(nsides){
forward(10); right(360.0/nsides);
}
wait(10);
}
Explanation of new statements
• int nsides; : “Reserve a cell for me in memory in which I
will store some integer value, and call that cell nsides”.
– You choose the name as you wish, almost!
– int : abbreviation of “integer”
• cout << … : Print that message on the screen.
• cin >> nsides; : Read an integer value from the keyboard
and put it in the cell nsides.
• 360.0/nsides : represents the value obtained after
dividing 360 by whatever is in nsides.
More commands/statements
• penUp(): Causes the pen to be raised.
• penDown(): Causes the pen to be lowered.
• 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…
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?)
Repeat within repeat
repeat(4){
repeat(3){
forward(10); penUp();
forward(10); penDown();
}
right(90);
}
Nested repeat statements
• Basic rule: repeat(x){ yyy } says:
– Statements yyy to be executed x times.
• If yyy contains repeat (w) {zzz}, then the
zzz is executed w times in each iteration of
outer repeat.
• Thus zzz will get executed x*w times.
• Program fragment on previous slide will draw
a square with dashed lines.
What will the following program do?
#include <simplecpp>
main_program{
cout << “a”;
repeat(5){
cout << “b”;
repeat(2){ cout << “c”; }
cout << “d”;
}
}
Answer
• The program will print
abccdbccdbccdbccdbccd
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.
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.
Pattern with 36 repetitions. You know
enough to write a program to do this!
Try it.
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.
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!
Spirit of the book
• 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!
Spirit of the book 2
• 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.
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 separated from each other.

Potrebbero piacerti anche