Fundamentals of C++ Programming - 03: Control Structures (part 2) - Dustin Nguyen, PhD

Structured data types
❖ Array
❖ Struct
❖ Basic control structures in C/C++
❖ Loop statements: while, for, do-while
❖ Structure programming 
pdf 51 trang xuanthi 27/12/2022 1460
Bạn đang xem 20 trang mẫu của tài liệu "Fundamentals of C++ Programming - 03: Control Structures (part 2) - Dustin Nguyen, PhD", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdffundamentals_of_c_programming_03_control_structures_part_2_d.pdf

Nội dung text: Fundamentals of C++ Programming - 03: Control Structures (part 2) - Dustin Nguyen, PhD

  1. Outcomes ❖ Using array, string, and structured data types ❖ Solve the problem using loop structures ❖ Implement program with loop structures: ❖ while, for, do-while ❖ Understand the role of algorithm in problem solving process 2
  2. Structured data types
  3. Structured data types ❖ Array: a sequence of memory slots that contains a specific data type ❖ [ ]; ❖ int Fibonacci[MAX_LENGTH];/* declare an integer array of MAX_LENGTH elements. This is a static declaration! */ ❖ * ;// alternative declaration, a pointer ❖ float *plotY; 6
  4. Structured data types ❖ Array: initialization ❖ At declaration time (static) ❖ int sNum[5] = {5, 6, 9, 2, 1}; ❖ float x[] = {0.1, 3.2, 5.7, 7.2};// allocated 4 elements ❖ Dynamically allocate ❖ float *pNum; pNum = new float[N]; 8
  5. Structured data types ❖ String: ❖ char strName[50];// undefined string ❖ char strName[50] = “Dustin”; ❖ char strOutText[] = “This text contains 32 characters”;//33 bytes ❖ char *pStr = “Unknown”; 10
  6. Structured data types ❖ String: ❖ string sText;// empty string “” ❖ string sText = “A C++ class that stores characters.”; ❖ sText.size, sText.length, sText.max_size, sText.resize, etc. ❖ sText[index], sText.at(index) ❖ sText += anotherText; ❖ sText.push_back, sText.pop_back, sText.find, sText.copy, etc. 12
  7. Structured data types ❖ typedef: define a data type ❖ typedef struct{ char name[30]; } StdName_t; StdName studentList[50]; ❖ typedef struct Student { int ID; char name[50];// can you use string? Why should you use? } Student_t; Student_t studentList; 14
  8. while statement ❖ Why do we need iterations? ❖ Waiting for something to happen ❖ Operate on several objects ❖ List, array of objects ❖ String 16
  9. while statement ❖ Flowchart while statement N Y statement statement 18
  10. while statement ❖ Breaking the rule ❖ while ( ) { ; if ( ) continue; ; } 20
  11. while statement ❖ Note: ❖ Remember to initialize variables in the condition expression before entering the while statement (at least you know what will happen when you check the condition). ❖ Do not forget stopping condition. ❖ Take care of counters. ❖ Use infinite loop wisely. 22
  12. while statement N Y statement N Y statement statement statement 24
  13. for statement ❖ Why do you need for statement? ❖ Just another way to write iteration/loop structure! ❖ Counting is a frequent activity ❖ for: a specialised loop that package the following tasks in a statement ❖ Initialise a counter variable ❖ Modify the counter ❖ Check complete condition 26
  14. for statement ❖ Flowchart initialization N modification Y statement statement 28
  15. for statement ❖ Nested loop: #include #include #include #include int main() { int main() { int img[12][16]; int img[12][16]; int i = 0, j; for (int i = 0; i < 12; i++) { for (; i < 12; i++) { for (int j = 0; j < 16; j++) { for (j = 0; j < 16; j++) { img[i][j] = rand() % 256; img[i][j] = rand() % 256; } } } } return 0; return 0; } } 30
  16. do-while statement ❖ do-while: do first, check later ❖ A convenient way to perform some operations ❖ E.g.: ❖ Asking user to input some values ❖ Check if any input value was invalid ❖ Loop to input again 32
  17. do-while statement ❖ Flowchart statement Y N statement 34
  18. do-while statement ❖ Nested loop ❖ do { ; do { ; while ( ) { ; } } while ( ); ; } while ( ); 36
  19. Structure programming ❖ Definition: a programming paradigm aimed at improving the clarity, quality and development time of a computer program by making extensive use of subroutines, block structures and for/while loops ❖ Structured programming languages: ALGOL, Pascal, PL/I, Ada, C/C++, etc. 38
  20. Structure programming ❖ Loop and array ❖ Loop is good for performing operations on arrays, strings. ❖ “while”, “do-while”, “for” are exchangeable. ❖ Fixed size data should be processed using finite loops. 40
  21. Issues ❖ Infinite loops: ❖ Sometimes your code is stuck in an infinite loop due to logic errors ❖ Factors: input, user interaction, or special computation ❖ E.g.: ❖ nInputs = 0; for (int i = 0; i > list[i]; nInputs++; } 42
  22. Priority of operators # Operator group 1 :: scope 2 ++ () . -> (postfix) unary 3 ++ ~ ! + - & * new delete sizeof (cast) (prefix) unary 4 .* ->* pointer to member 5 * / % arithmetic: scaling 6 + - arithmetic: addition 7 >> = relational 9 . == != equality 10 & AND 11 ^ XOR 12 | OR 13 && conjunction 14 || disjunction 15 . = *= /= %= += -= >>= <<= &= ^= |= ?: assignment level expressions 16 , sequencing 44
  23. Problem solving - example ❖ Input and draw the following figure in terminal: ❖ Input: N (number of lines) ❖ Output: (in case N = 5) * * 46
  24. Problem solving - example ❖ Parse strings ❖ The command line is given as follows: ❖ [ ] ❖ E.g.: test -o log.txt -d ❖ Write a program that parse the command line and print list of arguments 48
  25. Summarise ❖ Array, string, and structured data types ❖ Understand loop structures: while, for, do-while ❖ The role of algorithm in problem solving process ❖ Using loop structures on arrays, strings ❖ Implements algorithms with loops 50