Programs

advertisement
Programs
A program is a stored set of instructions that can be
interpreted and executed by a computer. This applies to the
written program which is just a text document and to the
electronic version which is a stream of non-human readable
binary digits ( 0 and 1).
Two Categories of Programs:
System
Multitasking/Single Tasking
Multiuser/Single User
Ex. operating system
Applications
Ex. Word Processor
Characteristics of a Well Designed and
Written Program
The program must operate properly
Correctness – meets the specifications
Integrity – does not create ‘side-effects’
Reliable – does not fail (crash, blue-screen)
Usable – easy to learn, screen presentation
Efficient – make good use of resources
Secure – protection from external threats
The program must be able to be maintained & revised
Documentation
Organization
Other desirable characteristics: Expandability, Portability
Programming Languages
A programming language is a language used to write instructions
for a computer. It lets the programmer express data processing
in a symbolic manner without regard to machine-specific details.
Low-level language:
First level above the binary language (machine language) of
the computer and is specific to the computer’s architecture.
High-level language:
Closer to human language
Depending upon the language, all programming languages
must be translated by application programs called compilers,
assemblers, or interpreters into executable machine language.
The C++ programming language uses a compiler.
Overview of the Programming Process
•
Clearly define what the program should do
State in writing what the program is to do
What information does it need? List the inputs and give them names.
How is it going to acquire that information? (keyboard, file, instrument, etc.)
How should the information be processed?
What should be the product(s) (output(s)) of the program? List them and give
them names.
o How should be outputs be presented? (display on screen, print, create file,
etc.)
o
o
o
o
o
•
Create an algorithm (step by step procedure) to solve the
problem (many tools are available for this process)
o pseudocode – Hybrid of code like and English like statements delineating the
steps involved to create a program that meets the problem conditions
o flow charts – visual aid to help visualize the solution
o UML – Unified Modeling Language
o others
•
Create and test the program
Write the code (text file*)
Save the file
Compile it to create a machine readable form
Run it with one or more sets of test data (Note: Test data should include all
possible situations)
o Correct errors and go back to the start of this list
o
o
o
o
The Translation Process
A human readable document containing instructions written in a highlevel language (in our case, C++) is created and passed off to a
program called a compiler that can process the C++ statement into
something the computer hardware can understand and execute.
****
Source code – text document containing the human readable written
instructions defining the algorithm (procedure) the computer is to follow to
accomplish a task or tasks.
Preprocessor – part of the compiler program used to translate the written
document into machine language. Some of the instructions in the source
code tell the compiler that it needs to modify or amplify the other
instructions. The statements in the source code preceded by # are for the
preprocessor.
Object Code created - Compiler processes the code produced by the
preprocessor into ‘almost machine executable code’ called object code
that contains the translated program and references to pieces of code
provided by the language to perform common tasks.
Linking - These references need to be ‘linked’ together to form the final
executable code.
Algorithm
Step by step outline of how to solve a problem.
Note: The program is the coded implementation of the algorithm
Example:
Problem: Given the price of an object on the price tag, determine
the sales tax and actual sales price which includes the sales tax.
Algorithm (Steps used to solve the problem):
Get the amount on the price tag.
Compute the sales tax. (Sales tax = Price Tag * Sales)
Compute the selling price. (Selling Price = Price Tag + Sales tax)
Display the Sales tax and the Selling Price
Program Source Code
Provide instructions for the compiler and the computer AND
important information for the programmer
Source code is just a human readable text file with a
file extension indicating the language that the
programmer used to write the program. For C++ that
file extension is ‘.cpp’.
As a beginner your programs will start with three basic
sections
• header comment
• compiler directives
• program body
Header Comment
The header comment appears at the top of the file
and contains information for programmers who need
to understand the program.
Examples of kinds of Information in the header
comment
• Identification of the programmer and his/her
organization
• Name/location of source file
• Statement of the task(s) the program is to perform
o Inputs required and their format
o Outputs produced
• Error code description
• Other information required by the company
Compiler Directives
Compiler directives are C++ statements that tell the
compiler about features of the program that it will
need to know about before it starts translating the rest
of the program.
You will use the following two compiler directives in
every program you write for this class. Others will be
added as we go along.
• #include <iostream>
• using namespace std;
Program Body
The program body is made up of sections of code
called functions. EVERY program will have a function
called ‘main’ which is always the starting point for
code execution.
Functions contain the code statements that instruct
the computer to carry out the algorithm you want to
use to carry out the task(s) the program is to perform.
For now, your programs will contain only 1 function,
the one that is always required, main.
Elements of a C++ Program
• Key Words
o
o
Reserved – cannot be used in other than in their intended fashion
Lower case – always written in lower case
• Operators
o
Symbols such as +,-,*,=, etc. that require operands and ‘operate’ on the operands
• Programmer Defined Symbolic Names
o
o
o
Cannot be the same as Key Words
Case sensitive
Usually follow a style dictated by your employer or instructor
• Punctuation
o
Semicolons, Commas
• Comments
/* Used for programmer notes spanning multiple lines. Everything between these
two symbols is for human readers of the program text and is ignored by the
compiler
*/
o Preceded by // on each new line they appear (single line comments)
o Used to document a program
o
• Grouping Symbols
o
o
Used to enclose sections of code or items that belong together
() and {}
Example C++ Program (color-coded)
/*Given the price tag of a store item, this program displays the sales tax and the final cost with sales tax
* included.
*/
#include <iostream>
using namespace std:
int main()
{
double marked_price,
tax,
selling_price;
//Get the price on the price tag
cout << “What is the price indicated on the price tag?”;
cin >> marked_price;
//Calculate the sales tax
tax = marked_price * .0825;
//Calculate the final selling price
selling_price = marked_price + tax;
//Display the tax and final selling price
cout << “Sales tax: “ << tax << endl;
cout << “Final selling price: “ << selling_price << endl;
return 0;
}
Legend
Key Word
Operator
Programmer defined name
Punctuation
Comment
Grouping symbols
Literal
Syntax vs. Style
Syntax
Style
• Required formatting
• Statements are terminated
with ‘;’
• () surround function
arguments
• {} enclose code belonging
to a unit
• ‘main’ function required
and is the entry point for
your program
• Key words are all lower case
• ‘,’ separates members of a
list
• Self or industry (or instructor)
imposed formatting
• Convention for naming
variables
• Named constants (to be
discussed later) are ALL CAPS
• Indentation required for
readability
• Note: C++ doesn’t care about
style. Only employers,
programmers AND instructors
reading the code care about
style
IDE – Integrated Development Environment
Eclipse
• Designed to maximize programmer productivity by providing
tightly-knit components with a similar user interface so the
programmer has to do less mode switching versus using discrete
development programs.
• Because an IDE is a complicated piece of software by its very
nature, higher productivity only occurs after a lengthy learning
process.
• Typically an IDE is dedicated to a specific programming
language. However, there are some multiple-language IDEs,
such as Eclipse, Oracle JDeveloper, recent versions of NetBeans
and Microsoft Visual Studio (many others).
We will be using Eclipse C++ IDE.
Two Kinds of Errors
Syntax errors – failure to follow the rules of
statement structure
Note: Program will usually fail to compile
Ex. missing semi-colons, un-paired grouping
symbols, undeclared variables, typos
Logic errors – program organization does not
lead to the correct result
Note: Program does compile but does not ‘work’
Operating Systems and Newlines
Newline – sequence of one or two non-human readable
characters indicating that a new line of text should begin.
CR – carriage return
LF – line feed
CR/LF – used in Windows and DOS operating systems
LF – used in Unix and Unix like operating systems
including Mac OS X
CR – used in Mac operating system prior to Mac OS X
Note: Eclipse creates and expects the Unix format. Files
created in the Eclipse IDE will have the Unix format.
Download