Array Tasks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Name: Eesar Hussain Class: Data Structure

Roll No: 498 Section:F

Task 1:

Scenario:

You are developing a student management system for a school using C++. The system should allow
teachers to perform various array operations on student records to efficiently manage their academic
information. Here's the scenario you need to base your question on:

Scenario Description:

You are tasked with creating a C++ program to help teachers manage student records for a school. The
program should provide functionalities for teachers to perform array operations on the student data,
including traversing the list of students, inserting new student records, deleting existing student records,
searching for specific students, and updating student information.

Question:

Develop a C++ program for managing student records in a school. Implement the following array
operations:

Traverse Operation:

Write a function to display the details of all the students currently enrolled in the school.

Insertion:

Implement a function that allows teachers to add a new student record to the system. The function
should prompt the teacher to enter the student's details (e.g., name, roll number, grade), and then add
it to the student records.

Deletion:

Create a function to remove a student record from the system based on the given roll number or name.
The function should prompt the teacher to enter the roll number or name of the student they want to
delete and then remove the corresponding record from the system.

Searching:

Write a function to search for a student in the records based on either the student's roll number or
name. The function should prompt the teacher to enter the roll number or name of the student they
want to search for and then display the student details if found.

Update:

Implement a function to update the information of a student in the records. The function should prompt
the teacher to enter the roll number or name of the student they want to update, and then allow them
to modify any information related to that student (e.g., name, grade, contact information).
Ensure that your program provides a user-friendly interface with clear instructions for each operation.
Test your program with sample inputs to demonstrate its functionality.

#include <iostream>

#include <string>

using namespace std;

// Structure to represent a student

struct Student {

string name;

int rollNumber;

char grade;

};

const int MAX_STUDENTS = 100;

Student students[MAX_STUDENTS];

int numStudents = 0;

// Function to display all student records

