1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm.

Slides:



Advertisements
Similar presentations
COSC 120 Computer Programming
Advertisements

Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
Computer Science 1620 Programming & Problem Solving.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Chapter 1: Introduction To Computer | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1 Introduction To Computers.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Welcome to IIT and cs115!. CS Secs. 001 Jon Hanrath SB
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Input & Output: Console
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Input, Output, and Processing
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Week 1 Algorithmization and Programming Languages.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Welcome to IIT and cs105! 1. CS Sec. 02 Summer 2015 George Koutsogiannakis – office: 214 SB Best way to communicate.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Introduction to C Programming
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Welcome to IIT and cs105!. CS 105 Jon Hanrath – SB112B Office Hours: –TBD
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 1: Introduction to Computers and Programming
Last week: We talked about: History of C Compiler for C programming
CMPT 201.
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
Computing Fundamentals
CSCI 161: Introduction to Programming
Chapter 2: Introduction to C++
Chapter 1: Introduction to Computers and Programming
2.1 Parts of a C++ Program.
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
An Introduction to Debugging
Software Development Environment, File Storage & Compiling
Subject:Object oriented programming
Chapter 1 c++ structure C++ Input / Output
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

2 Go to click on Syllabus, review Lecture 01 notes, course schedule. Make sure you go to Lab 0. (Make sure you go to Lab 0 even if didn’t miss last week :-) If You Missed Last Week

3 Write pseudocode (Design) Translate pseudocode into C++ source code (Coding) Edit code Compile Link Writing a C++ Program

4 English-like written solution to a programming problem. Somewhere between informal description and program written in C++. How do we get pseudocode? How do we solve problems? Design issues apply to almost any vocation. What Is Pseudocode?

5 Steps in creating pseudocode: Understand the problem. Decide how to solve the problem. Write the solution using a logical sequence of statements. Typically, we break down large problems into smaller subproblems and solve those. Creating Pseudocode

6 Source code is the actual program code that will run once compiled and linked. Pseudocode should be easily translatable into source code. Pseudo vs Source Code

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

8 // Sam Smith // CS 105 // Section 07 #include using namespace std; int main() { cout << “Hello World!!” << endl; return (0); } Your First Program

9 Actually uses multiple programs to translate source code program into machine code (executable by hardware). Preprocessor Actual compiler Linker Compiler

10 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 #include is an example Preprocessing

11 #include: Replace w/text of specified file. #include usually occurs at top of program. Pound sign (#) must be in first column. Ex: #include for typical input and output operations in C++ Error if file to include can’t be found. #include

12 Converts source code into an object file or machine code. Each change to source file requires a recompilation before re-execution. Compiler also looks for malformed programs — syntax errors a.k.a. “compile time errors”. Actual Compiler

13 Syntax error: A non-proper, not allowable, sequence of characters or words given a particular language. Typical syntax errors: Spelling mistakes Punctuation mistakes Wrong type of data for operation Syntax Error

14 Undefined variable name C++ is case sensitive Example: “Hi” >> cout (not COUT). Unrecognized keyword Certain “key” words have special meaning in C++. Example: int. Spelling Errors Examples

15 Missing, extra, or misplaced parentheses, braces, commas, semicolons... Parentheses unbalanced or of wrong shape. Comments malformed. Syntax errors are typically listed at bottom of screen when compilation is complete. Punctuation Error Examples

16 Two kinds of syntax error messages: Warning: Compiler finds possible error, lets it go by without failing. Error: Compiler finds an error that causes compile to fail. Syntax Error Messages

17 First error in program may “cause” other errors to show up. General advice: Fix the first error (and any obvious errors), then recompile. Syntax Error Messages

18 Linking connects (i.e. links) your object code with external libraries (external = not written as part of this program). Object library contains already-written and compiled code to use with other programs. Example: Code for iostream includes definition of “ >” Linking

19 If linker cannot find the libraries, error messages are generated. Successful linking creates an executable file. An executable file can be run. (.exe file in Windows) Linking

20 Also called semantic or run-time errors. Program compiles, but doesn’t produce the expected results. Results wrong, missing, extra. Program halting wrongly. Errors may be repeatable or intermittent. Logic Errors – “Bugs”

21 Halts early. Never halts (infinite loop). Halts due to bad operation. Possible example: Dividing by zero. Compiler does type checking to avoid wrong kinds of data at runtime. Halting Problems

