11
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Structure to hold student details
struct Student {
int roll;
string name;
string division;
string address;
};
// Function to add a new student
void addStudent() {
Student s;
// Taking student input
cout << "Enter Roll Number: ";
cin >> s.roll;
cin.ignore(); // Clear input buffer
cout << "Enter Name: ";
getline(cin, s.name);
cout << "Enter Division: ";
getline(cin, s.division);
cout << "Enter Address: ";
getline(cin, s.address);
// Open files in append mode
ofstream dataFile("students.dat", ios::app); // data file (internal)
ofstream textFile("students.txt", ios::app); // readable text file
// Write to both files
if (dataFile && textFile) {
dataFile << s.roll << " " << s.name << " " << s.division << " " << s.address << endl;
textFile << "Roll: " << s.roll << ", Name: " << s.name
<< ", Division: " << s.division << ", Address: " << s.address << endl;
cout << "Student added successfully.\n";
} else {
cout << "Error opening file.\n";
}
// Close files
dataFile.close();
textFile.close();
}
// Function to display a student's details
void displayStudent() {
int searchRoll;
bool found = false;
cout << "Enter roll number to search: ";
cin >> searchRoll;
ifstream dataFile("students.dat"); // open data file for reading
Student s;
// Read file line by line
while (dataFile >> s.roll >> s.name >> s.division >> s.address) {
if (s.roll == searchRoll) {
// If roll number matches, display student info
cout << "Student Found:\n";
cout << "Roll: " << s.roll << "\nName: " << s.name
<< "\nDivision: " << s.division << "\nAddress: " << s.address << endl;
found = true;
break;
}
}
dataFile.close();
// If student not found
if (!found) {
cout << "Student record not found.\n";
}
}
// Function to delete a student record
void deleteStudent() {
int deleteRoll;
bool found = false;
cout << "Enter roll number to delete: ";
cin >> deleteRoll;
ifstream dataFile("students.dat"); // original data file
ofstream tempFile("temp.dat"); // temporary file for rewriting
ofstream newTextFile("students.txt"); // recreate text file without deleted student
Student s;
// Read each record and copy to temp file (except the one to delete)
while (dataFile >> s.roll >> s.name >> s.division >> s.address) {
if (s.roll == deleteRoll) {
found = true; // mark as found
continue; // skip writing this student
}
// Write to temp data file and recreate readable text file
tempFile << s.roll << " " << s.name << " " << s.division << " " << s.address << endl;
newTextFile << "Roll: " << s.roll << ", Name: " << s.name
<< ", Division: " << s.division << ", Address: " << s.address << endl;
}
// Close all files
dataFile.close();
tempFile.close();
newTextFile.close();
// Replace old file with new file
remove("students.dat");
rename("temp.dat", "students.dat");
if (found)
cout << "Student deleted successfully.\n";
else
cout << "Student not found.\n";
}
// Main menu
int main() {
int choice;
do {
// Display menu
cout << "\n--- Student Information System ---\n";
cout << "1. Add Student\n2. Display Student\n3. Delete Student\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
// Perform action based on choice
switch (choice) {
case 1: addStudent(); break;
case 2: displayStudent(); break;
case 3: deleteStudent(); break;
case 4: cout << "Exiting...\n"; break;
default: cout << "Invalid choice.\n";
}
} while (choice != 4); // Repeat until user chooses to exit
return 0;
}
6
#include <iostream>
using namespace std;
class graph
{
public:
int v;
int adjmatrix[10][10];
int visited[10];
graph(int v)
{
this->v=v;
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
{
adjmatrix[i][j]=0;
}
visited[i]=0;
}
}
void addEdge(int u,int v,int weight)
{
u--,v--;
adjmatrix[u][v]=weight;
adjmatrix[v][u]=weight;
}
void display()
{
cout<<"Adjacency matrix: \n";
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
{
cout<<adjmatrix[i][j]<<" ";
}
cout<<"\n";
}
}
bool isConnected()
{
visited[0]=1;
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
{
if(adjmatrix[i][j]>0)
{
visited[j]=1;
}
}
}
for(int i=0;i<v;i++)
{
if(!visited[i])
{
return false;
}
}
return true;
}
};
int main()
{
int v,e;
cout<<"Enter no. of cities: ";
cin>>v;
graph g(v);
cout<<"Enter no of flight: ";
cin>>e;
cout<<"Enter flights paths (city1 city2 cost) \n";
for(int i=0;i<e;i++)
{
int c1,c2,cost;
cin>>c1>>c2>>cost;
g.addEdge(c1,c2,cost);
}
g.display();
if (g.isConnected()) {
cout << "The graph is connected.\n";
} else {
cout << "The graph is not connected.\n";
}
return 0;
}