0% found this document useful (0 votes)
15 views7 pages

H W1and3

The document contains two homework assignments for a student management system and a calculator program in C++. The student management system allows adding, displaying, searching, and sorting students by GPA, while the calculator program performs basic arithmetic operations using user input. Both programs utilize C++ features such as classes, functions, and switch statements.

Uploaded by

clasherscraft584
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)
15 views7 pages

H W1and3

The document contains two homework assignments for a student management system and a calculator program in C++. The student management system allows adding, displaying, searching, and sorting students by GPA, while the calculator program performs basic arithmetic operations using user input. Both programs utilize C++ features such as classes, functions, and switch statements.

Uploaded by

clasherscraft584
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/ 7

‫محمد عبد الكاظم سلمان‬

‫المرحلة االولى‬

‫هندسة الشبكات واالمن‬ ‫صباحي‬


‫( السيبراني‬B)

Homework 1: Student Management System

Focus: Classes, basic menu-driven program

Description:

Create a console application that manages student records using a


student class.

Each student has: ID, Name, Age, and Average.

Requirements:

• Add new student

• Display all students

Bonus:

• Search by ID

• Sort by GPA

Solution

#include <iostream>
#include <string>
using namespace std;

class Student {
public:
int id;
string name;
int age;
float average;

void input() {
cout << "Enter ID: ";
cin >> id;
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
cout << "Enter GPA: ";
cin >> average;
}
};

int main() {
const int MAX = 100;
Student students[MAX];
int count = 0;
int choice;

do {
cout << "\n--- Student Management System ---\n";
cout << "1. Add New Student\n";
cout << "2. Display All Students\n";
cout << "3. Search by ID\n";
cout << "4. Sort by GPA\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
if (count >= MAX) {
cout << "Student list is full.\n";
} else {
students[count].input();
count++;
}
}

else if (choice == 2) {
if (count == 0) {
cout << "No students to display.\n";
} else {
for (int i = 0; i < count; ++i) {
cout << "ID: " << students[i].id
<< ", Name: " << students[i].name
<< ", Age: " << students[i].age
<< ", GPA: " << students[i].average << endl;
}
}
}

else if (choice == 3) {
int searchId;
cout << "Enter ID to search: ";
cin >> searchId;
bool found = false;
for (int i = 0; i < count; ++i) {
if (students[i].id == searchId) {
cout << "ID: " << students[i].id
<< ", Name: " << students[i].name
<< ", Age: " << students[i].age
<< ", GPA: " << students[i].average << endl;
found = true;
break;
}
}
if (!found) {
cout << "Student with ID " << searchId << " not found.\n";
}
}

else if (choice == 4) {
for (int i = 0; i < count - 1; ++i) {
for (int j = i + 1; j < count; ++j) {
if (students[i].average < students[j].average) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
cout << "Students sorted by GPA (descending).\n";
}

else if (choice == 0) {
cout << "Exiting program.\n";
}

else {
cout << "Invalid choice. Try again.\n";
}

} while (choice != 0);

return 0;
}

Homework 3: Calculator Program


Focus: Functions, conditionals, switch-case

Description:
Write a C++ program that acts as a basic calculator to perform
arithmetic operations.

Requirements:

 Ask the user to input two numbers

 Ask the user to choose an operation: +, -, *, /

 Use a switch statement to perform the operation

 Display the result

Solution

#include <iostream>

using namespace std;

int main() {

double num1, num2, result;

char op;

cout << "Enter first number: ";

cin >> num1;

cout << "Enter second number: ";

cin >> num2;

cout << "Enter operation (+, -, *, /): ";

cin >> op;


switch (op) {

case '+':

result = num1 + num2;

cout << "Result: " << result << endl;

break;

case '-':

result = num1 - num2;

cout << "Result: " << result << endl;

break;

case '*':

result = num1 * num2;

cout << "Result: " << result << endl;

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

cout << "Result: " << result << endl;

} else {

cout << "Error: Division by zero is not allowed." << endl;

break;

default:

cout << "Invalid operation!" << endl;


}

return 0;

You might also like