0% found this document useful (0 votes)
10 views9 pages

DS Lab Assignment 2

The document contains multiple C++ programs demonstrating various functionalities such as finding the median of two sorted arrays, managing student and employee records using structures, and file operations including creating, reading, and searching in a file. Each program includes user interaction for data input and displays results accordingly. The author is identified as Rahul Choudhary with a specific ID.

Uploaded by

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

DS Lab Assignment 2

The document contains multiple C++ programs demonstrating various functionalities such as finding the median of two sorted arrays, managing student and employee records using structures, and file operations including creating, reading, and searching in a file. Each program includes user interaction for data input and displays results accordingly. The author is identified as Rahul Choudhary with a specific ID.

Uploaded by

sujal.dtu.04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment 2

Q1 WAP to find the Median of the elements after merging the two sorted Arrays of same size
Solution :
#include <bits/stdc++.h>
using namespace std;

double findMedian(vector<int>& arr1, vector<int>& arr2) {


int n = [Link]();
vector<int> merged(2*n);
merge([Link](), [Link](), [Link](), [Link](), [Link]());
int mid1 = (2*n - 1) / 2;
int mid2 = (2*n) / 2;
return (merged[mid1] + merged[mid2]) / 2.0;
}

int main() {
cout << "Rahul Choudhary" << endl;
cout << "24/IT/143" << endl;

int n;
cout << "Enter size of arrays: ";
cin >> n;

vector<int> arr1(n), arr2(n);


cout << "Enter elements of first sorted array: ";
for(int i=0; i<n; i++) cin >> arr1[i];

cout << "Enter elements of second sorted array: ";


for(int i=0; i<n; i++) cin >> arr2[i];

cout << "Median = " << findMedian(arr1, arr2) << endl;


return 0;
}
Q2 WAP to store an information in array of structure, dynamically, and also give a function to
display the current information. The program should give user a choice for inserting a data or to
display the current data. Implement the program for Student structures (contains student_name,
student_roll_no, total_marks).

Solution:
#include <bits/stdc++.h>
using namespace std;

struct Student {
string name;
int roll_no;
float total_marks;
};

void display(vector<Student>& students) {


cout << "\n--- Student Records ---\n";
for(auto &s : students) {
cout << "Name: " << [Link]
<< ", Roll No: " << s.roll_no
<< ", Marks: " << s.total_marks << endl;
}
cout << "------------------------\n";
}

int main() {
cout << "Rahul Choudhary" << endl;
cout << "24/IT/143" << endl;

vector<Student> students;
int choice;

do {
cout << "\n1. Insert Student\n2. Display Students\n3. Exit\nEnter choice: ";
cin >> choice;

if(choice == 1) {
Student s;
cout << "Enter name, roll no, total marks: ";
cin >> [Link] >> s.roll_no >> s.total_marks;
students.push_back(s);
}
else if(choice == 2) {
display(students);
}
} while(choice != 3);

return 0;
}

Q3 Implement the above program for Employee structures (contains employee_name, emp_no,
emp_salary)

Solution :
#include <bits/stdc++.h>
using namespace std;

struct Employee {
string name;
int emp_no;
float salary;
};
void display(vector<Employee>& empList) {
cout << "\n--- Employee Records ---\n";
for(auto &e : empList) {
cout << "Name: " << [Link]
<< ", Emp No: " << e.emp_no
<< ", Salary: " << [Link] << endl;
}
cout << "------------------------\n";
}

int main() {
cout << "Rahul Choudhary" << endl;
cout << "24/IT/143" << endl;

vector<Employee> empList;
int choice;

do {
cout << "\n1. Insert Employee\n2. Display Employees\n3. Exit\nEnter choice: ";
cin >> choice;

if(choice == 1) {
Employee e;
cout << "Enter name, emp_no, salary: ";
cin >> [Link] >> e.emp_no >> [Link];
empList.push_back(e);
}
else if(choice == 2) {
display(empList);
}
} while(choice != 3);

return 0;
}
Q4 Implement the above program for Faculty structures (contains faculty_name, faculty_ID,
subject_codes, class_names)

Solution
#include <bits/stdc++.h>
using namespace std;

struct Faculty {
string name;
int faculty_ID;
string subject_codes;
string class_names;
};

void display(vector<Faculty>& faculties) {


cout << "\n--- Faculty Records ---\n";
for(auto &f : faculties) {
cout << "Name: " << [Link]
<< ", ID: " << f.faculty_ID
<< ", Subject Codes: " << f.subject_codes
<< ", Class Names: " << f.class_names << endl;
}
cout << "------------------------\n";
}

int main() {
cout << "Rahul Choudhary" << endl;
cout << "24/IT/143" << endl;

vector<Faculty> faculties;
int choice;

do {
cout << "\n1. Insert Faculty\n2. Display Faculties\n3. Exit\nEnter choice: ";
cin >> choice;

if(choice == 1) {
Faculty f;
cout << "Enter name, faculty_ID, subject_codes, class_names: ";
cin >> [Link] >> f.faculty_ID >> f.subject_codes >> f.class_names;
faculties.push_back(f);
}
else if(choice == 2) {
display(faculties);
}
} while(choice != 3);

return 0;
}
Q5 WAP to Create a new file, Open an existing file, read the file to search a given word, write
into the file and close the file

Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
cout << "Rahul Choudhary" << endl;
cout << "24/IT/143" << endl;

fstream file;
const string filename = "[Link]";
int choice;

do {
cout << "\n1. Create/Write File\n2. Read File\n3. Search Word\n4. Exit\nEnter choice: ";
if (!(cin >> choice)) return 0;

if (choice == 1) {
[Link](filename, ios::out);
if (!file) {
cerr << "Error opening file for writing.\n";
continue;
}
cout << "Enter text to write into file (end with '#'):\n";
[Link](numeric_limits<streamsize>::max(), '\n');
string data;
getline(cin, data, '#');
file << data;
[Link]();
cout << "Written to file.\n";
}
else if (choice == 2) {
[Link](filename, ios::in);
if (!file) {
cerr << "Error opening file for reading.\n";
continue;
}
cout << "\nFile Contents:\n";
string line;
while (getline(file, line)) {
cout << line << '\n';
}
[Link]();
}
else if (choice == 3) {
[Link](filename, ios::in);
if (!file) {
cerr << "Error opening file for searching.\n";
continue;
}
cout << "Enter word to search: ";
string needle;
cin >> needle;
bool found = false;
string line;
while (getline(file, line)) {
if ([Link](needle) != string::npos) {
found = true;
break;
}
}
[Link]();
cout << (found ? "Word found in file.\n" : "Word not found.\n");
}
} while (choice != 4);

return 0;
}

You might also like