0% found this document useful (0 votes)
10 views20 pages

N Ntroduction TO Rogramming: Through

The document introduces a programming course focused on C++, outlining the course structure, programming environment, and initial coding activities. It emphasizes the importance of programming in various applications and provides an overview of the Simplecpp library for graphics programming. The lecture also includes practical coding examples and exercises for students to practice their skills.

Uploaded by

dhairya Patidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

N Ntroduction TO Rogramming: Through

The document introduces a programming course focused on C++, outlining the course structure, programming environment, and initial coding activities. It emphasizes the importance of programming in various applications and provides an overview of the Simplecpp library for graphics programming. The lecture also includes practical coding examples and exercises for students to practice their skills.

Uploaded by

dhairya Patidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

AN INTRODUCTION TO PROGRAMMING

THROUGH C++

with
Manoj Prabhakaran

Lecture 1
Introduction
and drawing some pictures

Based on material developed by Prof. Abhiram G. Ranade


Plan for Today
¥ About this course
¥ Getting started by coding up some simple programs

¥ In the lab, later today: setup your system and start coding!
About CS 101
Computer Programming
¥ Computers can be used to do a lot of things
¥ All the different apps in your phone, laptop, É
¥ All the online services you use are provided by large computers (servers)
working together (network)
¥ Computers embedded inside washing machines, car engines, É
¥ And then there are the ÒCyber-Physical SystemsÓ: Flying an aeroplane,
controlling a robot or a self-driving car, running a nuclear reactor, É

¥ But they need to be programmed to do so


¥ In this course: Introduction to programming
¥ No prior knowledge of computers expected
¥ Will lay the foundation for all sorts of uses later on
Programming Language
¥ Programs (instructions for a computer) can be quite complex
¥ Programming Languages: Provide a human-friendly way to write
the programs, while still being very precise
¥ Programming language used in this course: C++
1970s: C Programming Language (Dennis Ritchie)
developed for coding up the UNIX operating system
1980s: C++ (Bjarne Stroustrup) for ÒObject Oriented ProgrammingÓ
Standards: C++98, C++03, C++11, C++14, C++17, C++20, C++23, É

¥ Note: We will not learn about all the features of C++ in this course.
The programming environment
¥ Will setup in today's lab!
Initial weeks: C++ augmented with Simplecpp
Simplecpp is a C++ library developed in IITB
¥ Provides facilities convenient to learners
Ð Graphics programming Ð more fun!
Ð Some minor conveniences
Later weeks: Only C++
¥ We may continue to use Simplecpp graphics
The textbook
An introduction to programming through C++, Abhiram Ranade,
McGraw Hill Education, 2014.
¥ www.cse.iitb.ac.in/~ranade/book.html
¥ Available in physical and on-line bookstores
¥ Integrated with use of simplecpp

¥ Reading for todayÕs lecture: Chapter 1


Course Logistics
¥ Every week ¥ Evaluation
Ð Two lectures Ð Lab participation (10%)
Ð One lab Ð Two hand-written quizzes (7% x 2)
Ð Exercises for practice Ð Two lab quizzes (13% x 2)
¥ Learning Management System: Ð Mid-semester exam (20%)
Bodhitree Ð Final exam (30%)
Ð Lab submissions
Ð Slides will be available soon ¥ Course website:
after lecture https://www.cse.iitb.ac.in/~cs101/
Academic Integrity
Do not copy, consult or share in quizzes and exams!

Do not copy in lab submissions. You can consult references,


discuss ideas with peers, and ask TAs for guidance/hints.
But blindly copying defeats the purpose of the exercises.

All code submitted should be written by yourself

Any suspected cheating will be reported to D-ADAC


