0% found this document useful (0 votes)
8 views5 pages

c++ study note

The document provides answers to a series of questions related to C++ programming, including online IDEs, file extensions, and debugging. It also presents five programming tasks with complete C++ solutions: a scholarship eligibility program, a library book inventory management system, a retail store inventory tracker, a student grades calculation program, and a compound interest calculator. Each program is described in terms of its functionality and user interaction.

Uploaded by

Chrisfred Dambo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

c++ study note

The document provides answers to a series of questions related to C++ programming, including online IDEs, file extensions, and debugging. It also presents five programming tasks with complete C++ solutions: a scholarship eligibility program, a library book inventory management system, a retail store inventory tracker, a student grades calculation program, and a compound interest calculator. Each program is described in terms of its functionality and user interaction.

Uploaded by

Chrisfred Dambo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Here are the correct answers for Section A (continued):

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.)

Let me know if you need any clarifications!

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;
}

2. Library Book Inventory Management

This program allows a librarian to add, remove, and display books in a library.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

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);

for (auto it = books.begin(); it != books.end(); ++it) {


if (*it == book) {
books.erase(it);
cout << "Book removed successfully!\n";
return;
}
}
cout << "Book not found!\n";
}

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;
}

3. Retail Store Inventory Tracker

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];

// Input sales data for each day


cout << "Enter the number of items sold for each day of the week:\n";
for (int i = 0; i < 7; i++) {
cout << "Day " << i + 1 << ": ";
cin >> sales[i];
}

// Display total sales per day


cout << "\nSales Summary:\n";
for (int i = 0; i < 7; i++) {
cout << "Day " << i + 1 << ": " << sales[i] << " items sold\n";
}

return 0;
}

4. Student Grades Calculation

This program calculates the average score for a student and assigns a grade.

#include <iostream>
using namespace std;

char calculateGrade(float avg) {


if (avg >= 90) return 'A';
else if (avg >= 80) return 'B';
else if (avg >= 70) return 'C';
else if (avg >= 60) return 'D';
else return 'F';
}

int main() {
float scores[5], sum = 0, avg;

// Input test scores


cout << "Enter 5 test scores: ";
for (int i = 0; i < 5; i++) {
cin >> scores[i];
sum += scores[i];
}

// Calculate and display results


avg = sum / 5;
cout << "Average Score: " << avg << endl;
cout << "Grade: " << calculateGrade(avg) << endl;

return 0;
}
5. Compound Interest Calculator

This program calculates compound interest using the formula:

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;

// Convert rate to decimal


rate /= 100;

// Calculate Compound Interest


amount = principal * pow((1 + rate), time);
double interest = amount - principal;

// Display result
cout << "Final Amount: " << amount << endl;
cout << "Compound Interest: " << interest << endl;

return 0;
}

Which Three Will You Choose?

Each of these programs is fully functional, user-friendly, and efficient. Let me know if you
need modifications or explanations!

You might also like