Skills Check 2 Flashcards

1
Q

What are the advantages and disadvantages of programming in C++? Provide examples.

A

Advantages:
Fast and Efficient – C++ is a compiled language, making it faster than interpreted languages.
Allows for code reusability
Example: Game engines like Unreal Engine use C++ for speed.

Object-Oriented Programming (OOP) – Supports classes, inheritance, and polymorphism for better code organization.

Disadvantages:
Complexity – Requires a strong understanding of memory management and OOP.

Long Compilation Time – Large projects take longer to compile compared to interpreted languages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a declaration and a definition in C++? Provide examples.

A

Definition:
A definition allocates memory (for variables) or provides the actual implementation (for functions).

Example:
int x = 10;

int add(int a, int b) {
return a + b; // Defines the function body
}

Declaration:
A declaration tells the compiler that a name (variable, function, class, etc.) exists, but it does not allocate memory (for variables) or provide an implementation (for functions).

Example:
extern int x;
int add(int, int);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a program in C++ that will: (1) ask the user to provide two numbers, (2) perform the operation given by your action code a on the two numbers, and (3) display the result of the operation on the screen. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;
#include <cmath></cmath>

void task1() {
int number1;
int number2;
cout &laquo_space;“Enter a number: “;
cin&raquo_space; number1;
cout &laquo_space;“Enter another number: “;
cin&raquo_space; number2;

double result = pow(number1, number2);
cout << number1 << " raised to the power of " << number2 << " is " << result << endl; }

