Welcome to IIT and cs 105 1 CS

  • Slides: 73
Download presentation
Welcome to IIT and cs 105! 1

Welcome to IIT and cs 105! 1

CS 105 - Sec. 02 Summer 2015 • George Koutsogiannakis – office: 112 SB

CS 105 - Sec. 02 Summer 2015 • George Koutsogiannakis – office: 112 SB • 312 -567 -5288 • koutsogiannakis@iit. edu • Best way to communicate with me outside of class is via email or • In my office after class – 12 noon-1: 00 p. m. T & Th. • Course web site: www. cs. iit. edu/~cs 105 2

 • Lecture/Lab: T, R 5: 00 P. M. – 8: 00 P. M.

• Lecture/Lab: T, R 5: 00 P. M. – 8: 00 P. M. SB 108 3

CS Intro Courses • CS 105: 1 -Semester Requiring Little or No Previous Programming

CS Intro Courses • CS 105: 1 -Semester Requiring Little or No Previous Programming Experience, Programming in C++ • The course covers programming fundamentals. Practice exercises are assigned. 4

CS 105 – Required Textbook and Lab Manual • “C++Programming: From Problem Analysis to

CS 105 – Required Textbook and Lab Manual • “C++Programming: From Problem Analysis to Program Design” By D. S. Malik, • 5 th (red) Edition • You can use other editions but note that the schedule of assignments is based on the 5 th edition (page numbers differ from edition to edition) 5

CS 105 - Quizzes/Exams & Grading • 2 Quizzes – Each 5% of Grade

CS 105 - Quizzes/Exams & Grading • 2 Quizzes – Each 5% of Grade • 2 Exams: – Exam I - Lecture Exam - 10% – Exam II - Lecture Exam - 25% • Final Exam - 35% • Labs - 20% • Must Take Final to Pass Course 6

CS 105 Labs • 11 Labs Assigned consist of: – Reading Assignment, and Lab

CS 105 Labs • 11 Labs Assigned consist of: – Reading Assignment, and Lab Exercises from Textbook. – Programming Exercises (Located at End of Each Chapter in Textbook) • Labs Due on specific dates as mentioned on the web site. • Must be present in order to receive credit! Points are subtracted if late for lab session. • Show answers to instructor to receive credit. • Grades are posted on Blackboard. • Labs 0 -9: 1. 8 points each • Lab 10: 2 points • Total: 20 points towards your final grade. 7

Lab Grading procedure 1. Start lab on assigned date in class when the instructor

Lab Grading procedure 1. Start lab on assigned date in class when the instructor finishes the lecture part. 2. Ask questions about the lab. 3. Finish lab at home if not completed during the class session. 4. Have your lab graded by the instructor on the next session by showing your work. You will receive a grade when you show your work. 5. The grade will be available on Blackboard. 8

Other Class Work • I will have practice exercises for you to do on

Other Class Work • I will have practice exercises for you to do on the computer during lectures. • You are required to do the practice exercise and not work on anything else!!!! • No grading for practice exercises. • Cover topics discussed in order to provide hands on understanding. 9

Exams • • • 2 quizes : 5 points each Exam 1: 10 points

Exams • • • 2 quizes : 5 points each Exam 1: 10 points Exam 2: 25 points Exam 3: 35 points Consist of : – questions about topics discussed – Small programs • Hand written. 10

Expectations • Attendance in Lectures and Labs – Take notes during lectures! • •

Expectations • Attendance in Lectures and Labs – Take notes during lectures! • • Completion of Quizzes and Exams Four to Six Hours per Week on Homework No Cheating Have fun!! 11

Rules • No Swearing or Other Inconsiderate Behavior • Turn Off Cell Phones during

Rules • No Swearing or Other Inconsiderate Behavior • Turn Off Cell Phones during lectures. • Questions, Discussion, Ideas Welcome. • No cell phones, no other material allowed on the desk during quizzes or exams. 12

Excuses • Exam, Quiz Dates Already Posted. No make ups except in case of

Excuses • Exam, Quiz Dates Already Posted. No make ups except in case of emergency. • If Emergency: – Doctor/Hospital Note 13

