16
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

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

Embed Size (px)

Citation preview

Page 1: 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

ProgramsA 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:

SystemMultitasking/Single TaskingMultiuser/Single User

Ex. operating system

ApplicationsEx. Word Processor

Page 2: 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

Characteristics of a Well Designed and Written Program

The program must operate properlyCorrectness – meets the specificationsIntegrity – does not create ‘side-effects’Reliable – does not fail (crash, blue-screen)Usable – easy to learn, screen presentationEfficient – make good use of resourcesSecure – protection from external threats

The program must be able to be maintained & revised

DocumentationOrganization

Other desirable characteristics: Expandability, Portability

Page 3: 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

Programming LanguagesA 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.

Page 4: 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

Overview of the Programming Process• Clearly define what the program should do

o State in writing what the program is to doo What information does it need? List the inputs and give them names.o How is it going to acquire that information? (keyboard, file, instrument, etc.)o How should the information be processed?o 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.)

• 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 conditionso flow charts – visual aid to help visualize the solutiono UML – Unified Modeling Languageo others

• Create and test the programo Write the code (text file*)o Save the fileo Compile it to create a machine readable formo 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

Page 5: 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

The Translation ProcessA human readable document containing instructions written in a high-level 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.

Page 6: 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

AlgorithmStep 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

Page 7: 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

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

Page 8: 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

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 formato Outputs produced

• Error code description• Other information required by the company

Page 9: 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

Compiler DirectivesCompiler 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;

Page 10: 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

Program BodyThe 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.

Page 11: 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

Elements of a C++ Program

• Key Wordso Reserved – cannot be used in other than in their intended fashiono Lower case – always written in lower case

• Operatorso Symbols such as +,-,*,=, etc. that require operands and ‘operate’ on the operands

• Programmer Defined Symbolic Nameso Cannot be the same as Key Wordso Case sensitiveo Usually follow a style dictated by your employer or instructor

• Punctuationo Semicolons, Commas

• Commentso /* 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

• Grouping Symbolso Used to enclose sections of code or items that belong togethero () and {}

Page 12: 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

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 WordOperatorProgrammer defined namePunctuationCommentGrouping symbolsLiteral

Page 13: 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

Syntax vs. StyleSyntax 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

Page 14: 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

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.

Page 15: 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

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 resultNote: Program does compile but does not ‘work’

Page 16: 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

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 systemsLF – used in Unix and Unix like operating systems including Mac OS XCR – 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.