Chapter 1 - What s in a program?

Similar documents
Chapter 2 - I know what I want to do NOW WHAT? Student Learning Outcomes (SLOs)

Program Organization and Comments

Understanding main() function Input/Output Streams

How to approach a computational problem

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

To become familiar with array manipulation, searching, and sorting.

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Lab # 02. Basic Elements of C++ _ Part1

Computer Science II Lecture 1 Introduction and Background

First C or C++ Lab Paycheck-V1.0 Using Microsoft Visual Studio

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

QUICK EXCEL TUTORIAL. The Very Basics

Your First C++ Program. September 1, 2010

C++ Support Classes (Data and Variables)

Maciej Sobieraj. Lecture 1

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Lab: Supplying Inputs to Programs

My First Command-Line Program

These are notes for the third lecture; if statements and loops.

DOWNLOAD PDF MICROSOFT EXCEL ALL FORMULAS LIST WITH EXAMPLES

4. Structure of a C++ program

2 nd Week Lecture Notes

Chapter 1 Introduction

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Separate Compilation of Multi-File Programs

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Starting to Program in C++ (Basics & I/O)

Add Subtract Multiply Divide

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Review. Relational Operators. The if Statement. CS 151 Review #4

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Lesson 14 Final Exam Project Directions CIS105 Survey of Computer Information Systems

Programming. C++ Basics

Chapter 1 INTRODUCTION

EXCEL BASICS: PROJECTS

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Computer Programming : C++

Variables. Data Types.

Focus Group Analysis

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Learning Worksheet Fundamentals

Introduction to Programming using C++

Table of Contents. 1. Creating a Microsoft Excel Workbook...1 EVALUATION COPY

Excel 2016 Basics for Windows

Chapter 11 Pointers Part 3 (Putting it all together) Student Learning Outcomes (SLOs)

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

VARIABLES & ASSIGNMENTS

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Expressions, Input, Output and Data Type Conversions

Microsoft Excel Level 2

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Lesson 2 Variables and I/O

Chapter Four: Loops II

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Programming with C++ Language

Access: You will have to

LAB: INTRODUCTION TO FUNCTIONS IN C++

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

INFORMATION SHEET 24002/1: AN EXCEL PRIMER

Why Is Repetition Needed?

Chapter 2 C++ Fundamentals

Computer Science is...

conditional statements

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Object-Oriented Programming, Iouliia Skliarova

download instant at Introduction to C++

Microsoft Access XP (2002) - Advanced Queries

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Computer Programming

Cpt S 122 Data Structures. Introduction to C++ Part II

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Lab 1: First Steps in C++ - Eclipse

T H E I N T E R A C T I V E S H E L L

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Chapter 1 Introduction to Computers and C++ Programming

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Scientific Computing

CE221 Programming in C++ Part 1 Introduction

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

Civil Engineering Computation

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Lab 4: Introduction to Programming

PIC 10A Objects/Classes

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

Chapter 1 Introduction to Computers and Programming

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Transcription:

Chapter 1 - What s in a program? I. Student Learning Outcomes (SLOs) a. You should be able to use Input-Process-Output charts to define basic processes in a programming module. b. You should be able to use a generic Screen design sheet to create screen design sequences for a programming project. c. You should be able to identify the basic components of a function and describe the importance of the main function. d. You should be able to write a program in C++ that prints phrases of your choice. II. Demonstration Program Here is a working program in C++. It was written using Microsoft s (MS) Visual Studio. It can be created using other compilers the only MS eccentricities are the use of the stdafx.h and the use of int _tmain(int argc, _TCHAR* argv[]). The #include "stdafx.h" can be commented out and int _tmain(int argc, _TCHAR* argv[]) can be replaced by int main ( void ). 1. //The following is a first program 2. //Using your editor type it in and 3. // see what happens 4. #include "stdafx.h" 5. #include <iostream> 6. using namespace std; 7. int _tmain(int argc, _TCHAR* argv[]) 8. { 9. cout << "Press <ENTER> to continue" << endl; 10. cin.get(); 11. return 0; 12. } Program 1.1 Output with interactive Input.

