BS(IT),SEMESTER#01
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
Task1: Student Information Swap Task
You're managing student information for a school. Your task is to implement a function that swaps
the information of two students. This task will help you understand how pointers can be applied
to real-world problems.
Code:
#include <iostream>
#include <string> // For using the string data type
using namespace std;
// Define a structure to hold student information
struct Student {
string name;
int age;
string grade;
};
// Function to swap the information of two students
void swapStudentInfo(Student &student1, Student &student2) {
// Temporary variable to hold student1's information
Student temp = student1;
// Swap the information between the two students
student1 = student2;
student2 = temp;
}
Department of Computer 1/10 Semester BS IT 1
Sciences CSL-113: Computer
Programming Lab
, BS(IT),SEMESTER#01
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
int main() {
// Create two student objects and assign values to their attributes
Student student1 = {"John", 15, "10th"};
Student student2 = {"Alice", 16, "11th"};
// Print student information before the swap
cout << "Before swapping:" << endl;
cout << "Student 1: " << student1.name << ", Age: " << student1.age << ", Grade: " << student1.grade << endl;
cout << "Student 2: " << student2.name << ", Age: " << student2.age << ", Grade: " << student2.grade << endl;
// Call the function to swap the students' information
swapStudentInfo(student1, student2);
// Print student information after the swap
cout << "\nAfter swapping:" << endl;
cout << "Student 1: " << student1.name << ", Age: " << student1.age << ", Grade: " << student1.grade << endl;
cout << "Student 2: " << student2.name << ", Age: " << student2.age << ", Grade: " << student2.grade << endl;
return 0;
}
Output:
Department of Computer 2/10 Semester BS IT 1
Sciences CSL-113: Computer
Programming Lab
Task2: Managing expenses
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
Task1: Student Information Swap Task
You're managing student information for a school. Your task is to implement a function that swaps
the information of two students. This task will help you understand how pointers can be applied
to real-world problems.
Code:
#include <iostream>
#include <string> // For using the string data type
using namespace std;
// Define a structure to hold student information
struct Student {
string name;
int age;
string grade;
};
// Function to swap the information of two students
void swapStudentInfo(Student &student1, Student &student2) {
// Temporary variable to hold student1's information
Student temp = student1;
// Swap the information between the two students
student1 = student2;
student2 = temp;
}
Department of Computer 1/10 Semester BS IT 1
Sciences CSL-113: Computer
Programming Lab
, BS(IT),SEMESTER#01
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
int main() {
// Create two student objects and assign values to their attributes
Student student1 = {"John", 15, "10th"};
Student student2 = {"Alice", 16, "11th"};
// Print student information before the swap
cout << "Before swapping:" << endl;
cout << "Student 1: " << student1.name << ", Age: " << student1.age << ", Grade: " << student1.grade << endl;
cout << "Student 2: " << student2.name << ", Age: " << student2.age << ", Grade: " << student2.grade << endl;
// Call the function to swap the students' information
swapStudentInfo(student1, student2);
// Print student information after the swap
cout << "\nAfter swapping:" << endl;
cout << "Student 1: " << student1.name << ", Age: " << student1.age << ", Grade: " << student1.grade << endl;
cout << "Student 2: " << student2.name << ", Age: " << student2.age << ", Grade: " << student2.grade << endl;
return 0;
}
Output:
Department of Computer 2/10 Semester BS IT 1
Sciences CSL-113: Computer
Programming Lab
Task2: Managing expenses