22 (From Navy website for Adm. Grace Murray Hopper)Adm. Grace Murray Hopper 1947: Moth found shorting a relay in early computer. “Bugs” and “debugging” already used as terms, but this is first example of a bug being caused by an actual bug. The “First” Bug

23 The “First” Bug

24 Test-run program with different inputs. Test plan: series of tests (e.g., inputs) that have predetermined expected outputs. Test-run program under all potential conditions. May need simulator, other tools to look at data & operations during program run. Detecting Bugs

25 Debugging: Finding and removing bugs. When errors detected, analysis is needed. Work backward from symptom: What can cause this symptom? Example: We printed x and got the wrong value. Where did x get set/changed? Why was x set to a bad value? Debugging

26 Comments are used to help the person reading the program understand it. Good comments are invaluable. Typically two types of comments. Summary of program or major part of it. Descriptions of data/variables. Comments

27 Summary comment typically describes intent of larger piece of program. E.g. overall summary at beginning of program. With data/variable definition, comments describe properties, uses of data/variable. E.g. x should be ≥ 0 and ≤ y. Types of Comments

28 Don’t generally need to comment “what” is happening unless it’s especially complicated. Comments are good for saying things you can’t easily infer from the program. Why are we doing something? What are the properties of and relationships between variables? What Goes in Comments?

29 Two ways to write comments in C++ // Comment to end of line /* Comment until star slash */ First occurrence of star slash (not “matching” like parentheses). Can be many lines away. Comments in C++

30 Summary Comments Preprocessor statements (e.g., #include) Namespace declaration Main Function Elements of a Program

31 Main Function: int main() { // statements; return 0; } Main Function

32 White Space: Not recognized by compiler Used by humans to show program structure. Indent (e.g. 3 spaces) for each new function, selection, or loop Program Format

33 Variables are names (identifiers) used to store values that may change. Every variable has a value and a type. Type: the kind of value (integer, floating point, character, etc.) being stored. Value gets assigned/reassigned as program runs Variables

34 In C++, we “declare” variables. Tells compiler to set aside storage space. Tells compiler the variable’s type (what type of value it will hold) May or may not specify an initial value for the variable. Must declare variable before using it. Declarations of Variables

35 int main() { int num1; variable declaration num1 = 10; variable assignment cout << num1; variable output } Declaration int num1 includes type and name of variable (no initial value). Example

36 int num; num = 10; num = 15; num 10 num Main Memory 15 num Variables and Main Memory

37 Storage location of data in a computer. Used when a program is running. RAM = “Random Access Memory” “Wiped clean” when computer rebooted. Byte: basic unit of storage (8 bits; can store one letter of English alphabet) Main Memory

38 Kilobyte (KB): 1000 (or 1024) Bytes Bug photo was 100 KB Megabyte (MB): 1,000,000 Bytes CD holds 700 MB Gigabyte (GB): 1,000,000,000 Bytes DVD holds 4.7 GB Main Memory

39 Initialization: Giving a value to a variable when its space is allocated (specified in declaration). Assignment: Giving a value to a variable after it’s been allocated. Use assignment statement to do this. Initialization/Assignme nt

40 int main() { int num1 = 10; variable declaration with initialization cout << num1; variable output } Variable Initialization

41 C++ supports some built-in/”primitive” data types. (Correspond to types of data typically supported by hardware.) Various kinds of numbers (integral and floating-point). Characters. Built-In Datatypes

42 int: Integer, typically to (de- pends on machine/compiler). No commas!! float: Real number, range typically to E.g. 6.02e23 for 6.02 × 10²³. double: Larger exponents, more significant digits than float (typically 10e- 308 to 10e308). Built-In Numeric Types

43 Can use letters, digits 0–9 and underscores. Can’t start with a digit. Case-sensitive (Example: NumWidgets is not the same as numwidgets). Can’t contain spaces or other characters. Practical maximum of 32 characters. Cannot use C++ keywords. Identifiers

44 Use a meaningful, descriptive name so that variable’s use is easily understood. (E.g. counter, second, minute, length, width.) Separate internal words with underscores or with capitalization. (Example: averageRainfall or average_rainfall, not averagerainfall.) Naming Variables (Cont)

45 Scope: Area of a program within which a variable can be referenced. Typical variable has local scope, within the { … } in which it was defined. Later on we’ll see function parameters; they have local scope too. Scope of a Variable