computer programming and utilization cs101 spring 2012 soumen chakrabarti with cs101 tas and staff
TRANSCRIPT
Computer Programmingand Utilization
CS101 Spring 2012
Soumen Chakrabarti
with
CS101 TAs and Staff
CS101 2012.1
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 FS6 (160)
S10 (160)
(80)
Make-up lab
CS101 2012.1
“The management”
STA
JTAs
Instructor
STA
JTAs
Tue lab.batch
STA
JTAs
Wed lab.batch
STA
JTAs
STA
JTAs
Fri lab.batch
Web site STAs
Assignment STAs
Do not use personal email
Make-up lab.
Thu lab.batch
CS101 2012.1
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 withcooling fan
Magneticdisk dataconnectors
Fastelectronicmemory
CS101 2012.1
Storage and peripheral devices
Rotatingmagneticplatters
Record/playhead on arm
Data cable betweendisk and motherboard
KeyboardDisplay
CS101 2012.1
CPU
Simplified abstract view
Arithmeticand logic unit
Register 0Register 1Register 2
…
Random accessmemory (RAM)
RAM location 0RAM location 1RAM location 2
…Address
Data Reserved fordisplay
Reserved for keyboard
Program that tells the CPU what to do and
how to do it
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
“Assemblylanguage
programming”
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 Divide R2 by R3 and store in R4 Load 5 in R5 Multiply R5 by R4 and store in R6 Output R6 to the display
Painful!
CS101 2012.1
High level programming language
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
•Input F from keyboard•Set C to 5*(F-32)/9•Output C to display
CS101 2012.1
Saying it in C++
Some details omitted Save above source code to text file Compile source code to executable file Call executable from a shell command line
main() {
float fahrenheit;
cin >> fahrenheit;
float centigrade = 5*(fahrenheit-32)/9;
cout << centigrade;
}
Procedure name
Variable declaration
Arithmetic expression
Variable type
CS101 2012.1
Ha
The hardware-software stack
Hardware
Operating System (Linux)
Shell command line (bash)
Your C++ executable program
Your program accesses resources like CPU, keyboard,display, through the lower layers of system software
CS101 2012.1
C++: More detail
What is a procedure? What are data types? Where did cin and cout come from? Rules of writing arithmetic expressions
main() {
float fahrenheit;
cin >> fahrenheit;
float centigrade = 5*(fahrenheit-32)/9;
cout << centigrade;
}
Procedure name
Variable declaration
Arithmetic expression
Data type
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 asH2::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
At your shell, typeg++ cf.cc
Now run resulting file as./a.out
#include <iostream>using namespace std;main() { float fahrenheit; cin >> fahrenheit; float centigrade = 5*(fahrenheit-32)/9; cout << centigrade;} Save to file “cf.cc”
Executable a.out
g++ compiler
CS101 2012.1
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/usr/bin/g++
/
home
cs101 you
usr
bin
g++bash
CS101 2012.1
Compilation and execution summary
CPU RAM Disk Keyboard Display
Operating system (Windows, Linux, Mac OS, …)
C/C++ execution environment
Bashshellio
stre
am
mat
h
strin
g
main() function in a.out
char, short, int, float, double, if, switch, while, …
Source code cf.cc
iostream
math.h
string
Pre
com
pile
d lib
rarie
sH
eade
rfil
es
g++ compiler
CS101 2012.1
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);}
(What will happen if360/numSides isfractional?)
Red text shows a block of instructions to be
executed in sequence
CS101 2012.1
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