WRITING A PROGRAM USING VISUAL C++

Download Report

Transcript WRITING A PROGRAM USING VISUAL C++

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!!!
Writing a C++ Program
• Write Pseudocode (Design)
• Translate Pseudocode into C++ Source
Code in Visual C++
• Edit Code
• Compile
• Link
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
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
C++ Required Elements
• Every C++ Program Must Have:
int
main()
{
}
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++
Your First Program
// Jon Hanrath
// CS105
// Section 042
#include <iostream>
using namespace std;
int
main()
{
cout << “Hello World!!” << endl;
}
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
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
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
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
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
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
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
Elements of a Program
• Summary Comments
• Preprocessor Statements (e.g., #include)
• Main Function:
int main()
{
// statements;
return 0;
}
Program Format
• White Space:
– Not Recognized by Compiler
– Indent (e.g. 3 Spaces) for Each New Function,
Selection, or Loop
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
Declaring a Variable
• 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
Variables and Main Memory
int num;
Main Memory
num
<garbage>
num = 10;
num
10
num = 15;
num
15
Main Memory
•
•
•
•
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)
Initialization and Assignment
• Initialization: Value Given to Variable at
Declaration
• Assignment: Value Given to Variable in
Execution Statement
Variable Initialization
int main()
{
int num1 = 10; //variable declaration
// with initialization
cout << num1; // variable output
}
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 10e-38 to 10e38
• double: Larger Real Number (10e-308 to
10e308)
• char: Character
Naming Variables in C++:
Identifiers
• 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 (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:
averageRainfall, totalStudentGrades,
maxBuildingHeight, minPackageWeight;
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