Line-by-Line Description 1. Comment which is noted by the // preceeding the text in the line. The comment is terminated by the end of the line. MS Visual Studio notes comments in green font color. 2. Another comment. 3. And another 4. This is a Preprocessor. Preprocessors are noted by the # symbol. This is an instruction to the compiler which is executed before any of your code is reviewed. In this case #include says that a file (or code) is to be incorporated into your project. Include is a Reserved Word. Reserved words can only be used in specific ways. MS notes reserved words with a blue color. Note the quote marks. The stdafx.h file is MS specific and may cause a compile error in other systems. This line can be commented out. If it is included, it MUST be the first include. 5. This is another #include but the quote marks are different they look like < >. The difference is the location of the file to look in. The means look in the current working directory in this case the hello directory (generally when we refer to project we really mean project directory, or just directory ). The < > indicates the file is located in a folder specified by the Integrated Development Environment (IDE) not in our working directory. MS uses the brown color to indicate a string (character sequence) constant. In both lines 4 and 5 the file names are brown. 6. In this line there are two reserved words using and namespace. Although somewhat beyond the scope of this course, a namespace is an organization mechanism for large files and allows you to selectively access components without loading the entire file. In this case we will be using the std component of the iostream header file. The std character sequence is in black. This indicates, other than being the assigned name to the namespace, it has no special meaning to the compiler. 7. This is the beginning of what we think of as the program. Actually it is the identification (name) given to the block of code that follows in lines 8 through 12. We will be more specific later. This sometimes is termed the function heading. The function is the

primary programming module in C and C++. The function heading is made up of 3 parts: return type, function identifier, and parameter list. a. Return type is the data type of the value created as a result of executing the function (in this case an integer). Note that int is a reserved word. b. Function Name is the identifier (name) we (the programmer) give the block of code (function). c. Parameter list is enclosed by parentheses ( ). The parameter list indicates the values required for the operation of the function. It includes the data type and identifier used to reference the data within the block of code. More about this later. 8. The opening brace ( { ) identifies the beginning of the block of programming (code). Notice the closing brace ( } ) in line 12. This is where the program (block of code) takes care of business. 9. This is the first executable line of code in our programming block. The first character sequence cout is linked to the default output device, the screen (console output). The next symbol is the insertion operator, <<. Its job is to insert the following stuff into the output stream, sending it to the specified device. The third component (in quotes) is the data that is to be inserted into the output stream and to be sent to the device, in this case - Press <ENTER> to continue. Once again we see the insertion operation operator. Then the final instruction, endl, means end of the line and sends a carriage return to the output device. The last character, the semi-colon ( ; ), terminates the line of executable code. We will see other lines of instruction that do not have semi-colons after them. These will be lines of control code. They will control other executable instructions. You can preview an example of these in the program sample for chapter 2. 10. This line of executable code (noted by the ending semi-colon) is actually an input instruction that we are using to tell the computer to wait for the enter key to be pressed to continue. The cin refers to the default input device, the keyboard (console input). The.get() is a component (member function) of cin that allows the program to

capture a single character. We are not doing anything with the character (for example assigning it to memory) just throwing it away. Your turn: After you have typed in the program above, compiled it and seen the results try the following modifications recompile and execute after each step. 1. Add a line of code above line 9 to output your name. 2. Add a line of code that puts a blank line between your name and the Press <ENTER> to Continue prompt. 3. Try modifications of your own (one line-at-a-time), compile, and execute. Now let s see what errors do to our output. 11. Remove a semi-colon from one of the lines of executable code and compile. Did you see the message window? Look at the first error message (sometimes the first two messages) it s the most important message. Sometimes you can get 20 or more messages for a single error. The first one or two messages are the most significate. In this case your error message might look something like the following depending on your compiler >c:\users\ed\documents\visual studio 2005\projects\hello\hello\hello.cpp(13) : error C2146: syntax error : missing ';' before identifier 'cin' The bold is my addition for emphasis. 12. Correct this error and create a different error. Get used to using and understanding the messaging system for your compiler. After seeing the actions with a single error, experiment with two errors. Just remember it is the first couple of messages that count

Program design First it is critical to understand how a program relates to the computer (via the compiler). Think of the computer as a very obedient 2-year old. You have to tell them exactly, step by step, how to do something. If the computer (compiler) doesn t understand, it throws a fit (error messages). If you make assumptions between steps the computer does not understand, and guess what ---? So what do you have to do? You have to think about how you are going to tell the computer how to do something this is called the design. A program has to be designed. One of my mentors once said, the sooner you get on the computer, the longer it takes. It was not until years later that I understood. The purpose of the design is two-fold: mostly for our benefit to remember what we were doing and to keep us on task, then to communicate our ideas to other folks. The two initial design tools we will consider is the Input-Process- Output (IPO) chart and the story board. 1. The IPO chart Programming modules (with a few exceptions) require some data to be available before anything can be done. An analogy You are a mathematical genious, you can add 7- digit numbers in your head. However no one has given you any numbers to add. How much work can you expect to accomplish? Nada ( nothing in Spanglish). Additionally, even if you have data it might not be what you need for a particular outcome. Another analogy You are that same mental giant and someone has given you a pile of numbers but they ask for the descriptions for the widgets in stock!!! What?? I can t do that with the data you gave me. The data is not appropriate for the needed outcome. How much work can be accomplished?

A third analogy Assume you have no idea as to how to add numbers for you 2 plus 2 is hyroglyphics. You are give a pile of numbers and are asked to generate a normal distribution of the values. What s that you say??? Once again it ain t happening. The moral here is that you (and the computer) have to have data to work with, have to have some attainable out come ( we can get there from here ), and we know how to do the work. We have input, know the process, and can create the desired output (I/P/O). I tend to create an IPO chart for every function (module) I create. My approach is to indicate the data that I have to work with and the desired output first. Then I work on the process. The process may have to be broken into multiple steps (eventually functions) to operate effectively. An IPO chart might look like Input (1 st ) Process (3 rd ) Output (2 nd ) List the data to work with The anticipated operations to be performed Expected outcome(s) 2. The story board Once we have an idea as to what we are going to do, the next step is to figure out what the screens might look like. The sequence of screen designs is the story board. I use a template that was created in Excel <refer to the class Resources page>. The following is a sample screen design sequence for an input screen (user prompts, etc.) and a design for displaying the resulting output. Because the user does not generally have anything to do with the processing, there is typically not screen design for the process (although not always).

Putting it altogether The following is a scenario that incorporates the use of an IPO chart and associated story board design for a program that prompts a user for a series of numbers, calculates an average, and displays the resulting output. I understand that this is probably over-kill for this kind of program but we are talking about a concept and an approach that will be useful as the projects get more substantial. I have added a third, splash screen design. The splash screen will act as an introduction to our program. As conceived, it is simply a series of output statements (cout) with a prompt and trap for the ENTER key (see above). IPO Input (1 st ) Process (3 rd ) Output (2 nd ) Prompt for each value ---------- Allow the user to enter the value Add each input value to a total For each value add 1 to a counter ----------- After all values are entered divide the total by the counter and assign Display the average The Database In the following sequence of chapters a data base design and development scenario will be followed. A number of different scenarios will be suggested in an appendix. Any of the scenarios can be developed in a similar manner. In real-time one would probably not follow the chapter sequence lock-step because in the text we are interested in using the development of a database project as an instructional tool also, presenting a number of techniques which may or may not be incorporated as the situation dictates.

The Database scenario (Employee Tracking System) The purpose of the employee tracking system is to keep track of employee data including contact information, date of hire, and position. The company has two categories of employees (for our purposes) managers and sales personnel. Additionally the company has an employment part-time status and a full-time status which are used to calculate pay rates. All data will be entered as dynamic strings (covered in the next chapter) and converted (as necessary) into numeric equivalents. The following data items will be required for each employee: Employee Number (key field) unique values Last Name First Name Address Zip Code Job Position (M/S) Job Status (F/P) Hours Worked Job Position and Job Status will be used to determine the pay rate of the individual employee. The pay rate will be multiplied by the number of hours worked to determine the total pay for this pay period. The pay rates are determined as indicated in the table below. The measure is in dollars per hour. Job Status Job Position Manager Sales Full Time 27.50 17.00 Part Time 19.75 10.50 At this point we are expected to generate an IPO chart, a splash screen design, input screen design, and an output screen design.

IPO Chart for the Employee Tracking System. Input (1 st ) Process (3 rd ) Output (2 nd ) Last Name First Name Address Zip Job Position Job Status Hours Worked (no processing) -> (no Processing) (no processing) (no processing) (no processing) (no processing) (no processing)-> Last Name First Name Address Zip Job Position Job Status Hours Worked Pay for the period The words No Processing do not normally appear. Typically you see an arrow passing through the Processing column. Samples of possible screen designs. Figure 1. Sample Splash Screen for the Employee Tracking System using the Excel template.

Figure2. Sample input screen for the Employee Tracking System. Figure 3. Sample output screen for the Employee Tracking System

Programming/design considerations and Tips 1. IPO chart make sure all input data is listed in the input column; all expected outcomes for this module are indicated in the output column; and all processes are indicated in the process column. 2. Screen design For this project you should be designing for a text screen with 25 rows and 80 characters (columns) in each row. Remember the computer prints from the upper left corner to the lower right corner, one row at a time (at this point no going back). a. Splash Screen try to use the whole screen. In the design template a single character can be in a cell of the worksheet. To quickly figure the center position (assuming a 80-character column) subtract the number of characters in your string from 80. Then divide the result by 2. If rounding is required, it does not really matter if you round up or down just be consistent. DO NOT bunch all of the presentation in the upper left corner of the screen. Include the project title, the developer(s), and other pertinent information. Provide a prompt for the user. b. Input Screen this is the first of the standardized screens. The necessary components are project title, screen title, user instructions, main presentation, user -prompt to continue. In this case the instructions appear after the screen title is printed in anticipation of the data prompts. It is not a good idea to automatically transition to the next screen after the last data item input. c. Output Screen is essentially the same as the input screen. This will be the same for all subsequent screens. The pay will be a result of multiplying the hours worked and the pay rate. The pay rate will be determined using nested if-else structures (discussed in the next chapter).