C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 2: Introduction to C++.
Introduction to C Programming
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
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.
Overview of C++ Chapter C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Bill Tucker Austin Community College COSC 1315
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 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Variables Mr. Crone.
LESSON 2 Basic of C++.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Chapter 2 - Introduction to C Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 Primitive Data Types and Operations
The Fundamentals of C++
Presentation transcript:

C++ Basics L KEDIGH

Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List and identify in examples all math, relational, logical, and equality operators. 3. Identify, describe, and provide examples of the three types of programming errors. Source Code used in this slide show Prog82a2 – multiple traffic tickets

Objective 1 Identify basic components of a c++ program using proper terminology and programming conventions adopted for this class. IDOC and Comments. Basic Components of a c++ Program. Terminology

IDOC and Comments CLASSROOM CONVENTIONS AND TERMINOLOGY

Types of Comments Line comment // comments out everything on the right of the // IDOC, magic numbers, end brace, etc. Debugging Block comment /* Covers multiple lines Program Description Sample Output Debugging /* //5 Many cities levy traffic fines based on a flat rate and 6 a per dollar charge for each mile over the speed limit 7 the vehicle was traveling. Write a program that 8 determines the fine based on the following: A charge 9 of $20.00 for the ticket and $5.00 for each mile over 10 the speed limit the vehicle was traveling. 11 For example: if a driver was ticketed for driving mph in a 30 mile zone the ticket would be 13 $ (12 X $5.00) = $ The program should 14 prompt for the speed limit and the rate of speed for 15 the vehicle 16 */ //17

// //1 // Date //2 // pl1 period //3 // contributors : //4 /* //5 Many cities levy traffic fines based on a flat rate and 6 a per dollar charge for each mile over the speed limit 7 the vehicle was traveling. Write a program that 8 determines the fine based on the following: A charge 9 of $20.00 for the ticket and $5.00 for each mile over 10 the speed limit the vehicle was traveling. 11 For example: if a driver was ticketed for driving mph in a 30 mile zone the ticket would be 13 $ (12 X $5.00) = $ The program should 14 prompt for the speed limit and the rate of speed for 15 the vehicle 16 */ //17 IDOC using 4 line comments Block Comment Open block comment Close block comment

Basic Components CLASSROOM CONVENTIONS AND TERMINOLOGY

// //1 // Date //2 // pl1 period //3 // contributors : //4 /* //5 Many cities levy traffic fines based on a flat rate and 6 a per dollar charge for each mile over the speed limit 7 the vehicle was traveling. Write a program that 8 determines the fine based on the following: A charge 9 of $20.00 for the ticket and $5.00 for each mile over 10 the speed limit the vehicle was traveling. 11 For example: if a driver was ticketed for driving mph in a 30 mile zone the ticket would be 13 $ (12 X $5.00) = $ The program should 14 prompt for the speed limit and the rate of speed for 15 the vehicle 16 */ //17 #include //18 #include //19 #include //20 //21 using namespace std; //22 //23 int main(int argc, char *argv[]) //24 { //25 const double BASE = ; //26 const double FINE = 5.00 ; //27 int sLimit,speed,numTickets; //28 double fine, total=0 ; //29 int setwSize = 35; //30 cout <<"How many tickets do you have to enter? "; //32 cin >> numTickets ; //33 //34 int i ; //35 for(i=1 ; i<=numTickets ; i++) //36 { //37 cout <<setfill('-')<<setw(25)<<left<< "Enter the speed limit:" ; //38 cin >> sLimit ; //39 cout <<setw(25)<<left<< "Enter the vehicle speed:" ; //40 cin >> speed ; //41 fine = (speed-sLimit)*FINE+BASE ; //42 if(fine>99) //43 cout <<setfill('-')<<setw(23)<<"Fine"; //44 else //45 cout <<setfill('-')<<setw(24)<<"Fine"; //46 cout <<"$"<<setprecision(2)<<fixed<<fine<<endl<<endl; //47 total += fine ; //48 }//end for i //49 //50 cout << setfill('=') << setw(35) << "" << endl; //51 cout << "Total amount of the fines - $" << total << endl; //52 cout << " Average fine is $" << total/(i-1) << endl; //53 //54 system("PAUSE"); //55 return EXIT_SUCCESS; //56 }//end main Input OutputVariable Declarations Preprocessor Directives IDOC C++ programs start with function named main.

Terminology CLASSROOM CONVENTIONS AND TERMINOLOGY

#include //18 #include //19 #include //20 //21 using namespace std; //22 //23 int main(int argc, char *argv[]) //24 { //25 const double BASE = ; //26 const double FINE = 5.00 ; //27 int sLimit,speed,numTickets; //28 double fine, total=0 ; //29 int setwSize = 35; //30 cout <<"How many tickets do you have to enter? "; //32 cin >> numTickets ; //33 const keyword Must be assigned a value. Compiler error : uninitialized const `x' insertion operator << – insert on the stream to the monitor or user. extraction operator >> – extracts from stream (user’s keyboard) and goes into the program.

What is a Programming Convention? An agreement, compact, or contract. A rule, method, or practice established by usage; Example: the convention of showing north at the top of a map. General agreement or consent; accepted usage, especially as a standard of procedure.

// //1 // Date //2 // pl1 period //3 // contributors : //4 #include //18 #include //19 #include //20 //21 using namespace std; //22 //23 int main(int argc, char *argv[]) //24 { //25 const double BASE = ; //26 const double FINE = 5.00 ; //27 int sLimit,speed,numTickets; //28 double fine, total=0 ; //29 int setwSize = 35; //30 //31 cout << setfill('=') << setw(35) << "" << endl; //51 cout << "Total amount of the fines - $" << total << endl; //52 cout << " Average fine is $" << total/(i-1) << endl; //53 }//end main IDOC class convention Preprocessor Directives Function header or signature Function Body End brace and comment Also known as Compiler Directives Also know as Header Files or include files

Objective 2 List and identify in examples all math, relational, logical, and equality operators.

Operators Math Operators * / + - % (Modulus or Mod) Logical Operators && - AND || - OR ! – NOT Equality Operators == equals != not equals Relational Operators < <= > >= Assignment Operators -= += *= /= %= sum = sum + num ; Could be written sum += num ;

Order of Operations [] (). Left to Right new ! (cast) Right to Left * / % Left to Right + - Left to Right >= Left to Right == != Left to Right &&Left to Right ||Left to Right = += -= /= *= %= Right to Left

Evaluate the Expression double ans = 4+8/3+1 //integer division double ans = double ans = 6+1 double ans = 7

What is the output? int a=3, b=0, c=-1, d=4; if( a*c>d+c ) cout <<"True"<<endl; else cout << "False"<<endl; False

What is the output? int a=3, b=0, c=-1, d=4; if( d+c>a+b ) cout <<"True"<<endl; else cout << "False"<<endl; False

What is the output? int a=3, b=0, c=-1, d=4; if( a-c>b+c ) cout <<"True"<<endl; else cout << "False"<<endl; True

What is the output? int a=3, b=0, c=-1, d=4; if( a*c>d+c || c!=-1 ) cout <<"True"<<endl; else cout << "False"<<endl; False

What is the output? int a=3, b=0, c=-1, d=4; if( a*c>d+c || c!=-1 && a==d+c) cout <<"True"<<endl; else cout << "False"<<endl; False

What is the output? int a=3, b=0, c=-1, d=4; if( a*c>d+c && a==d+c || c!=5 ) cout <<"True"<<endl; else cout << "False"<<endl; True

Objective 3 Identify, describe, and provide examples of the three types of programming errors.

Compiler or Syntax Errors Compiler does not compile or run. cout << “Enter a number “ expected `;' before "if" Cout <<"Enter "<<end; `Cout' undeclared (first use this function)

Logic Errors Determine which of two variables contains the largest value.

Logic Errors Program runs but provides incorrect results. int a=3, b=3; if(a>b) cout << a <<" is greater than " << b <<endl; else cout << b <<" is greater than " << a <<endl; Output 3 is greater than 3 Press any key to continue... This is not what is wanted. Programmer didn’t consider two number of equal value

Logic Errors int a=3, b=3; if(a>b) cout << a <<" is greater than " << b <<endl; else If(a==b) cout << “Both have the same value " << endl; else cout << b <<" is greater than " << a << endl; Output Both have the same value Press any key to continue... a is not larger than b so go to the else. Check to see if they have the same value. The only possibility left is b has a larger value than a.

Runtime Errors Division by zero is the most common. int a=4, b=3, c=-1, d=4; cout << b/(a-d)<<endl;

Identifier or Variable Naming Rules. Variable names can be longer than 255 characters but only the first 255 are recognized by the compiler. All variable names must begin with a letter of the alphabet or an underscore( _ ). After the first initial letter, variable names can also contain letters and numbers. No spaces or special characters, however, are allowed. You cannot use a C++ keyword (reserved word) as a variable name. Will not compile if not followed.

Naming Convention for Variables lowerCamelCase Single word names consist of lower case letters and in names consisting of more than one word all, except the first word, are capitalized as in colMeans or totalFreshmanStudents. This naming convention is used for function names in c++, Java and JavaScript. Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters. Uppercase characters are distinct from lowercase characters. Using all uppercase letters is used primarily to identify constant variables. Will compile if not followed.

Naming Convention for Functions Functions should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. run(); runFast(); getBackground(); Will compile if not followed.

Questions Multiple Choice True False and Completion