LetÕs Get Started!
Let us write some simple C++ programs
¥ The programs will draw pictures on the screen!
¥ Use ÒTurtle SimulatorÓ contained in simplecpp
Ð Based on Logo: A language invented for teaching
programming to children by Seymour Pappert et al.
Ð We give commands to a turtle to move around.
Ð Turtle has a pen, so it draws as it moves.
A simplecpp The first program
convenience
#include <simplecpp> ‣ ÒUse simplecpp facilitiesÓ
main_program { ‣ Main program, enclosed in { ... }
turtleSim(); ‣ Start turtle simulator
‣ Creates window + turtle at centre, facing
forward(100); right(90); right
forward(100); right(90); ‣ forward(n) :
forward(100); right(90); ‣ Move the turtle n pixels in the direction it is
currently facing.
forward(100); right(d) :

getClick(); ‣ Make turtle turn d degrees to the right.
} ‣ getClick() :
‣ Wait for a click.
How to run this program Demo

Using a C++ Compiler Instruct your OS (e.g.,


Using an Editor
E.g., g++, clang++, Linux kernel, macOS,
E.g. Vim, Emacs,
Microsoft Visual C++ Windows) to execute.
Notepad, TextEdit,
Compiler, Intel C++ E.g., via a shell running
Sublime, …
compiler, … on a terminal

Write Text file Compile Binary file Run

if error
if error
A better way to draw a square
#include <simplecpp>
main_program{
turtleSim();
repeat(4){
forward(100);
right(90);
}
getClick();
}
A simplecpp
Repeat Statement convenience

¥ Syntax (i.e., the form):


repeat (n) { body }
¥ Here, body consists of one or more statements.
¥ Semantics (i.e., the meaning): Body should be executed n times.
¥ repeat is an example of a loop (will see more loop structures later)
¥ In a loop, each execution of the body is called an iteration
Drawing a polygon Demo

#include <simplecpp> Print to the screen


main_program {
turtleSim(); A variable called ÒnsidesÓ
whose value can be set or
cout << ÒHow many sides?Ó;
The type of the variable. changed later
int for ÒintegerÓ. int nsides;
cin >> nsides; Read from keyboard into the
variable nsides
repeat(nsides){
forward(100); Repeat ÒnsidesÓ times
right(360.0/nsides);
} In each iteration of the body,
getClick(); draw one side, and turn
An arithmetic operation
} enough to start the next side
More simplecpp commands
¥ left(A) : turn left A degrees. Equivalent to right(ÐA)
¥ penUp(), penDown(): Causes the pen to be raised, lowered
Drawing happens only if the turtle moves while the pen is low.
¥ hide(true), hide(false) : The pen indicator (triangle) is hidden, shown
¥ sqrt(x) : square root of x.
¥ sine(x), cosine(x), tangent(x) : x should be in degrees.
¥ sin(x), cos(x), tan(x) : x should be in radians.

¥ Also commands for arcsine, arccosine, arctangentÉ See the book.


Nested Loops
¥ Consider
repeat (n) { body }
¥ Suppose body itself has a loop within it:
repeat (n) { É repeat(m) { Inner body }É }

¥ In this example, Inner body will be executed m n times


¥ The rest of the outer body will be executed only n times
Drawing the axes Demo
#include <simplecpp>
main_program{
turtleSim(); Comment
repeat(4) { // in each of four directions
repeat(5) { // will mark 5 big steps, each 50 units long
right(90); forward(4); forward(-8); forward(4); right(-90); // a big tick mark
repeat(9) { One big step has 10 small steps, and 9 small ticks
forward(5); // a small step
right(90); forward(2); forward(-4); forward(2); right(-90); // a small tick mark
}
forward(5); // 10th small step.
}
forward(-250); // come back to the origin
right(90); // and turn to the next axis
}
hide(true); Indentation
getClick();
}
Exercises
¥ Draw a square of side ¥ Draw the outer ¥ Draw an n-point star
100 and inscribe a square using a
square inside it dashed line.

Ð Hint: There are two angles of


interest. One determines
how ÒpointyÓ the star is. The
other related to the first via
Ð Hint: Use PythagorasÕs Ð Can you do it for the inner
n: turtle turns left and right,
theorem to determine the square also? How will you
but when it finishes drawing
length of the side choose the dash-gap length?
itÕd have turned net 360o

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy