Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3...

38
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino

Transcript of Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3...

Page 1: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

MSAS5104Programming with Data Structures

and AlgorithmsWeek 3

Scott Marino

Page 2: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Topics

• Computer languages

• What is a program?

• Modular design

• The programming process

• The C++ language

• Values in C++

• Variables in C++

• Creating a program

• Compiling a program

• Lab 1 / Homework

Page 3: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Computer Languages

• First generation language– Each CPU has a fixed set of instructions hard-coded

into it in a language known as machine language• The only language the computer truly understands

• The only instructions the computer can execute

• Consists of a series of “offs” and “ons”

– A single instruction to a computer such as - display a character on the screen may require dozens of instructions in machine language

– Nobody programs in machine language

Page 4: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Computer Languages

• Second generation language– Assembly languages are one step removed from the

machine languages

– The programming logic behind assembly language is much the same as machine language

– The actual machine code language is translated to characters

• Machine language = 0011011011

• Assembly Language = ADD

Page 5: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Computer Languages

• Second generation language– A program called an “assembler” translates the

assembly language program into machine code

– Assembly languages are written to mimic the computers machine language

• Usually provides the most efficient programs because it is written with the same logic as machine language

• Development time is slower because of the detailed instructions required to perform a task

Page 6: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Computer Languages

• Third and Fourth generation languages– Designed for humans to work with

– English-like syntax

– A single 3GL command can translate to dozens of machine code instructions

– C, C++, COBOL, JAVA, Pascal, Perl, etc.

– Despite being English-like, strict syntax is required to write a program that can be translated properly

Page 7: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

What is a program?

• To us, a program is simply a set of textual instructions, saved as a file

• Many steps are required to translate the textual program into a form that the computer can use

• Different languages all undergo a similar process for creating executable programs

• For this class, we will focus on C++

Page 8: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

What is a program?

• The process of creating an executable program– Starts with creating a set of instructions in a

programmable language

– The set of instructions is saved as a text file known as “source code”

– A program known as a compiler is invoked to translate the source code to “object code”

• The compile step will produce an object module or return the syntax errors encountered

Page 9: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

What is a program?

– A program known as a linker is invoked to add “system modules” to the object module

• I/O routines and other required pre-written code

• Each executable program is typically linked on the computer in which it will execute– Sometimes it can be linked on a similar machine and

“ported” to other machines

• Testing is required of the program– Just because it compiles and links does not mean it is

doing what it was intended to do

Page 10: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

What is a program?

• From the 1940’s to the mid-1990’s, most program creation was done as individual tasks– Write program, save it, invoke compiler, invoke linker

– Separate debugging and testing tools

• In the mid-1990’s, integrated development environments began to emerge as PC’s became more common– In a single environment, you can write a program, click

the save icon, and click to compile and link

– Integrated debugging and testing tools

Page 11: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Modular Design

• Many programming tasks are quite large in scope– MS-Windows, Word, Excel, Accounting, games, etc

– Consists of many thousands of lines of code

• Take a complex task and break it into smaller pieces known as modules

• Combine to individual modules to create the final program

• Step-wise refinement– Break each task down into increasingly smaller sizes

Page 12: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Modular Design

• A trend has been towards object-oriented programming– Treat everything as an object

– Determine the properties (attributes) for the object

– Define the behaviors (methods) for the object

– Create a published/public interface

– Encapsulate (hide) the details of the code from view

– Abstract - we know how to use the objects, but don’t care how they do what the do

• Objects are written to be re-usable

Page 13: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

The programming process

• Creating a program is a balance of several factors in the business environment– Execution efficiency

• Will it run fast enough?

– Programming efficiency• Will it take too long to write?

– Maintainability• Can it be easily updated to accommodate change?

– Code re-use• Is there any code already written that can be re-used?

Page 14: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

The programming process

• Task– Investigate the situation

• Analysis– Research the problem and create a plan to solve it

• Design– Develop the procedure to solve the problem

• Implementation– Write the actual code

• Test

Page 15: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

The C++ language

• 1967 - BCPL (Basic Combined Programming Language) commonly referred to as B

• 1972 - Dennis Ritchie improves B and calls it C• 1983 - C is enhanced and C++ is created as the

next generation of C– The ++ operator in C is “increment by 1”

• There are ANSI (American National Standards Institute) standards written for C, but there are none for C++

Page 16: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

The C++ language

• C++ was designed to be a modular language• The C++ compiler is flexible with spacing, blank

lines, and indentation, however, it is desirable to write programs that are neat, organized, and well documented

• Program source code should follow an outline format with each level or subordinate topic being indented for readability

Page 17: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

C++ Anatomy

• Comments– Comments are used in a program to explain the purpose

of a command or function or to document other important details of the program

– The “//” operator is used to denote the start of comments

• Each comment line ends with the first carriage return

– The /* …comments … */ is operators are used to create blocks of comments

• Can span multiple lines or be used on a single line

Page 18: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

C++ Anatomy

