📖 Scenario: Imagine you are explaining to a friend why C++ is a popular programming language in many areas like games, software, and systems.
🎯 Goal: You will create a simple C++ program that lists some reasons why C++ is widely used.
📋 What You'll Learn
Create a list of reasons why C++ is widely used
Add a variable to count the number of reasons
Use a loop to display each reason
Print the total number of reasons at the end
💡 Why This Matters
🌍 Real World
C++ is used in many real-world applications like video games, operating systems, and high-performance software because it is fast and gives programmers control over how the computer works.
💼 Career
Knowing why C++ is popular helps beginners understand its importance and motivates learning it for careers in software development, game programming, and systems engineering.
Progress0 / 4 steps
1
Create a list of reasons
Create a std::vector<std::string> called reasons with these exact entries: "Fast performance", "Control over system resources", "Widely used in game development", "Supports object-oriented programming", "Large community and libraries".
C++
Hint
Use std::vector<std::string> and initialize it with the list of reasons inside curly braces.
2
Add a counter for the number of reasons
Add an int variable called count and set it to the size of the reasons vector using reasons.size().
C++
Hint
Use int count = reasons.size(); to get the number of reasons.
3
Display each reason using a loop
Use a for loop with int i = 0 to i < count to print each reason from the reasons vector using std::cout. Print each reason on its own line.
C++
Hint
Use a for loop from 0 to count - 1 and print each reasons[i] with std::cout.
4
Print the total number of reasons
After the loop, print the text Total reasons: followed by the value of count using std::cout.
C++
Hint
Use std::cout << "Total reasons: " << count << std::endl; to print the total count.
Practice
(1/5)
1. Why is C++ widely used in software development?
easy
A. Because it is only used for web design
B. Because it does not support object-oriented programming
C. Because it is fast and gives control over memory
D. Because it is slower than most languages
Solution
Step 1: Understand C++ performance
C++ is known for its speed and efficiency in running programs.
Step 2: Recognize memory control
C++ allows programmers to manage memory directly, which is important for performance.
Final Answer:
Because it is fast and gives control over memory -> Option C
Quick Check:
Speed and memory control = B [OK]
Hint: Remember: speed and memory control make C++ popular [OK]
Common Mistakes:
Thinking C++ is only for web design
Believing C++ is slow
Ignoring memory management features
2. Which of the following is the correct way to include the standard input-output library in C++?
easy
A. #include iostream
B. #include <iostream>
C. import iostream;
D. #include <stdio>
Solution
Step 1: Recall C++ syntax for including libraries
C++ uses #include with angle brackets for standard libraries.
Step 2: Identify correct syntax
#include <iostream> is the correct way to include the input-output stream library.
Final Answer:
#include <iostream> -> Option B
Quick Check:
#include <iostream> = D [OK]
Hint: Use #include with angle brackets for standard C++ libraries [OK]
Common Mistakes:
Using #include <stdio> instead of <iostream>
Writing import instead of #include
Missing angle brackets around library name
3. What will be the output of this C++ code?
int main() {
int a = 5;
int b = 10;
int c = a + b;
std::cout << c << std::endl;
return 0;
}
medium
A. 15
B. 510
C. Syntax Error
D. 5
Solution
Step 1: Analyze variable values and operation
Variables a and b are 5 and 10, respectively. Adding them gives 15.
Step 2: Understand output statement
std::cout prints the value of c, which is 15, followed by a newline.
Final Answer:
15 -> Option A
Quick Check:
5 + 10 = 15 [OK]
Hint: Add variables before printing to find output [OK]