Scott MarinoMSMIS Kean University
MSAS5104Programming with Data Structures
and AlgorithmsWeek 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
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
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
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
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
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++
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
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
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
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
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
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?
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
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++
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
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
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
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
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
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
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
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
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
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
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
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
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}
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
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
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
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
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
Scott MarinoMSMIS Kean University
Executing a program
• From the command line type “program”– This will execute the program
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
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
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
Scott MarinoMSMIS Kean University
Homework / Lab 1
• Turn in– Program source code
– Execution results
– Write down the compiler used