# Comprehensive C++ Exam Review Guide ## 1. **Variables and Constants:** - Variables store data and have specific data types. ```cpp int age = 25; float price = 19.99; char grade = 'A'; ``` - Declare variables using syntax: `datatype variableName;`. - Constants are declared using the `const` keyword. ```cpp const double PI = 3.14159; ``` - **Variable Scope and Lifetime:** - **Scope:** The region where a variable can be accessed. ```cpp void exampleFunction() { int localVar = 10; // Local variable with function scope // code } // localVar goes out of scope here ``` - **Lifetime:** The duration a variable exists in memory. - Local variables exist only during the function's execution. - Global variables exist throughout the program. ## 2. **Input/Output:** - Input using `cin` and output using `cout`. ```cpp cout << "Enter your age: "; cin >> age; ``` - Formatting output with `setw` for width and setting precision with `setprecision`. ```cpp cout << setw(10) << "Name" << setw(5) << "Age" << endl; ``` ## 3. **Files:** - File operations with `ifstream` for reading and `ofstream` for writing. ```cpp ifstream inputFile("input.txt"); ofstream outputFile("output.txt"); ``` - File stream methods like `getline` and `>>` for input from files. ```cpp inputFile >> variable; ``` ## 4. **Loops:** - **While Loop:** ```cpp while (count < 10) { // code count++; } ``` - **Do-While Loop:** ```cpp do { // code } while (condition); ``` - **For Loop:** ```cpp for (int i = 0; i < 5; i++) { // code } ``` - Three parts of loops: Initialization, Condition, Update. ```cpp for (int i = 0; i < 5; i++) { // Initialization: int i = 0; // Condition: i < 5; // Update: i++; ``` ***Break and Continue statements*** - `break` Statement - The `break` statement is used to exit a loop or switch statement. - It is commonly used to terminate a loop prematurely. - `continue` Statement - The `continue` statement is used to skip the current iteration of a loop and continue to the next one. ## 5. **Accumulators, Sentinels, Counters, Increment/Decrement:** - **Accumulators:** Variables for accumulating sums. ```cpp int sum = 0; sum += value; ``` - **Sentinels:** Special values indicating the end of input. ```cpp while (input != -1) { // code } ``` - **Counters:** Variables for counting iterations. ```cpp for (int i = 0; i < 3; i++) { // code ``` ## 6. **Arrays:** - Declare arrays using `datatype arrayName[size];`. ```cpp int numbers[5] = {1, 2, 3, 4, 5}; ``` - Access elements using index (starting from 0). ```cpp int value = numbers[2]; ``` - Operations like sorting and searching. ## 7. **Vectors (Basic):** - Dynamic arrays with `<vector>` header. ```cpp #include <vector> vector<int> scores; ``` - Methods like `push_back`, `pop_back`, and `size()` for dynamic resizing. ```cpp scores.push_back(90); ``` ## 8. **Structs:** - Structures group variables of different types. ```cpp struct Person { string name; int age; }; ``` - Declaration using `struct` keyword. - Access members using dot notation. ```cpp Person student; student.name = "John"; ``` ## 9. **Logical Operators:** - `&&` (AND), `||` (OR), `!` (NOT). ```cpp if (x > 0 && y < 10) { // code } ``` - Building compound conditions for decision-making. ## 10. **Switch, If/Else:** - **Switch:** ```cpp switch (choice) { case 1: // code break; case 2: // code break; default: // code } ``` - **If/Else:** ```cpp if (score >= 90) { // code } else { // code } ``` - Nested conditions and multi-branch decision-making. ## 11. **Static Variables:** - Variables that retain their values between function calls. ```cpp void counter() { static int count = 0; // Static variable retains its value count++; } ``` - Declared using the `static` keyword inside a function. - **Scope and Lifetime of Static Variables:** - **Scope:** Limited to the function where they are declared. ```cpp void```cpp void exampleFunction() { static int staticVar = 10; // Static variable with function scope // code } // staticVar goes out of scope here ``` - **Lifetime:** Extends throughout the program's execution. ```cpp void exampleFunction() { static int staticVar = 10; // Static variable with function scope // staticVar exists throughout the program's execution // code } // staticVar remains in memory even after the function ends ``` ## 12. **Global Variables:** - Variables declared outside of any function, accessible throughout the program. - Global variables have file scope, meaning they are visible to all functions within the same file. - Be cautious about the potential impact on program structure and maintainability. ```cpp int globalVar = 100; // Global variable with file scope void anotherFunction() { // globalVar is accessible here } ``` ## 13. **Functions (Pass by Reference and Pass by Value):** - **Pass by Value:** ```cpp void square(int num) { num = num * num; } ``` - **Pass by Reference:** ```cpp void increment(int &value) { value++; } ``` - Understanding parameter passing mechanisms. - Function overloading and scope of variables. ## 14. **Example of getline with and without Delimiter:** - **Without Delimiter:** ```cpp string sentence; getline(cin, sentence); // Reads the entire line until Enter is pressed ``` - **With Delimiter (e.g., comma):** ```cpp string data; getline(cin, data, ','); // Reads until a comma is encountered ``` ## Practice Questions These are review questions for the exam. You may submit them to replace the assignments noted. If you replacing a lab submit both the pseudocode and code. If you are replacing a design activity (DAXX), submit the pseudocode. If you are placing a coding activity (CAXX), submit the code. Indicate in your submission which you are replacing. *You cannot use a question to replace all 3. Any unlabeled sections or questions submitted are a bonus of the project.* ### Variables and Constants: 1. **Declare and Initialize Variable:** (DA01/CA01) - Declare a variable named `temperature` of type `double` and initialize it with the value 98.6. Print the temperature. 2. **Constant and Array:** - Create a constant named `MAX_SCORE` with a value of 100. Use it to define an array to store test scores. ### Input/Output: 3. **Formatted Output:** (DA02/CA02/Lab 1) - Write a C++ program that prompts the user to enter the names and ages of three individuals. Format and print a table with columns for Name and Age, aligning the output using `setw`. 4. **File Reading and Sum:**(Lab 2) - Read integers from a file named "numbers.txt" and calculate their sum. Print the sum along with the count of numbers read. 5. **Password Validation:** (DA04/CA04) - Implement a `do-while` loop to repeatedly ask the user for a password until they enter the correct one ("Secret123"). Display a success message along with the number of attempts made. 6. **File Writing:** (Lab 3) - Write a program that prompts the user to input their favorite quote and saves it to a file named "quote.txt". ### Loops: (Lab 5) 7. **While Loop:** - Implement a `while` loop to print numbers from 1 to 10. Print each number on a new line. 8. **For Loop and Sum:** - Write a program using a `for` loop to find the sum of the first 50 natural numbers. Display the result. 9. **Sentinel-Controlled Loop:** - Use a sentinel-controlled loop to read numbers from the user until they enter -1. Calculate and print the sum of the entered numbers. ### Accumulators, Counters: 10. **Accumulator and Average:** - Implement an accumulator to calculate the average of 5 exam scores. Print the average with two decimal places. 11. **Counter and Occurrences:** - Use a counter to count the occurrences of the letter 'a' in a given string. Display the count. ### Arrays and Vectors: 12. **Array and Average:** (DA06) - Declare an array named `grades` to store 5 exam scores. Calculate and print the average of the grades. 13. **Array Search:**(CA06) - Implement a function to search for the specific element 42 in an array of integers. Display whether it was found or not. 14. **Vector and Total Cost:** - Create a vector to store the prices of three products. Calculate and print the total cost of the products. 15. **Vector Operations:** (Lab 6) - Implement a program that uses a vector to store a list of student names. Dynamically add a new name and display the updated list. ### Structs: 16. **Struct Array:** (DA07) - Define a structure named `Book` with members `title` and `author`. Create an array of three books and print their details. 17. **Struct User Input:** - Write a program that uses a structure to represent a person. Prompt the user to input their name and age, then display the information. ### Logical Operators: 18. **Logical Condition:** - Create a program that checks if a given number is both positive and even. Display a suitable message based on the check. ### Switch, If/Else: 19. **Menu-Driven Switch:** (Lab 4) - Create a menu-driven program using a switch statement to perform addition, subtraction, multiplication, and division operations. 20. **If/Else Grade Determination:** (DA03/CA03) - Implement an if/else statement to determine the grade (A, B, C, etc.) based on a given exam score. Display the grade. ### Static Variables: 21. **Static Variable Counter:** - Create a function named `callCounter` with a static variable to count how many times it has been called. Print the count. 22. **Static Variable Total Objects:** - Implement a program that uses a static variable to keep track of the total number of objects created. Print the total. ### Global Variables: 23. **Global Variable Sales:** - Declare a global variable named `totalSales` and use it in different functions to calculate and display the overall sales. 24. **Global Constant Max Attempts:** - Declare a global constant named `MAX_ATTEMPTS` for the maximum allowed attempts in a game. Display the value. ### Functions: (DA05/CA05) 25. **Function Square:** - Write a function `square` that takes an integer as a parameter and returns its square. Print the result. 26. **Function Pass by Reference:** - Implement a program that uses pass by reference to update the value of a variable within a function. Prompt the user to enter a number, then call the function to increment the entered value. Display the updated value.