Unacceptable Excuses • • Slept Late Felt Sick I’m Just a Freshman Roommate Ate

Unacceptable Excuses • • Slept Late Felt Sick I’m Just a Freshman Roommate Ate My Alarm Clock/Textbook/Underwear Missed Bus Had a Game/Match/Practice Didn’t Know When Exam/Quiz Was If Any of Above Happen, Get to Class As SOON as Possible!!! 14

CS 105 - Ethics • Exams: – Closed Book, Closed Notes, Closed Everything –

CS 105 - Ethics • Exams: – Closed Book, Closed Notes, Closed Everything – Nothing in Ears (mp 3 players, cell phones, etc. ) • Labs Should Be Done Independently 15

CS 105 – Where to Get Help • www. cs. iit. edu/~cs 105 •

CS 105 – Where to Get Help • www. cs. iit. edu/~cs 105 • Internet: Search for “C++ tutorial”, or “C++ help” • GET HELP EARLY RATHER THAN LATER!!! 16

CS 105 – Web Page • http: //www. cs. iit. edu/~cs 105 • Click

CS 105 – Web Page • http: //www. cs. iit. edu/~cs 105 • Click on Syllabus – Weekly Assignments – Quiz and Exam Dates – Lecture Slides – Other Course Information 17

CS 105 Labs (Cont) • All Labs Assigned from Textbook (Malik) • Work on

CS 105 Labs (Cont) • All Labs Assigned from Textbook (Malik) • Work on Labs on Personal PC, or Campus Computers • Save. cpp File(s) to Floppy or flash drive, or send to yourself in email and Bring to Lab Section for Discussion and Questions 18

Course Philosophy • Computer Science Side – Problem Solving – Logical Thought – Programming

Course Philosophy • Computer Science Side – Problem Solving – Logical Thought – Programming in C++ • “Real World” Side – Human Nature – Corporate World – Surviving during and after College 19

Problem Solving • • Arrange a Deck of Cards by Suit and Rank How

Problem Solving • • Arrange a Deck of Cards by Suit and Rank How Would You Do This? How Would You Tell a Child to Do This? How Would You Tell a Computer to Do This? 20

Problem Solving • CS 105 Develops Logic Skills to Solve Problems by Writing a

Problem Solving • CS 105 Develops Logic Skills to Solve Problems by Writing a Program • A Program is a Problem Solving Tool • Computers Follow Instructions Given to Them • Computers Do Not Have “Intuition” • Computers Do Not Make Decisions “on Their Own” 21

Why Use a Program? • Computers Perform Tasks Many Times Faster than a Person

Why Use a Program? • Computers Perform Tasks Many Times Faster than a Person • Computers Are More Consistent than a Person • Computers Can Work 24 -7 22

Terminology • Source Code: the Original Problem-Solving, Logical Solution Written in a Programming Language

Terminology • Source Code: the Original Problem-Solving, Logical Solution Written in a Programming Language (e. g. C++) • Compiling: the Action of Turning the Source Code into a Format the Computer Can Use • Linking: the Action of Bringing in Already Written Code (Libraries) for Use in a New Program • Executable: the Result of Compiling and Linking a Source Program; the “. exe” file that the Computer Can Run 23

Terminalogy • Pseudocode: Writing your thoughts for solving the problem (the program) in a

Terminalogy • Pseudocode: Writing your thoughts for solving the problem (the program) in a combination of English sentences and programming (i. . e C++) instructions. – Could take many attempts before the solution make sense. 24

Syntax • A programming language has: – Definite rules as to how to write

Syntax • A programming language has: – Definite rules as to how to write instructions called the ”Grammar” and “Syntax” of the language. Pretty much like a spoken language. – Has definite words and symbols that make up the instructions called “keywords”. Pretty much like the dictionary of a spoken language. 25

C++ Required Elements • Every C++ Program Must Have: int main() { } 26

C++ Required Elements • Every C++ Program Must Have: int main() { } 26

Explanation • In the previous slide: – int , main are keywords. – (),

