* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download CS101 Spring 2012 LHC32
Survey
Document related concepts
Transcript
Computer Programming
and Utilization
CS101 Spring 2012
Soumen Chakrabarti
with
CS101 TAs and Staff
First steps
http://www.cse.iitb.ac.in/~cs101/
Fill out survey when survey link appears
Find out your lab batch
Eighty percent of success is showing up.
Woody Allen
CS101 2012.1
Places and times
Lectures (two sections)
• Slot 10: Tu, F 2—3:30
• Slot 6: W, F 11—12:30
Labs (OSL)
• Four evenings a week, plus one makeup
Tutorial
• Depends on interest
M
T
W
Th
F
S10 (160)
S6 (160)
(80)
Make-up lab
CS101 2012.1
“The management”
cs101.iitb@gmail.com
Web site STAs
Instructor
STA
STA
STA
Assignment STAs
STA
STA
cs101stas@cse.iitb.ac.in
JTAs
JTAs
JTAs
JTAs
JTAs
cs101jtas@cse.iitb.ac.in
Makeup lab.
Tue lab.
batch
Wed lab.
batch
CS101 2012.1
Thu lab.
batch
Fri lab.
batch
Do not use personal email
Assessment
Midterm exam and quizzes
• Two to three exams
• Total 25—35%
Final exam
• 30—40%
Some lab. sessions will be graded
• 15—20%
Project and viva
• 5—10%
CS101 2012.1
PC building blocks: motherboard
CPU with
cooling fan
Fast
electronic
memory
Magnetic
disk data
connectors
CS101 2012.1
Storage and peripheral devices
Keyboard
Display
Rotating
magnetic
platters
Record/play
head on arm
Data cable between
disk and motherboard
CS101 2012.1
Simplified abstract view
Arithmetic
and logic unit
Register 0
Register 1
Register 2
…
Address
RAM location 0
RAM location 1
RAM location 2
…
Reserved for
display
Data
CPU
Reserved for keyboard
Program that tells the
CPU what to do and
how to do it
Random access
memory (RAM)
CS101 2012.1
Rules of the game
A register or a RAM location can store
exactly one value at any time
Writing into a register or RAM location (or
reading into it from the keyboard) destroys
the earlier value
Outputting a register or RAM location to the
display is done by copying to the area
reserved for the display; this does not
destroy the source value
Accessing any RAM location is equally fast
CS101 2012.1
E.g.: Centigrade and Fahrenheit
C/5 = (F-32)/9
Painful to say “register 5” and “RAM location 43” so
we will use shorter names
Load input F from keyboard into R0
Load 32 into R1
Store R0R1 in R2
Load 9 in R3
“Assembly
language
Divide R2 by R3 and store in R4
programming”
Load 5 in R5
Multiply R5 by R4 and store in R6
Painful!
Output R6 to the display
CS101 2012.1
High level programming language
•Input F from keyboard
•Set C to 5*(F-32)/9
•Output C to display
Programmer need not keep mental map of
which register and RAM location contain
values of what symbolic variables
Compiler or interpreter automatically
translates from above format to register and
RAM location manipulations we saw
CS101 2012.1
Saying it in C++
Variable
Procedure name
type
main() {
Variable declaration
float fahrenheit;
cin >> fahrenheit;
float centigrade = 5*(fahrenheit-32)/9;
cout << centigrade;
Arithmetic
}
expression
Some details omitted
Save above source code to text file
Compile source code to executable file
Call executable from a shell command line
CS101 2012.1
The hardware-software stack
Your C++ executable program
Shell command line (bash)
Operating System (Linux)
Hardware
Ha
Your program accesses resources like CPU, keyboard,
display, through the lower layers of system software
CS101 2012.1
C++: More detail
Procedure name
Data
type
main() {
Variable declaration
float fahrenheit;
cin >> fahrenheit;
float centigrade = 5*(fahrenheit-32)/9;
cout << centigrade;
Arithmetic
}
expression
What is a procedure?
What are data types?
Where did cin and cout come from?
Rules of writing arithmetic expressions
CS101 2012.1
Procedure or function
Encapsulates a piece of computation and
gives it a name
• E.g. main is the default procedure that is run
when your program is executed from the shell
May accept input values stored in named
variables
• E.g. int max(int a, int b)
And return output value
• E.g. max(-3, 2) should return 2
CS101 2012.1
Data types
Computer memory is a 2d array of bits
• Eight columns (one byte or “B”)
• Rows depends on how much memory you have;
“1 GB” means 1,073,741,824 rows
• Hard disk is similar, only larger and slower
What programmers want
•
•
•
•
Integers, real numbers, complex numbers
Characters, strings of characters
Arrays, variable length lists, mappings
Windows, buttons, menus
Will study how many of these are
represented in memory
CS101 2012.1
Choosing names
C++ allows any sequence of characters A—
Z, a—z, 0—9, and underscore
Not starting with a digit
Up to some maximum number of characters
old_style_variable_name
newStyleVariableName (“camel case”)
turbineRPM or turbineRpm?
CS101 2012.1
cin and cout
“Console in” (keyboard) and “console out”
(display)
These variables are not defined magically
To use them, must prefix our C++ code with
instruction to include a header file like this:
#include <iostream>
The operating system and compiler work
together to let your code access the
keyboard and display through cin and cout
Not quite…
CS101 2012.1
Namespaces
We must write std::cin and std::cout
Two different Ravi Vermas in hostels 2 and 5
To avoid confusion, write as
H2::RaviVerma and H5::RaviVerma
Lets libraries written by different people avoid
variable and function name clashes
std is the “standard” namespace within
which C++ predefined variables and
functions are provided
CS101 2012.1
The std namespace and using
Tedious to type std:: in front of everything
If you are not using too many namespaces
simultaneously, you can choose a default by
saying
using namespace std;
before using things defined inside std.
CS101 2012.1
First complete source code
#include <iostream>
using namespace std;
main() {
float fahrenheit;
cin >> fahrenheit;
float centigrade = 5*(fahrenheit-32)/9;
cout << centigrade;
Save to file “cf.cc”
}
At your shell, type
g++ cf.cc
g++ compiler
Now run resulting file as
./a.out
CS101 2012.1
Executable a.out
Files
cf.cc and a.out are files
A file is a sequence of bytes
These bytes can be interpreted differently
depending on the applications that read or
write the files
• cf.cc is a text file to be written by a programmer
and read by the C++ compiler
• Any name ending in .cc or .cpp is ok
• a.out is an executable file to be run from the
shell command line
• You can rename this file as you wish
CS101 2012.1
Disk organization: Directories
A directory is a container that can contain
files and other directories
Files cannot contain directories
Tree hierarchy of directories
/
Root is “slash” /
Full path written as
home
usr
/usr/bin/g++
bin
bash
CS101 2012.1
g++
cs101
you
Compilation and execution summary
string
math.h
iostream
Source code cf.cc
g++ compiler
C/C++ execution environment
Operating system (Windows, Linux, Mac OS, …)
CPU
RAM
Disk
Keyboard
CS101 2012.1
Display
Header
files
Bash
shell
Precompiled
libraries
iostream
math
char, short, int, float,
double, if, switch, while, …
string
main() function in a.out
Visual programming: Turtle graphics
“Turtle” holding pen
Initially touching paper
Turn through angle
Move some distance
Pen down, pen up
forward(100);
left(90);
forward(100);
left(90);
forward(100);
left(90);
forward(100);
Repititive and boring
CS101 2012.1
Blocks and loops
Want to draw arbitrary regular polygons
Input: numSides (number of sides)
Repeat numSides times:
• Move through a fixed length, say 100 units
• Turn anticlockwise by 360/numSides degrees
int numSides;
cin >> numSides;
repeat(numSides) {
forward(100);
left(360/numSides);
}
CS101 2012.1
Red text shows a block of
instructions to be
executed in sequence
(What will happen if
360/numSides is
fractional?)
1st lab session
Familiarize with Linux desktop
Open a terminal window, the bash shell
Your home and other directories; files
Running shell commands
Using a text editor
Typing a small C++ program
Compiling it using g++
Running the resulting executable file
CS101 2012.1