Stuprog

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <iostream>

using namespace std;


const int MAX_STUDENTS = 100;
const int NAME_LENGTH = 50;
const int DEPARTMENT_LENGTH = 50;

struct Student {
char name[NAME_LENGTH];
int age;
char department[DEPARTMENT_LENGTH];
char studentID[NAME_LENGTH];
char address[NAME_LENGTH];
};

// Function to add a new student


void addStudent(Student students[], int& count) {
if (count >= MAX_STUDENTS) {
cout << "Cannot add more students. The list is full.\n";
return;
}
cout << "Enter student name: ";
cin >> students[count].name;
cout << "Enter student age: ";
cin >> students[count].age;
cout << "Enter student department: ";
cin >> students[count].department;
cout << "Enter student ID: ";
cin >> students[count].studentID;
cout << "Enter student address: ";
cin >> students[count].address;
count++;
}

// Function to compare two strings character by character


bool compareStrings(const char str1[], const char str2[]) {
int i = 0;
while (str1[i] != '\0' || str2[i] != '\0') {
if (str1[i] != str2[i]) {
return false;
}
i++;
}
return true;
}

// Function to update information for an existing student


void updateStudent(Student students[], int count) {
char studentID[NAME_LENGTH];
cout << "Enter student ID to update: ";
cin >> studentID;
for (int i = 0; i < count; i++) {
if (compareStrings(students[i].studentID, studentID)) {
cout << "Enter new name for student ID " << students[i].studentID << ":
";
cin >> students[i].name;
cout << "Enter new age for " << students[i].name << ": ";
cin >> students[i].age;
cout << "Enter new department for " << students[i].name << ": ";
cin >> students[i].department;
cout << "Enter new address for " << students[i].name << ": ";
cin >> students[i].address;
cout << "Student information updated.\n";
return;
}
}
cout << "Student not found.\n";
}

// Function to sort students by age using bubble sort


void sortStudentsByAge(Student students[], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (students[j].age > students[j + 1].age) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
cout << "Students sorted by age.\n";
}

// Function to count the number of students in each department


void countStudentsByDepartment(Student students[], int count) {
char departments[MAX_STUDENTS][DEPARTMENT_LENGTH];
int departmentCounts[MAX_STUDENTS] = { 0 };
int departmentIndex = 0;

for (int i = 0; i < count; i++) {


bool found = false;
for (int j = 0; j < departmentIndex; j++) {
if (compareStrings(students[i].department, departments[j])) {
departmentCounts[j]++;
found = true;
break;
}
}
if (!found) {
for (int k = 0; k < DEPARTMENT_LENGTH; k++) {
departments[departmentIndex][k] = students[i].department[k];
if (students[i].department[k] == '\0') break;
}
departmentCounts[departmentIndex]++;
departmentIndex++;
}
}

cout << "Number of students in each department:\n";


for (int i = 0; i < departmentIndex; i++) {
cout << departments[i] << ": " << departmentCounts[i] << " students\n";
}
}

// Function to display all students


void displayStudents(Student students[], int count) {
cout << "Students list:\n";
for (int i = 0; i < count; i++) {
cout << i + 1 << ". " << students[i].name << " - Age: " << students[i].age
<< ", Department: " << students[i].department << ", Student ID: " <<
students[i].studentID
<< ", Address: " << students[i].address << "\n";
}
}

int main() {
Student students[MAX_STUDENTS];
int count = 0;
int choice;

do {
// Display menu options for the user
cout << "Student Management System\n";
cout << "1. Add new student\n";
cout << "2. Update student information\n";
cout << "3. Arrange students by age\n";
cout << "4. Count students by department\n";
cout << "5. Display all students\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

// Handle user choice and call the appropriate function


switch (choice) {
case 1:
addStudent(students, count);
break;
case 2:
updateStudent(students, count);
break;
case 3:
sortStudentsByAge(students, count);
break;
case 4:
countStudentsByDepartment(students, count);
break;
case 5:
displayStudents(students, count);
break;
case 0:
cout << "Exiting the program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 0);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy