Writing Your First Programs Chapter 2 Mr. Dave Clausen La Cañada High School.

Slides:



Advertisements
Similar presentations
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Advertisements

If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Introduction to Computer Programming
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 © 2000 John Urrutia. All rights reserved. Qbasic Constructing Qbasic Programs.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Instructor - C. BoyleFall Semester
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 2: Java Fundamentals
Week 1 Algorithmization and Programming Languages.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Chapter 3 AS3 Programming. Introduction Algorithms + data structure =programs Why this formula relevant to application programs created in flash? The.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 Objects Types, Variables, and Constants Chapter 3.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Basic Elements of C++.
The Selection Structure
Chapter 2: Introduction to C++
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Mr. Dave Clausen La Cañada High School
2.1 Parts of a C++ Program.
Writing Your First C++ Programs
Chapter 3: Input/Output
Chapter 2: Introduction to C++.
Chapter 2 part #3 C++ Input / Output
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Writing Your First Programs Chapter 2 Mr. Dave Clausen La Cañada High School

Mr. Dave Clausen2 Program Development Top Down Design l Planning is a critical issue –Don’t type in code “off the top of your head” l Programming Takes Time –Plan on writing several revisions –Debugging your program l Programming requires precision –One misplaced semi-colon will stop the program

Mr. Dave Clausen3 Exercise in Frustration l Plan well (using paper and pencil) l Start early l Be patient l Handle Frustration l Work Hard l Don’t let someone else do part of the program for you. Understand the Concepts Yourself!

Mr. Dave Clausen4 Six Steps To Good Programming Habits #1 l 1. Analyze the Problem –Formulate a clear and precise statement of what is to be done. –Know what data are available –Know what may be assumed –Know what output is desired & the form it should take –Divide the problem into subproblems

Mr. Dave Clausen5 Six Steps To Good Programming Habits #2 l 2. Develop an Algorithm –Algorithm: a finite sequence of effective statements that when applied to the problem, will solve it. –Effective Statement: a clear unambiguous instruction that can be carried out. –Algorithms should have: a specific beginning and ending that is reached in a reasonable amount of time (Finite amount of time).

Mr. Dave Clausen6 Six Steps To Good Programming Habits #3 l 3. Document the Program –Programming Style –Comments –Descriptive Variable Names –Pre & Post Conditions –Output

Mr. Dave Clausen7 Six Steps To Good Programming Habits #4-5 l 4. Code the Program –After algorithms are correct –Desk check your program l 5. Run the Program –Syntax Errors (semi colon missing, etc.) –Logic Errors (divide by zero, etc.)

Mr. Dave Clausen8 Six Steps To Good Programming Habits l 6. Test the Results –Does it produce the correct solution? –Check results with paper and pencil. –Does it work for all cases? Border, Edge, Extreme Cases –Revise the program if not correct.

Mr. Dave Clausen9 Top Down Design l Subdivide the problem into major tasks –Subdivide each major task into smaller tasks Keep subdividing until each task is easily solved. l Each subdivision is called stepwise refinement. l Each task is called a module l We can use a structure chart to show relationships between modules.

Mr. Dave Clausen10 Top Down Design

Mr. Dave Clausen11 Top Down Design l Pseudocode –is written in English with C++ like sentence structure and indentations. –Major Tasks are numbered with whole numbers –Subtasks use decimal points for outline.

Mr. Dave Clausen12 Pseudocode Checkbook.cpp

Mr. Dave Clausen13 Writing Programs l C++ Vocabulary –reserved words have a predefined meaning that can’t be changed –library identifiers words defined in standard C++ libraries –programmer supplied identifiers defined by the programmer following a well defined set of rules

Mr. Dave Clausen14 Writing Programs l Words are CaSe SeNsItIvE –For constants use ALL CAPS (UPPERCASE) –For reserved words and identifiers use lowercase l Syntax –rules for construction of valid statements, including order of words punctuation

Mr. Dave Clausen15 Library Identifiers l Predefined words whose meanings could be changed. l Examples: –iostream cin cout –iomanip setprecision setw –cmath pow sin sqrt

Mr. Dave Clausen16 Identifiers l Must start with a letter of the alphabet or underscore _ (we will not use underscores to start identifiers) l aim for 8 to 15 characters l common use is to name variables & constants

Mr. Dave Clausen17 Basic Program Components l Comments l Preprocessor Directives l using namespace std; l Constant Declaration Section l Type Declaration Section l Function Declarations l Main Program Heading: int main( ) –Declaration Section (eg. variables) –Statement Section

Mr. Dave Clausen18 A Sample Program reserved words Reswords.doc

Mr. Dave Clausen19 Writing Code in C++ l Executable Statement –basic unit of grammar library identifiers, programmer defined identifiers, reserved words, numbers and/or characters –A semicolon almost always terminates a statement usually not needed AFTER a right curly brace } –Exception: declaring user defined types. l Programs should be readable noformat.cppformat.cpp

Mr. Dave Clausen20 Simple Data Types l Type int –represent integers or whole numbers –Some rules to follow: Plus signs do not need to be written before the number Minus signs must be written when using negative #’s Decimal points cannot be used Commas cannot be used –A comma is a character and will “crash” your program, no joke. Leading zeros should be avoided (octal or base 8 #’s) limits.h int_max int_min

Mr. Dave Clausen21 Simple Data Types l Type double –used to represent real numbers –many programmers use type float, the AP Board likes the extra precision of double –avoid leading zeros, trailing zeros are ignored –limits.h, float.h dbl_max, dbl_min, dbl_dig

Mr. Dave Clausen22 Simple Data Types l Type char –used to represent character data a single character which includes a space See Appendix 4 in our text –must be enclosed in single quotes eg. ‘d’ –Escape sequences treated as single char ‘\n’ newline ‘\’’ apostrophe ‘\”’ double quote ‘\t’tab ‘\\’ pathnames for files

Mr. Dave Clausen23 Simple Data Types l Strings –used to represent textual information –string constants must be enclosed in double quotation marks eg. “Hello world!” empty string “” new line char or string “\n” “the word \”hello\”” (puts quotes around “hello” )

Mr. Dave Clausen24 Output l #include –cout pronounced see-out –cout << ‘\n’; –cout << endl; –cout << “Hello world!”; –cout << “Hello world!” << endl; printadd2.cpp

Mr. Dave Clausen25 Formatting Integers l #include (input/output manipulators) l right justify output –cout << setiosflags (ios::right); l specify field width –cout << setw(10) << 100 (output: *******100, where * represents a space.) l specify decimal precision –cout<<setiosflags (ios::fixed | ios::showpoint | ios::right)<< setprecision (2);

Mr. Dave Clausen26 Setprecision l Precision is set and will remain until the programmer specifies a new precision –The decimal uses one position –Trailing zeros are printed the specified number of places –Leading plus signs are omitted –Leading minus signs are printed and use 1 position –Digits are rounded, not truncated.

Mr. Dave Clausen27 Test Programs l Test programs are short programs written to provide an answer to a specific question. l You can try something out l Play with C+ + l Ask “what if” questions l Experiment: try and see