int main() {
task1();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the uses and benefits of type-casting in C++? Provide examples.

A

Type-casting in C++ is a way to convert a variable from one data type to another. It can help improve code safety, clarity, and maintainability by making the programmer’s intent explicit.
examples:
1. Converting int to double with static_cast:
2.Removing constness with const_cast:
3. int to a float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a program in C++ that will use the appropriate library function to calculate, on a number provided by the user, the operation given by your action code b(square root ). The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;
#include <cmath></cmath>

void task2() {
int number1;
int result;
cout &laquo_space;“Enter a number: “;
cin&raquo_space; number1;
result = sqrt(number1);
cout &laquo_space;result &laquo_space;endl;

}
int main() {
task2();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a program in C++ that will: (1) ask the user to provide two numbers, (2) make sure that the first number is smaller than the second number, and display an error message if that is not the case, after which the program should exit with an error code of -1, (3) go through all the numbers in the range between the first and the second number, and (4) display on the screen only those numbers in that range that meet the criterion given by your action code c(divisible by 3 ). The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;
#include <cmath></cmath>

void task3() {
int number1, number2;
cout &laquo_space;“Enter two numbers: “;
cin&raquo_space; number1&raquo_space; number2;

if (number1 > number2) {
    cout << "Error: The first number must be smaller than the second." << endl;
    exit(-1);  // Exit with error code -1
}
for (int i = number1; i <= number2; i++) {
    if (i % 3 == 0) {
        cout << i << endl;
    }
} }

int main() {
task3();
return 0;
}

// TIP See CLion help at <a
// href=”https://www.jetbrains.com/help/clion/”>jetbrains.com/help/clion/</a>.
// Also, you can try interactive lessons for CLion by selecting
// ‘Help | Learn IDE Features’ from the main menu.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a program in C++ that will continue its execution until the user decides to stop it. In each iteration of the program execution, the program should flip a virtual coin, and tell the user whether the outcome of the flip is “Heads” or “Tails.” There should be at least one virtual flip. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;
#include <cstdlib>
#include <ctime></ctime></cstdlib>

void task7() {
srand(time(0)); // Seed the random number generator once
char choice;

do {
    int flip = rand() % 2;  // Generate 0 or 1
    if (flip == 0) {
        cout << "Heads" << endl;
    } else {
        cout << "Tails" << endl;
    }

    // Ask the user if they want to flip again
    cout << "Do you want to flip again? (y/n): ";
    cin >> choice;

} while (choice == 'y' || choice == 'Y');

cout << "Thank you for playing!" << endl; }

int main() {
task7();
return 0;
}

// TIP See CLion help at <a
// href=”https://www.jetbrains.com/help/clion/”>jetbrains.com/help/clion/</a>.
// Also, you can try interactive lessons for CLion by selecting
// ‘Help | Learn IDE Features’ from the main menu.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a program in C++ that will continue its execution until the user decides to stop it. In each iteration of the program execution, the program should throw a virtual, six-sided die, and tell the user the outcome of the throw (i.e., the number of dots). There should be at least one throw. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

#include <cstdlib>
#include <ctime></ctime></cstdlib>

using namespace std;

void rollDie() {
srand(time(0)); // Seed the random number generator once
char choice;

do {
    int roll = (rand() % 6) + 1;  // Generate a number between 1 and 6
    cout << "You rolled a " << roll << "!" << endl;

    // Ask the user if they want to roll again
    cout << "Do you want to roll again? (y/n): ";
    cin >> choice;

} while (choice == 'y' || choice == 'Y');

cout << "Thanks for playing!" << endl; }

int main() {
rollDie();
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the uses and benefits of structs in C++? Provide examples.

A

A struct in C++ is a user-defined data type that groups related variables together. It is useful for organizing data and improving code readability.

Benefits:
Groups Related Data – Helps keep related variables together in one unit.
Improves Readability – Makes code easier to understand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the uses and benefits of enumerations in C++? Provide examples.

A

Uses:
Improves Code Readability – Assigns meaningful names to integer constants.
Prevents Invalid Values – Limits a variable to specific predefined values.
Enhances Maintainability – Easier to update and manage compared to raw integer constants.

Key Benefits:
Increases clarity – Instead of using numbers (e.g., 2 for Wednesday), we use Wednesday.
Reduces errors – Prevents assigning invalid values.
Easier debugging – Meaningful names make debugging easier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a program in C++ that will include a separate function that will accomplish the task given by your action code d(print on the screen a line “divider” consisting of 33 dashes ). The function should not return a value, but the program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Function to print a divider line using a loop
void printDivider() {
for (int i = 0; i < 33; i++) {
cout &laquo_space;”-“;
}
cout &laquo_space;endl; // Move to the next line after printing 33 dashes
}

int main() {
// Call the function to print the divider
printDivider();

return 0; // Successful execution }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a program in C++ that will include a separate function that will perform the calculation given by your action code e ( multiplication )on a number provided by the user. The function should return the result of the calculation, which then should be displayed on the screen. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Function to perform multiplication
int multiplyByFactor(int num, int factor) {
return num * factor;
}

int main() {
int number, factor;

// Get input from the user
cout << "Enter a number: ";
cin >> number;
cout << "Enter a multiplication factor: ";
cin >> factor;

// Call the function and store the result
int result = multiplyByFactor(number, factor);

// Display the result
cout << "Result: " << result << endl;

return 0; // Successful execution }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a program in C++ that will include a separate function that will take as a parameter a number provided by the user, perform on that number the manipulation given by your action code f ( increment by 2 ). The function should not return the result of the calculation, but the original variable should reflect the result of that manipulation. The new value of the variable should be displayed on the screen. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Function to increment the number by 2 (pass-by-reference)
void incrementByTwo(int &num) {
num += 2; // Increment the value by 2
}

int main() {
int number;

// Get input from the user
cout << "Enter a number: ";
cin >> number;

// Call the function to modify the number
incrementByTwo(number);

// Display the updated value
cout << "New value after incrementing by 2: " << number << endl;

return 0; // Successful execution }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the uses and benefits of overloading functions? Provide examples.

A

It allows users to have more than one function having the same name but different properties. Overloaded functions enable users to supply different semantics for a function, depending on the signature of functions.

Advantages of function overloading are as follows:

The main advantage of function overloading is that it improves code readability and allows code reusability.
The use of function overloading is to save memory space, consistency, and readability.

The main disadvantage is that it requires the compiler to perform name mangling on the function name to include information about the argument types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a program in C++ that will include two separate functions, both of which will perform, on two numbers provided by the user, the calculation given by your action code g( divide the first number by the second number ), but one of them will work for whole numbers, and the other one for decimals. The function should return the result of the calculation, using the appropriate data type, which should also be displayed on the screen. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Function to divide two integers (whole numbers)
int divideIntegers(int a, int b) {
if (b == 0) {
cout &laquo_space;“Error: Division by zero is not allowed.” &laquo_space;endl;
exit(-1); // Exit with error code
}
return a / b; // Integer division
}

// Function to divide two floating-point numbers (decimals)
double divideDecimals(double a, double b) {
if (b == 0.0) {
cout &laquo_space;“Error: Division by zero is not allowed.” &laquo_space;endl;
exit(-1); // Exit with error code
}
return a / b; // Floating-point division
}

int main() {
int intNum1, intNum2;
double doubleNum1, doubleNum2;

// Get integer input from the user
cout << "Enter two whole numbers (integers) for division: ";
cin >> intNum1 >> intNum2;

// Call the integer division function and display the result
cout << "Integer division result: " << divideIntegers(intNum1, intNum2) << endl;

// Get floating-point input from the user
cout << "Enter two decimal numbers for division: ";
cin >> doubleNum1 >> doubleNum2;

// Call the floating-point division function and display the result
cout << "Decimal division result: " << divideDecimals(doubleNum1, doubleNum2) << endl;

return 0; // Successful execution }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a program in C++ that will include a recursive function that will calculate the factorial of any positive number provided by the user. The function should return the result of the calculation, which should also be displayed on the screen. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Recursive function to calculate factorial
long long factorial(int num) {
if (num == 0 || num == 1) // Base case
return 1;
return num * factorial(num - 1); // Recursive case
}

int main() {
int number;

// Get input from the user
cout << "Enter a positive number to calculate its factorial: ";
cin >> number;

// Validate input
if (number < 0) {
    cout << "Error: Factorial is not defined for negative numbers." << endl;
    return -1;  // Exit with error code
}

// Call the factorial function and display the result
cout << "Factorial of " << number << " is: " << factorial(number) << endl;

return 0; // Successful execution }
16
Q

Write a program in C++ that will include an in-line function that will perform the conversion given by your action code h( minutes to hours ) on a number provided by the user. The function should return the result of the calculation, which should also be displayed on the screen. The program should return 0 upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Inline function to convert minutes to hours
inline double minutesToHours(double minutes) {
return minutes / 60.0; // 1 hour = 60 minutes
}

int main() {
double minutes;

// Get input from the user
cout << "Enter time in minutes: ";
cin >> minutes;

// Call the inline function and display the result
cout << minutes << " minutes is equal to " << minutesToHours(minutes) << " hours." << endl;

return 0; // Successful execution }
17
Q

What are the uses and benefits of default arguments in functions? Provide examples.

A

Default arguments in C++ allow functions to have predefined values for parameters, which get used if the caller does not provide them.

Benefits:
Reduces Function Overloading – No need to create multiple versions of a function.
Improves Code Simplicity – Fewer parameters need to be specified during function calls.

18
Q

Write a program in C++ that will include two separate functions, both of which will be capable of modifying the same variable, the name of which is provided by your action code I(total_value ). The variable should be initiated to a number provided by the user at the beginning of the execution of the program. One of the functions will allow to increase the variable by a number specified by the user, while the other function will allow for the value of the variable to be decreased by a number provided by the user. Test both your functions, and display the final value of the variable at the end of the execution of the program. The program should also return the final value of the variable upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Function to increase total_value
void increaseValue(int &total_value, int amount) {
total_value += amount;
}

// Function to decrease total_value
void decreaseValue(int &total_value, int amount) {
total_value -= amount;
}

int main() {
int total_value, increase_amount, decrease_amount;

// Get initial value from the user
cout << "Enter the initial total value: ";
cin >> total_value;

// Get the increase amount
cout << "Enter the amount to increase: ";
cin >> increase_amount;
increaseValue(total_value, increase_amount); // Modify total_value

// Get the decrease amount
cout << "Enter the amount to decrease: ";
cin >> decrease_amount;
decreaseValue(total_value, decrease_amount); // Modify total_value

// Display the final value
cout << "Final total value: " << total_value << endl;

return total_value; // Return the final total_value upon successful execution }
19
Q

Write a program in C++ that will include a “setter” function for a global variable, the name of which is provided by your action code j(overall_score ). Test the function, and display the final value of the variable at the end of the execution of the program. The program should also return the final value of the variable upon a successful execution.

A

include <iostream></iostream>

using namespace std;

// Global variable
int overall_score = 0;

// Setter function to update overall_score
void setOverallScore(int new_score) {
overall_score = new_score;
}

int main() {
int new_value;

// Get new score from the user
cout << "Enter the new overall score: ";
cin >> new_value;

// Call setter function to update overall_score
setOverallScore(new_value);

// Display the final value of overall_score
cout << "Final overall score: " << overall_score << endl;

return overall_score; // Return the final overall_score upon successful execution }