Top Banner
If You Missed Last Week Go to www.cs.iit.edu/~cs105 , Click on Syllabus, review lecture 01 notes, course schedule • Contact your TA (email on website) Schedule going over Lab 0 with them AS SOON AS POSSIBLE!!!
26

If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

If You Missed Last Week

• Go to www.cs.iit.edu/~cs105, Click on Syllabus, review lecture 01 notes, course schedule

• Contact your TA (email on website) Schedule going over Lab 0 with them AS SOON AS POSSIBLE!!!

Page 2: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• Write Pseudocode (Design)

• Translate Pseudocode into C++ Source Code in Visual C++

• Edit Code

• Compile

• Link

Writing a C++ Program

Page 3: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• 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

What Is Pseudocode?

Page 4: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 5: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

C++ Required Elements

• Every C++ Program Must Have:

int

main()

{

}

Page 6: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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++

Page 7: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

Your First Program// Jon Hanrath// CS105// Section 042#include <iostream>using namespace std;intmain(){ cout << “Hello World!!” << endl;}

Page 8: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 9: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 10: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 11: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 12: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 13: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 14: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

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

Page 15: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

Elements of a Program

• Summary Comments• Preprocessor Statements (e.g., #include)• Main Function:

int main(){ // statements; return 0;

}

Page 16: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

Program Format

• White Space:– Not Recognized by Compiler– Indent (e.g. 3 Spaces) for Each New Function,

Selection, or Loop

Page 17: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• 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

Variables

Page 18: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• Declaration Normally Comes after a Function Definition Opening Curly Brace: int main()

{ int num1; //variable declaration

num1 = 10; // variable assignment cout << num1; // variable output }

• Declaration Includes Type and the Name of the Variable

• All Variables Must Be Declared before Being Used

Declaring a Variable

Page 19: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

Variables and Main Memory

int num;

num = 10;

num = 15;

<garbage>

num

10

num

Main Memory

15

num

Page 20: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• Storage Location of Data in a Computer• Used When a Program Is Running• “Wiped Clean” When Computer Rebooted• Byte: Basic Unit of Storage (Can Store One Letter

of the Alphabet)• Kilobyte (KB): 1000 (or 1024) Bytes (Pages of Text)• Megabyte (MB): 1,000,000 Bytes (Large Pictures)• Gigabyte (GB): 1,000,000,000 Bytes (Music Files,

Video Files)

Main Memory

Page 21: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• Initialization: Value Given to Variable at Declaration

• Assignment: Value Given to Variable in Execution Statement

Initialization and Assignment

Page 22: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

int main()

{ int num1 = 10; //variable

declaration

// with initialization

cout << num1; // variable output

}

Variable Initialization

Page 23: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• 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 10e-38 to 10e38

• double: Larger Real Number (10e-308 to 10e308)

• char: Character

Built-In (or Primitive) Data Types for Variables

Page 24: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• Can Use Letters: Remember That C++ is Case Sensitive (e.g., NumWidgets 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

Naming Variables in C++: Identifiers

Page 25: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• 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:averageRainfall, totalStudentGrades, maxBuildingHeight, minPackageWeight;

Naming Variables (Cont)

Page 26: If You Missed Last Week Go to cs105, Click on Syllabus, review lecture 01 notes, course schedule Contact your TA (email on website) Schedule.

• 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

Variable Scope