Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List."— Presentation transcript:

1 C++ Basics L KEDIGH

2 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

3 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

4 IDOC and Comments CLASSROOM CONVENTIONS AND TERMINOLOGY

5 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 12 42 mph in a 30 mile zone the ticket would be 13 $20.00 + (12 X $5.00) = $80.00. The program should 14 prompt for the speed limit and the rate of speed for 15 the vehicle 16 */ //17

6 // //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 12 42 mph in a 30 mile zone the ticket would be 13 $20.00 + (12 X $5.00) = $80.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

7 Basic Components CLASSROOM CONVENTIONS AND TERMINOLOGY

8 // //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 12 42 mph in a 30 mile zone the ticket would be 13 $20.00 + (12 X $5.00) = $80.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 = 20.00 ; //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.

9 Terminology CLASSROOM CONVENTIONS AND TERMINOLOGY

10 #include //18 #include //19 #include //20 //21 using namespace std; //22 //23 int main(int argc, char *argv[]) //24 { //25 const double BASE = 20.00 ; //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.

11 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.

12 // //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 = 20.00 ; //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

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

14 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 ;

15 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

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

17 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

18 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

19 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

20 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

21 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

22 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

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

24 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)

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

26 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

27 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.

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

29 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.

30 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.

31 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.

32 Questions Multiple Choice True False and Completion


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

Similar presentations


Ads by Google