Explanation • In the previous slide: – int , main are keywords. – (), {, } are symbols – How the keywords and the symbols appear in our programm, is guided by the grammar/syntax of the language. – i. e. the syntax requires that every C++ program start with the keyword int followed by the keyword main followed by (). 27

Explanation • The symbol { indicates that the programming instructions start after the symbol.

Explanation • The symbol { indicates that the programming instructions start after the symbol. • The symbol } indicates the end of the programming instructions. • The keyword main () indicates the name of a function (by the name main ). – function is a term used to describe a specific task that a program has to perform. 28

Explanation • A program can have many tasks, therefore many functions. • The Grammatical

Explanation • A program can have many tasks, therefore many functions. • The Grammatical rule that we have learned so far is that: – EVERY C++ PROGRAM MUST HAVE A FUNCTION CALLED main – AND THAT THE SYMBOLS () MEAN FUNCTION – AND THAT THE INSTRUCTIONS FOR THE FUNCTION (TASK) START AFTER THE SYMBOL { AND END WITH THE SYMBOL } 29

How the program is understood by the computer • A computer understands machine level

How the program is understood by the computer • A computer understands machine level instructions which consist of a series of 1 and 0. – In other words machine level instructions are coded instructions. 30

How the program is understood by the computer • The program that we write

How the program is understood by the computer • The program that we write has to: – Be checked for compliance to the grammar and syntax of the language. – Be converted to machine level instructions. • A software tool called “ the compiler” is responsible for the above two tasks. 31

How the program is understood by the computer • If the compiler finds errors

How the program is understood by the computer • If the compiler finds errors (the first task of the compiler) : – It provides you with messages and clues as to the kind of grammatical or syntactical errors found. – It refuses to perform the second task (the translation to machine coded instructions), until all errors are corrected. 32

How the program is understood by the computer • After you correct all errors

How the program is understood by the computer • After you correct all errors the compiler will perform the second task: – You know that the second task of the compiler was performed because it produces the executable file (the file with the coded machine level instructions). • Clicking on the executable file will cause the computer to execute the machine level instructions generated by the compiler based on your C++ level instructions. 33

Logical tasks • Is my program performing the tasks I intended for it? •

Logical tasks • Is my program performing the tasks I intended for it? • Note: A compiler can not check for logical errors only grammatical and syntactical errors are checked. i. e. suppose the correct logic in the task that you want the computer to perform requires the addition of two numbers but instead your instruction is to multiply the two numbers. 34

Your First Program // George Koutsogiannakis // CS 105 // Summer Section 2 #include

Your First Program // George Koutsogiannakis // CS 105 // Summer Section 2 #include <iostream> using namespace std; int main() { cout << “Hello World!!” << endl; return (0); } 35

Your First Program • Notice that certain information about who wrote the program, the

Your First Program • Notice that certain information about who wrote the program, the class the person attends and the section number as well as the date are in written as comments. • Same information must appear in every program you write in this class as part of the labs or exams/quizzes!!! • Comments are disregarded by the compiler and they are marked with // in each line. 36

Writing a C++ Program • Write Pseudocode (Design) • Translate Pseudocode into C++ Source

Writing a C++ Program • Write Pseudocode (Design) • Translate Pseudocode into C++ Source Code in Visual C++ • Edit Code • Compile • Link 37

What Is Pseudocode? • Every Programmer’s Way of Writing Down Steps in Solving a

What Is Pseudocode? • Every Programmer’s Way of Writing Down Steps in Solving a Problem • Steps in Creating Pseudocode: – Understand the Problem – Decide How to Solve the Problem – Write the Solution Using a Logical Sequence of Statements • Design Issues Apply to almost ANY Vocation 38

Writing Source Code • Source Code Is the Actual Program Code that Will Run

Writing Source Code • Source Code Is the Actual Program Code that Will Run Once Compiled and Linked • Pseudo Code Should Be Easily Translated into Source Code 39

Preprocessing • Preprocessing Is the Actions Taken Before a Source File Is Handed Off

Preprocessing • Preprocessing Is the Actions Taken Before a Source File Is Handed Off to the Compiler • Outcome of Preprocessing Must Still Be a Correct Source Code File • Example Preprocessing Statement: – – #include: Includes Text of Other Source Files #include Usually Occur at Top of Program Pound Sign (#) Must Be in First Position on Line Example: #include <iostream> for Typical Input and Output Operations in C++ 40

Compiler • Converts Source Code into an Object File or Machine Code • Each

Compiler • Converts Source Code into an Object File or Machine Code • Each Change to Source File Requires a Recompilation • Compiler Detects Syntax Errors • Syntax Error: a Non-Proper, Not Allowable, Sequence of Characters or Words Given a Particular Language 41

Syntax Errors • Spelling Errors Examples: – Undefined Variable Name – Unrecognized Keyword –

Syntax Errors • Spelling Errors Examples: – Undefined Variable Name – Unrecognized Keyword – Cannot Find Include File or Library • Punctuation Errors Examples: – Missing Curly Braces – Missing Semicolons – Malformed Comments • Syntax Errors Are Listed at Bottom of Screen When Compilation Is Complete 42

Syntax Errors (Cont) • Syntax Errors Are Listed at Bottom of Screen When Compilation

Syntax Errors (Cont) • Syntax Errors Are Listed at Bottom of Screen When Compilation Is Completed • Two Kinds of Messages: – Warning: Compiler Thinks There Is a Problem, but Lets It Go By Without Failing – Error: Compiler Finds an Error and Fails to Compile • First Error in Program May “Cause” Other Errors to Show Up • General Rule: Fix First Error (and Any Obvious Errors); Then Recompile 43

Linking • Links (Connects) Object Code with External Libraries, which Contain Functions (Already Written

Linking • Links (Connects) Object Code with External Libraries, which Contain Functions (Already Written and Compiled Code) to Use with Another Program • Creates an Executable File • An Executable File (e. g. . exe File) Can Be Run • If Linker Cannot Find Libraries, Error Message Are Generated 44

Logic Errors – “Bugs” • Also Called Semantic or Run-Time Errors • Program Compiles,

Logic Errors – “Bugs” • Also Called Semantic or Run-Time Errors • Program Compiles, but Doesn’t Produce the Expected Results • Examples: – – – Program Keeps Running Forever (Infinite Loop) Nothing Happens Program Output Is Incorrect Error Message at Run Time (e. g. Dividing by 0) Errors May Be Intermittent • May Be Able to Find Semantic Problems with Debugger 45

Detecting “Bugs” • Running Program Should Be Tested Several Times with Different Inputs •

Detecting “Bugs” • Running Program Should Be Tested Several Times with Different Inputs • Test Plan – Series of tests (e. g. , inputs) that have predetermined expected outputs. • Running Program Should Be Tested Several Times under All Potential Conditions • When Errors Detected: Analysis Needed: Debugging Mechanism 46

Comments • Comments: Describe a Program • // Comment to end of line •

Comments • Comments: Describe a Program • // Comment to end of line • /* Comment until first occurrence of star slash */ • Difficult to Understate Importance of Good Comments • Should Be at Beginning of Program (Overall Summary) • Should Be Next to Variable Declaration Briefly Describing Uses of Variable • Should Be Anywhere in Program Where Unobvious Coding Is Performed 47

Program Format • White Space: – Not Recognized by Compiler – Indent (e. g.

Program Format • White Space: – Not Recognized by Compiler – Indent (e. g. 3 Spaces) for Each New Function, Selection, or Loop 48

Variables • Identifiers Used to Store Values that May Change • Every Variable Has

Variables • Identifiers Used to Store Values that May Change • Every Variable Has a Type : What Kind of Value (e. g. , Integer, Floating Point Number, Character) Is Begin Stored • Every Variable Has a Value which Is Assigned and Can Be Changed • Every Variable Must Be Declared, which Sets Aside Memory for the Storage Location 49

Declaring a Variable • Declaration Normally Comes after a Function Definition Opening Curly Brace:

Declaring a Variable • Declaration Normally Comes after a Function Definition Opening Curly Brace: int main() { int num 1; //variable declaration num 1 = 10; // variable assignment cout << num 1; // variable output } • Declaration Includes Type and the Name of the Variable • All Variables Must Be Declared before Being Used 50

Variables and Main Memory int num; Main Memory num <garbage> num = 10; num

Variables and Main Memory int num; Main Memory num <garbage> num = 10; num 10 num = 15; num 15 51

Main Memory • • Storage Location of Data in a Computer Used When a

Main Memory • • Storage Location of Data in a Computer Used When a Program Is Running “Wiped Clean” When Computer Rebooted Bit: a logical single 1 or 0 value (sometimes referred to as True or False) • Byte: Basic Unit of Storage (Can Store One Letter of the Alphabet) – Consists of 8 Bits. • Kilobyte (KB): 1000 (or 1024) Bytes (Pages of Text) • Megabyte (MB): 1, 000 Bytes (Large Pictures) • Gigabyte (GB): 1, 000, 000 Bytes (Music Files, Video Files) 52

Initialization and Assignment • Initialization: Value Given to Variable at Declaration • Assignment: Value

Initialization and Assignment • Initialization: Value Given to Variable at Declaration • Assignment: Value Given to Variable in Execution Statement 53

Variable Initialization int main() { int num 1 = 10; //variable declaration // with

Variable Initialization int main() { int num 1 = 10; //variable declaration // with initialization cout << num 1; // variable output } 54

Built-In (or Primitive) Data Types for Variables • int: Integer Range of Typically -32,

Built-In (or Primitive) Data Types for Variables • int: Integer Range of Typically -32, 768 to 32, 767 (machine and compiler dependent) • float: Real Number (i. e. , integer part, decimal part, and exponent part) Range of Typically 10 e-38 to 10 e 38 • double: Larger Real Number (10 e-308 to 10 e 308) • char: Character 55

Naming Variables in C++: Identifiers • Can Use Letters: Remember That C++ is Case

Naming Variables in C++: Identifiers • Can Use Letters: Remember That C++ is Case Sensitive (e. g. , Num. Widgets Is Not the Same as numwidgets) • Can Use Digits 0 -9, and Underscore • Cannot Start with a Digit • Cannot Contain Spaces or Other Characters • Typically Maximum of 32 Characters • Cannot Use C++ Keywords 56

Naming Variables (Cont) • Should Use a Meaningful, Descriptive Name so that Variable’s Use

Naming Variables (Cont) • Should Use a Meaningful, Descriptive Name so that Variable’s Use Is Easily Understood: • Examples: counter, second, minute, length, width • Be Consistent with Case; Usually Lower Case with Upper Case for Second Part of Variable Name • Examples: average. Rainfall, total. Student. Grades, max. Building. Height, min. Package. Weight; 57

Variable Scope • Scope: Area of a Program within which a Variable Can Be

Variable Scope • Scope: Area of a Program within which a Variable Can Be Referenced • Variable Definitions Are Recognized in the Curly Braces in which They Were Defined • Variables Declared Outside of Functions Are Recognized from the Point Declaration through the Rest of the Program 58

Named Constants • Constant: An Identifier that Is Initialized to a Value that Cannot

Named Constants • Constant: An Identifier that Is Initialized to a Value that Cannot Change • Usually Declared at Top of Program using Keyword const • Standard Naming of Constants Is to Use All Upper Case Letter with or without Underscore between Words • All Constants Must Be Initialized • Syntax: const int MAXHT = 100; 59

Advantages of Constants • Easier to Understand • Easier to Modify • Example: Compare

Advantages of Constants • Easier to Understand • Easier to Modify • Example: Compare using Number 5000 in Program versus Constant MAXHT 60

Named Constants const int MAXHT = 100; main() {. . . current. Height >

Named Constants const int MAXHT = 100; main() {. . . current. Height > MAXHT. . . bridge. Height == MAXHT. . . bridge. Height + new. Addition >= MAXHT. . . // used MAXHT 223 times in this program } 61

Literal Constants (Values) • Constants Whose values Are Already Known: Characters (Specified Inside Single

Literal Constants (Values) • Constants Whose values Are Already Known: Characters (Specified Inside Single Quotes): ‘A’ , ‘a’ , ’ 5’ , ’ ‘ , ’n’ (newline) , ’’ (NULL Character) Integers: 10, 1345, -34 Float or Double: 2. 3, -45. 18, 10. 6 e 6 String (Specified Inside Double Quotes): “HELLO” , “What a great deal. ” , “ 5” 62

Input and Output • • • Also Known as I/O Output Stream: cout <<

Input and Output • • • Also Known as I/O Output Stream: cout << variable; Input Stream: cin >> variable; New Line: endl All C++ Statements End in Semicolon (; ) 63

Input and Output Example #include <iostream> using namespace std; int main() { int num.

Input and Output Example #include <iostream> using namespace std; int main() { int num. Entered; cout << “Please enter an integer: ”; cin >> num. Entered; cout << “Thanks, you entered: “ << num. Entered << “. ” << endl; } 64

Characters and Strings • Standard Template Library (STL): Library of C++ Functions Used Worldwide

Characters and Strings • Standard Template Library (STL): Library of C++ Functions Used Worldwide • String Type NOT Built-in in C++ • String Type Defined in STL • To Use STL Strings: #include <iostream> #include <string> using namespace std; string last. Name; 65

STL Strings • Declaration: string your. Name; • Assigning a Value to a String:

STL Strings • Declaration: string your. Name; • Assigning a Value to a String: your. Name = “A. Goose”; • String Constants (Values) Must Be Enclosed in Double Quotes 66

Input String Example #include <iostream> #include <string> using namespace std; int main() { string

Input String Example #include <iostream> #include <string> using namespace std; int main() { string last. Name; cout << “Enter your last name: “; cin >> last. Name; cout << “Your last name is: “ << last. Name << endl; return(0); } 67

Expressions • Expression: A Sequence of One or More Identifiers and Operators that Evaluates

Expressions • Expression: A Sequence of One or More Identifiers and Operators that Evaluates to a Value • Operator: A Symbol Expressing a Way to Modify a Value or Values (e. g. , + for Addition) • Operand: A Value Being Modified by an Operator • Example Expressions: 5 current. Height + 10 68

Arithmetic Expressions • Standard Arithmetic Operations Can Be Performed: Addition, Subtraction, Multiplication, Division •

Arithmetic Expressions • Standard Arithmetic Operations Can Be Performed: Addition, Subtraction, Multiplication, Division • Standard Arithmetic Operators Can Be Used for These Operations: +, -, *, / • Others: % - “Modulo (Mod)” – Remainder after Integer Division -- Decrement (Subtract 1) ++ Increment (Add 1) 69

Order of Operations • Precedence: Level of Importance of Operations Multiplicative Operators Have Higher

Order of Operations • Precedence: Level of Importance of Operations Multiplicative Operators Have Higher Precedence than Additive Operators: *, /, % Higher +, - Lower • Associativity: Order of Operation for Equal Level Precedence Most Operators Have Left-to-Right Associativity Use Parentheses to Force Differing Precedence of Operations 70

Know for the Quiz 1 - July 07 • All Terms (Underlined Items) •

Know for the Quiz 1 - July 07 • All Terms (Underlined Items) • Variable Declaration, Initialization, and Assignment • Constant Declaration • Expressions • Operators • Input (cin) and Output (cout) 71

Know for the Quiz • Read Chapters 1 & 2 , pages 28 -67,

Know for the Quiz • Read Chapters 1 & 2 , pages 28 -67, 67 -77, 77 -86, 86 -100, 117 -124. (Note: Edition 5 page numbers) • Ask questions about the exercises if unable to complete Lab 0 OR Lab 1. • Practice with additional exercises from text on your own. • Study examples material distributed in class. • Study Lect 01, 02, 03 slides. Also study the first 3 slides of Lect 04, 05 • Quiz will be given at the end of class period, after the lecture part. 72

Quiz Format • 15 questions where you are given a concept and you are

Quiz Format • 15 questions where you are given a concept and you are asked to match it against a list of definitions (3/5 of the grade). • Write one program (2/5 of the grade) that requires: – Declarations of constants and variables. – Input from user. – Arithmetic expressions including increment or decrement operators. – Display of program output 73