• Directives– Directives are instructions to the compiler to perform

certain actions

– In C++ each directive starts with the “#” (pound) sign• #include <iostream>

– Directives can add common modules to a program or can perform conditional compilation

Page 19: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

C++ Anatomy

• Statements– Statements in C++ are code that, when compiled,

perform specific actions

– test_grade = 99; // assign 99 to test_grade

– All statements end with a “;” (semicolon)

– Statements can span more than one line• Quoted data cannot span multiple lines

Page 20: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

C++ Anatomy

• Objects– C++ has a limited number of statements, but has a large

number of objects

– Objects have behaviors and properties

– cout << “printing something\n”;• cout is an object

• the << is the insertion operator

• the quoted data prints

• the \n is a new line character

• the line ends with a semicolon

Page 21: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

C++ Anatomy

• Functions– A function is a set of instructions that performs a single

operation

– Functions can include statements, objects, references to other functions, machine language, and other “stuff”

– Functions are used to create “modular” code

Page 22: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

C++ Anatomy

• Functions– int main(void)

{… some code …}

– Every C++ program must contain only one “main” function

– Program execution begins with the main function and executes all code within the curly braces

Page 23: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

C++ Anatomy

• Declarations– Variables and functions must be “declared” before you

can use them in a program

– int test_grade;• Declares a variable named test_grade

– Header files included into a program contain declarations for common functions and are typically self contained

• They include declarations for all the variables they require

– The cout function is declared in the “iostream” header

Page 24: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Values in C++

• Numeric values– There are real numbers and integer numbers

– Real numbers contain decimal points

– Integers are whole numbers• Sometimes referred to as the counting numbers

– Can be negative or positive

Page 25: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Values in C++

• Character values– By enclosing a value in single quotes ‘4‘, you tell the

C++ compiler to refer to the ASCII value for the data inside the quotes

– All character references MUST be enclosed in single quotes

• A numeric 4 is different than a character ‘4’

– C++ allows funny things to happen when mixed data types are used in a single statement

Page 26: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Values in C++

• Special Characters– Generally not printable on

the screen and cannot be typed

• \0 -- null value

• \a -- audible (bell)

• \b -- backspace

• \f -- form feed

• \n -- new line

• \r -- carriage return

• \t -- horizontal tab

• \v -- vertical tab

• \? -- print question mark

• \\ -- print backslash

Page 27: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Values in C++

• String– A string is a set of values enclosed within “double

quotes”

– “Escape” special characters inside of strings by using the backslash

• question marks, carriage returns, etc.

• Keep in mind, string values have slightly different properties and methods than character and numeric data

Page 28: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

A simple program

• The Hello World example• #include <iostream>

using namespace std; // for some compilersint main(void) // main program function { cout << “Hello World!\n”; return 0; // end program with 0 return code}

Page 29: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

A simple program

• Telnet to eve.kean.edu– Some basic UNIX required

• Using “pico”, create a file called hello.cpp containing the hello world program

• Compile the program• Execute the program

Page 30: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Connecting to Kean

• Run PuTTY– Configure a session to connect to eve.kean.edu

– Connect as “ssh” which connects to port 22

Page 31: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Introduction to Unix

• mv Move or Rename a file• cp Copy a file• rm remove or delete a file• ls -rtl list directory contents• mkdir make a directory• cd change directory• passwd change your password• exit close the session / logoff

Page 32: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

PICO Edit

• From the command line enter “pico file.name”

• Use arrow keys to navigate around the screen

• Backspace and delete keys can be used to correct typing mistakes

• Ctrl-O will save your file

• Ctrl-X will exit the editor

• Search the web for tutorials for using PICO

Page 33: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Compiling a program

• In UNIX– g++ -oprogram program.cpp

– invokes the UNIX C/C++ compiler

– -oprogram tells the compiler to create the executable program as “program”

– program.cpp is the file name for the source program

• Make sure not to create the executable program with the exact same name as the source program

Page 34: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Executing a program

• From the command line type “program”– This will execute the program

Page 35: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Tips and tricks

• Open 2 UNIX sessions one for editing and one for compiling– Save the program (Ctrl-O) on one session and toggle to

the other to compile

• Avoid creating each program from scratch– Copy an existing program to the new name

• Pick an identifiable name for your programs• Create a directory for each program• KISS - Keep it simple

Page 36: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Printing the results

• Cut/Paste the results into notepad or other text editor– Highlight the text in putty to put it into the windows

clipboard

– Toggle to the editor and click paste

• Format as courier or a fixed width font for easier viewing

• Print

Page 37: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Homework / Lab 1

• Write a program to print the following:– Your Name

– Phone Number

– City, State Zip

– Must have all the punctuation, end of lines, formatting, etc.

• Program comments must include:– // Student name and e-mail

// Assignment N// Date// Description of program

Page 38: Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 3 Scott Marino.

Scott MarinoMSMIS Kean University

Homework / Lab 1

• Turn in– Program source code

– Execution results

– Write down the compiler used