c++ study note
c++ study note
9. Where can you find an online IDE for writing, compiling, and executing C++
applications?
a) http://www.tutorialspoint.com/compile_cpp_online.php (This website provides an
online C++ compiler.)
10. Which file extension should be used when saving C++ files?
c) .cpp (C++ source files typically use the .cpp extension.)
11. What is the purpose of std::endl in C++?
b) Ends the line in the console (std::endl is used to insert a newline in the output
stream.)
12. What does the command g++ -o hello Hello.cpp do in Linux?
b) Creates an executable named "hello" (-o specifies the output file name.)
13. How can you run a C++ program in Microsoft Visual Studio on Windows?
a) Press Ctrl+F5 (This runs the program without debugging in Visual Studio.)
14. What is the purpose of unchecking the "Precompiled Header" option in Microsoft
Visual Studio?
d) Speeds up the compilation process (Precompiled headers improve compilation
speed, but unchecking disables them.)
15. Which of the following is a popular free C++ compiler for Linux?
b) g++ (GCC’s C++ compiler is widely used on Linux.)
16. What is the purpose of #include in C++?
b) To import external libraries (#include is used to include standard or user-defined
libraries.)
17. How is std::cout pronounced?
b) Standard-c-out (std::cout refers to "standard character output.")
18. What does the term "debugging" refer to in software development?
c) Analyzing and fixing errors in code (Debugging is the process of finding and
correcting errors.)
19. Why is it recommended to save C++ files with the .cpp extension?
c) It is a convention for C++ files (.cpp is the standard file extension for C++ source
code.)
20. What is the purpose of the return 0; statement in a C++ program?
b) Indicates a successful execution (return 0; signals that the program completed
successfully.)
Here are C++ solutions for each of the five programming tasks. Choose three based on your
preference.
1. Scholarship Eligibility Program
This program evaluates a student's scholarship eligibility based on their GPA, SAT score, and
extracurricular activities.
#include <iostream>
using namespace std;
int main() {
double gpa;
int satScore;
char extracurricular;
// User input
cout << "Enter Student's GPA: ";
cin >> gpa;
cout << "Enter Student's SAT Score: ";
cin >> satScore;
cout << "Has the student participated in extracurricular activities?
(Y/N): ";
cin >> extracurricular;
// Scholarship Criteria
if ((gpa >= 3.5 && satScore >= 1200) || (gpa >= 3.2 && satScore >= 1100 &&
extracurricular == 'Y')) {
cout << "Congratulations! The student is eligible for the
scholarship.\n";
} else {
cout << "Sorry, the student does not meet the scholarship criteria.\
n";
}
return 0;
}
This program allows a librarian to add, remove, and display books in a library.
#include <iostream>
#include <vector>
#include <string>
vector<string> books;
void addBook() {
string book;
cout << "Enter book title to add: ";
cin.ignore();
getline(cin, book);
books.push_back(book);
cout << "Book added successfully!\n";
}
void removeBook() {
string book;
cout << "Enter book title to remove: ";
cin.ignore();
getline(cin, book);
void displayBooks() {
cout << "Available books:\n";
for (const auto &book : books) {
cout << "- " << book << endl;
}
}
int main() {
int choice;
do {
cout << "\nLibrary Inventory Management\n";
cout << "1. Add Book\n2. Remove Book\n3. Display Books\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: addBook(); break;
case 2: removeBook(); break;
case 3: displayBooks(); break;
case 4: cout << "Exiting...\n"; break;
default: cout << "Invalid choice! Try again.\n";
}
} while (choice != 4);
return 0;
}
This program allows a store manager to track items sold each day using an array.
#include <iostream>
using namespace std;
int main() {
int sales[7];
return 0;
}
This program calculates the average score for a student and assigns a grade.
#include <iostream>
using namespace std;
int main() {
float scores[5], sum = 0, avg;
return 0;
}
5. Compound Interest Calculator
A = P (1 + r/n)^{nt}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double principal, rate, time, amount;
// Input values
cout << "Enter Principal Amount: ";
cin >> principal;
cout << "Enter Annual Interest Rate (in %): ";
cin >> rate;
cout << "Enter Time Period (in years): ";
cin >> time;
// Display result
cout << "Final Amount: " << amount << endl;
cout << "Compound Interest: " << interest << endl;
return 0;
}
Each of these programs is fully functional, user-friendly, and efficient. Let me know if you
need modifications or explanations!