void traverse() {

cout << "List of students:" << endl;

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

cout << "Name: " << students[i].name << ", Roll Number: " << students[i].rollNumber << ", Grade: "
<< students[i].grade << endl;

// Function to add a new student record

void insert() {

if (numStudents < MAX_STUDENTS) {


cout << "Enter student details:" << endl;

cout << "Name: ";

cin >> students[numStudents].name;

cout << "Roll Number: ";

cin >> students[numStudents].rollNumber;

cout << "Grade: ";

cin >> students[numStudents].grade;

numStudents++;

cout << "Student record added successfully!" << endl;

} else {

cout << "Maximum number of students reached. Cannot add more students." << endl;

// Function to delete a student record

void deletion() {

int rollNumber;

cout << "Enter the roll number of the student you want to delete: ";

cin >> rollNumber;

bool found = false;

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

if (students[i].rollNumber == rollNumber) {

found = true;

for (int j = i; j < numStudents - 1; ++j) {

students[j] = students[j + 1];

numStudents--;

cout << "Student record deleted successfully!" << endl;

break;
}

if (!found) {

cout << "Student with roll number " << rollNumber << " not found." << endl;

// Function to search for a student record

void searching() {

int rollNumber;

cout << "Enter the roll number of the student you want to search for: ";

cin >> rollNumber;

bool found = false;

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

if (students[i].rollNumber == rollNumber) {

found = true;

cout << "Student found:" << endl;

cout << "Name: " << students[i].name << ", Roll Number: " << students[i].rollNumber << ", Grade:
" << students[i].grade << endl;

break;

if (!found) {

cout << "Student with roll number " << rollNumber << " not found." << endl;

// Function to update a student record

void update() {
int rollNumber;

cout << "Enter the roll number of the student you want to update: ";

cin >> rollNumber;

bool found = false;

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

if (students[i].rollNumber == rollNumber) {

found = true;

cout << "Enter new details for the student:" << endl;

cout << "Name: ";

cin >> students[i].name;

cout << "Grade: ";

cin >> students[i].grade;

cout << "Student record updated successfully!" << endl;

break;

if (!found) {

cout << "Student with roll number " << rollNumber << " not found." << endl;

int main() {

int choice;

do {

cout << "\nStudent Management System Menu:\n";

cout << "1. Traverse (Display all students)\n";

cout << "2. Insert (Add a new student)\n";

cout << "3. Delete (Remove a student)\n";

cout << "4. Search (Find a student)\n";


cout << "5. Update (Modify student details)\n";

cout << "6. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

traverse();

break;

case 2:

insert();

break;

case 3:

deletion();

break;

case 4:

searching();

break;

case 5:

update();

break;

case 6:

cout << "Exiting program. Goodbye!" << endl;

break;

default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 6);


return 0;

Output

Lab Task 2
You are tasked with developing a program for managing a library's book inventory using C++. The
program should provide functionality for various array operations to handle the book records efficiently.
Here's the scenario you need to base your question on:

Scenario Description:

You are a software developer working for a library. The library needs a software tool to manage its book
inventory efficiently. Your task is to design a C++ program that allows librarians to perform various array
operations on the book inventory. The program should include functionalities for traversing the book
records, inserting new books, deleting existing books, searching for specific books, and updating book
information.

Question:

Develop a C++ program to manage the book inventory for the library. Implement the following array
operations:

Traverse Operation:

Write a function to display all the books currently in the inventory.

Insertion:

Implement a function that allows the librarian to add a new book to the inventory. The function should
prompt the user to enter the details of the new book (e.g., title, author, ISBN), and then add it to the
inventory.

Deletion:

Create a function to remove a book from the inventory based on the given book title or ISBN. The
function should prompt the user to enter the title or ISBN of the book they want to delete and then
remove it from the inventory.

Searching:

Write a function to search for a book in the inventory based on either the book's title or ISBN. The
function should prompt the user to enter the title or ISBN of the book they want to search for and then
display the book details if found.

Update:

Implement a function to update the information of a book in the inventory. The function should prompt
the user to enter the title or ISBN of the book they want to update, and then allow them to modify any
information related to that book (e.g., title, author, ISBN)

Ensure that your program provides a user-friendly interface with clear instructions for each operation.
Test your program with sample inputs to demonstrate its functionality.

Solution:
#include <iostream>

#include <string>

using namespace std;

struct Book {

string title;

string author;

string ISBN;

};

const int MAX_BOOKS = 100;

Book books[MAX_BOOKS];

int numBooks = 0;

void traverse() {

cout << "List of books:" << endl;

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

cout << "Title: " << books[i].title << ", Author: " << books[i].author << ", ISBN: " << books[i].ISBN <<
endl;

void insert() {

if (numBooks < MAX_BOOKS) {

cout << "Enter book details:" << endl;

cout << "Title: ";

cin.ignore();

getline(cin, books[numBooks].title);

cout << "Author: ";


getline(cin, books[numBooks].author);

cout << "ISBN: ";

cin >> books[numBooks].ISBN;

numBooks++;

cout << "Book added successfully!" << endl;

} else {

cout << "Maximum number of books reached. Cannot add more books." << endl;

void deletion() {

string searchTerm;

cout << "Enter the title or ISBN of the book you want to delete: ";

cin.ignore();

getline(cin, searchTerm);

bool found = false;

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

if (books[i].title == searchTerm || books[i].ISBN == searchTerm) {

found = true;

for (int j = i; j < numBooks - 1; ++j) {

books[j] = books[j + 1];

numBooks--;

cout << "Book deleted successfully!" << endl;

break;

if (!found) {

cout << "Book not found." << endl;


}

void searching() {

string searchTerm;

cout << "Enter the title or ISBN of the book you want to search for: ";

cin.ignore();

getline(cin, searchTerm);

bool found = false;

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

if (books[i].title == searchTerm || books[i].ISBN == searchTerm) {

found = true;

cout << "Book found:" << endl;

cout << "Title: " << books[i].title << ", Author: " << books[i].author << ", ISBN: " << books[i].ISBN
<< endl;

break;

if (!found) {

cout << "Book not found." << endl;

void update() {

string searchTerm;

cout << "Enter the title or ISBN of the book you want to update: ";

cin.ignore();

getline(cin, searchTerm);

bool found = false;


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

if (books[i].title == searchTerm || books[i].ISBN == searchTerm) {

found = true;

cout << "Enter new details for the book:" << endl;

cout << "Title: ";

getline(cin, books[i].title);

cout << "Author: ";

getline(cin, books[i].author);

cout << "ISBN: ";

cin >> books[i].ISBN;

cout << "Book information updated successfully!" << endl;

break;

if (!found) {

cout << "Book not found." << endl;

int main() {

int choice;

do {

cout << "\nLibrary Inventory Management System Menu:\n";

cout << "1. Traverse (Display all books)\n";

cout << "2. Insert (Add a new book)\n";

cout << "3. Delete (Remove a book)\n";

cout << "4. Search (Find a book)\n";

cout << "5. Update (Modify book details)\n";

cout << "6. Exit\n";


cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: traverse(); break;

case 2: insert(); break;

case 3: deletion(); break;

case 4: searching(); break;

case 5: update(); break;

case 6: cout << "Exiting program. Goodbye!" << endl; break;

default: cout << "Invalid choice. Please try again." << endl;

} while (choice != 6);

Output

You might also like