1 / 31

CMPT 238 Data Structures C++ Review

CMPT 238 Data Structures C++ Review. Instructor: Tina Tian. Include Directives. #include < iostream > Preprocessor directive iostream is a library containing definitions of the input and output function Linker Appears at the start of the program, begin with # using namespace std ;

mark-barnes
Télécharger la présentation

CMPT 238 Data Structures C++ Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPT 238 Data StructuresC++ Review Instructor: Tina Tian

  2. Include Directives • #include <iostream> • Preprocessor directive • iostream is a library containing definitions of the input and output function • Linker • Appears at the start of the program, begin with # • using namespace std; • Tells the compiler to use names in iostreamin a “standard” way

  3. int main() • { //beginning of the main function • .... //statements • return 0; • } //end of the program

  4. The output function cout • c(C++ language)out(output) • c(C++ language)in(input) • cout<<whatever data you want to output; • cout<<“Welcome to CMPT102!”; • << represents the flow of the data.

  5. Input Function cin • cout: output to screen • cin: input from keyboard • cout<<whatever you want to output; • cin>>variable_name; • cin>>variable1>>variable2; • Orcin>>variable1; • cin>>variable2;

  6. Escape sequences • Start a new line • \n (need to go inside of the quotes) • endl • Escape sequence • \n new line • \t tab • \” “ • \\ \

  7. Comments • // This is a comment. • /* • This is a comment. • This is another comment. • */

  8. Combine Declaration and Assignment • int number = 3; • double rate = 0.07, balance = 0;

  9. http://software.intel.com/en-us/articles/size-of-long-integer-type-on-different-architecture-and-oshttp://software.intel.com/en-us/articles/size-of-long-integer-type-on-different-architecture-and-os

  10. The char Type • Short for character • Any single character from the keyboard • a letter, a digit, or punctuation mark • intnumber; char letter; • intnumber = 5;char letter = ‘a’; • char letter = ‘A’;

  11. The Class string #include <string> charletter; string message; charletter = ‘a’; string message = “hello”; • Concatenation • string firstName = “Chuck”; • string lastName = “Norris”; • string name = firstName + lastName; //string name = firstName + “ “ + lastName;

  12. The bool Type • Boolean • Two values • True • False • bool flag; • boolisPrime= false;

  13. Constants • double pi = 3.14159; • const double PI = 3.14159; • constdouble CELSIUS_TO_FAHRENHEIT = 33.8; • Old version of C++ compilers: • #define PI 3.14159

  14. Arithmetic Operators • +, -, *, / • int / int -> int • double / int-> double • 10/3, 5/2, 11/3 • (a+b)*c • (a-(b+c))*d

  15. Modulus % • 17 % 5 = 2 • 23 % 2 = ? • 20 % 3 = ? • How to determine if a number is even/odd?

  16. Multiwayif-else syntax • if (condition_1) • Action_1; • else if (condition_2) • Action_2; • ... • else if (condition_N) • Action_N; • else • Action_For_All_Other_Cases;

  17. Comparison Operators • > greater than • < less than • >= greater than or equal to • <= less than or equal to • == equal to • != not equal to • a = 2; • if (a == 2)

  18. The And Operator • If score is greater than 0and less than 100... • (score > 0) && (score < 100) • is true only if both are true

  19. The Or Operator • (score1 >= 60) || (score2 >= 60) • is true if one or both are true

  20. Syntax of the while Statement • while(Boolean_Expression) • { • Statement_1; // body • Statement_2; // iteration • ... • } • while(Boolean_Expression) • Statement;

  21. Using the while Loop for Input Validation cout<<“Guess a number between 0 and 10: "; cin>>number; while((number <0) || (number > 10)) { cout<<"Invalid Entry!“;cout<<"Guess a number between 0 and 10: "; cin>>number; } //Start playing the game

  22. Increment and Decrement Operators • Usually are used with int variables • count = count + 1; • count++; • count = count – 1; • count--;

  23. For Loop Dissection • The for loop uses the same components as the while loop in a more compact form • for (int n = 1; n <= 10; n++) Initialization Action Update Action Boolean Expression

  24. for(int n = 1; n < = 10; n = n+2) • for(int n = 0 ; n > -100 ; n = n-7) • for(int n = 0 ; n < 100 ; n = n*2) • for(double x = pow(y,3.0); x > 2.0; x = sqrt(x) )

  25. Nested Loops • for (inti = 1; i <= 5; i++)// outer loop • { • for (int j = 1; j<=5; j++)// inner loop • { • cout<<i<<“ “<<j<<endl; • } • }

  26. The break Statement • The break statement can be used to exit a loop. • Write a program to ask the user to enter 10 positive numbers and output their sum. • Terminate the loop when the user enters a negative value.

  27. The continueStatement • Can use continue to go to end of loop and prepare for next repetition

  28. Exercise: Pattern Displays • Write a C++ program that uses a loop to display the pattern below. • ++++++++++ • +++++++++ • ++++++++ • +++++++ • ++++++ • +++++ • ++++ • +++ • ++ • +

  29. Next class • C++ Review • Functions • Arrays • Pointers • OOP

  30. Homework • Review your old 102 assignments • Practice C++

More Related