1 / 27

Computer Programming and Utilization CS101 Spring 2012

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. Places and times.

torin
Download Presentation

Computer Programming and Utilization CS101 Spring 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programmingand UtilizationCS101 Spring 2012 Soumen Chakrabarti with CS101 TAs and Staff

  2. 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

  3. 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 S10 (160) M T W Th F S6 (160) (80) Make-up lab CS101 2012.1

  4. “The management” cs101.iitb@gmail.com Web site STAs Instructor Assignment STAs STA STA STA STA STA cs101stas@cse.iitb.ac.in JTAs JTAs JTAs JTAs JTAs cs101jtas@cse.iitb.ac.in Thu lab. batch Tue lab. batch Wed lab. batch Fri lab.batch Make-up lab. Do not use personal email CS101 2012.1

  5. 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

  6. PC building blocks: motherboard CPU withcooling fan Fastelectronicmemory Magneticdisk dataconnectors CS101 2012.1

  7. Storage and peripheral devices Keyboard Display Rotatingmagneticplatters Record/playhead on arm Data cable betweendisk and motherboard CS101 2012.1

  8. Simplified abstract view CPU Random accessmemory (RAM) RAM location 0 Arithmeticand logic unit Address RAM location 1 RAM location 2 … Register 0 Register 1 Data Reserved fordisplay Register 2 … Reserved for keyboard Program that tells the CPU what to do andhow to do it CS101 2012.1

  9. 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

  10. 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 R0R1 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 “Assemblylanguageprogramming” Painful! CS101 2012.1

  11. 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

  12. Saying it in C++ Variable type main() { float fahrenheit; cin >> fahrenheit; float centigrade = 5*(fahrenheit-32)/9; cout << centigrade; } Procedure name Variable declaration 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

  13. The hardware-software stack Your C++ executable program Shell command line (bash) Operating System (Linux) Ha Hardware Your program accesses resources like CPU, keyboard,display, through the lower layers of system software CS101 2012.1

  14. C++: More detail main() { float fahrenheit; cin >> fahrenheit; float centigrade = 5*(fahrenheit-32)/9; cout << centigrade; } Procedure name Variable declaration Data type 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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 sayingusing namespace std;before using things defined inside std. CS101 2012.1

  21. First complete source code #include <iostream> using namespace std; main() { float fahrenheit; cin >> fahrenheit; float centigrade = 5*(fahrenheit-32)/9; cout << centigrade; } • At your shell, typeg++ cf.cc • Now run resulting file as./a.out Save to file “cf.cc” g++ compiler Executable a.out CS101 2012.1

  22. 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

  23. 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 usr bin cs101 you g++ bash CS101 2012.1

  24. Compilation and execution summary string math.h Source code cf.cc iostream g++ compiler main() function in a.out C/C++ execution environment string math iostream Bashshell char, short, int, float, double, if, switch, while, … Headerfiles Operating system (Windows, Linux, Mac OS, …) Precompiled libraries CPU RAM Disk Keyboard Display CS101 2012.1

  25. 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

  26. 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);} Red text shows a block of instructions to be executed in sequence (What will happen if 360/numSides isfractional?) CS101 2012.1

  27. 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

More Related