lab TASK 1-2
lab TASK 1-2
Implement c++ code to write a swap() function using Pass by Value and
Pass by Reference-How can you implement a swap function in C++ using
pass by value?-Can you illustrate how to implement a swap function in C+
+ using pass by reference?
CODE:
#include<iostream>
int t;
t=x;
x=y;
y=t;
int main()
int a,b;
cin>>a>>b;
swap(a,b);
Task 2
Write a class called quadrilateral. Your task is to store the length of all the
sides of the quadrilateral and the value of the 2 opposite angles within the
quadrilateral. Then implement member functions:
To compute the area of the quadrilateral.
To compute the parameter of the quadrilateral.
A constant function that will display the length of all the sides, the
angles, the parameter of the quadrilateral and area of the
quadrilateral.
Create setter and getter methods.
Demonstrate the use of the object in the main function. Make sure
that the function names are meaningful and self-descriptive.
CODE:
#include <iostream>
#include <cmath>
class Quadrilateral {
private:
public:
side1 = s1;
side2 = s2;
side3 = s3;
side4 = s4;
angle1 = a1;
angle2 = a2;
float s = computePerimeter() / 2;
return area;
cout << "Sides: " << side1 << ", " << side2 << ", " << side3 << ",
" << side4 << " units"<<endl;
cout << "Angles: " << angle1 << "°, " << angle2 << endl;
cout << "Area: " << computeArea() << " square units"<<endl;
};
int main() {
Quadrilateral quad;
quad.setAngles(60, 70);
quad.displayQuadrilateralDetails();
return 0;
}
Task 3
Write a program that creates a class called arc. The data members of the
class are radius, angle and arc_length of the arc is provided. Write three
functions that set the respective values, and then write a single constant
function that will read the values. Demonstrate the use of the object in the
main function. Make sure that the function names are meaningful and self-
descriptive
CODE:
#include <iostream>
class Arc {
private:
float radius;
float angle;
float arc_length;
public:
void setRadius(float r) {
radius = r;
void setAngle(float a) {
angle = a;
arc_length = length;
cout << "Angle: " << angle << " degrees" << endl;
cout << "Arc Length: " << arc_length << " units" << endl;
};
int main() {
Arc myArc;
myArc.setRadius(10.5);
myArc.setAngle(45.0);
myArc.setArcLength(8.2);
myArc.displayArcDetails();
return 0;
Task 4
Create a class . The data members of the class are IMEIno (int), Type
(String), Make (String), Modelno (int), Memory(float),
Operating_System(String). Then Implement member functions to: 1. Set
the values of all data members. 2. Display the values of all data member
CODE:
#include <iostream>
class Mobile {
private:
int IMEIno;
string Type;
string Make;
int Modelno;
float Memory;
string Operating_System;
public:
void inputDetails() {
getline(cin, Type);
getline(cin, Make);
getline(cin, Operating_System);
void displayDetails() {
cout << "Memory: " << Memory << " GB" << endl;
};
int main() {
Mobile phone;
phone.inputDetails();
phone.displayDetails();
return 0;
Task 5
Create a class. that takes input marks from user and calculate the grade
as per the grading criteria. Make appropriate class functions and objects
CODE:
#include<iostream>
class beis5A {
public:
int x;
void intro() {
cout << "---this is a program to check the grade" << endl <<
endl;
void input() {
cin >> x;
void grade() {
else
};
int main() {
b1.intro();
b2.input();
b2.grade();
return 0;
Task 5
2.Pointers can hold the address of another variable and this is called
referencing the memory location. When we attempt to extract values from
a memory location then this is called dereferencing a pointer-What is the
significance of pointers in C++?-Define referencing a memory location in
the context of pointers.-Explain the concept of dereferencing a pointer
and its importance in C++
CODE:
#include <iostream>
using namespace std;
*ptr = 50;
int main() {
cout << "Original value of num: " << num << endl;
cout << "Memory address of num: " << &num << endl;
cout << "Pointer ptr stores address: " << ptr << endl;
cout << "Dereferencing pointer (value at ptr): " << *ptr << endl;
*ptr = 20;
cout << "Modified value of num using pointer: " << num << endl;
modifyValue(&num);
cout << "Value after function call: " << num << endl;
displayArray(arr, size);
return 0;
Task 6
CODE:
#include <iostream>
#include <string>
struct Course {
int course_ID;
string course_title;
int credit_hrs;
string student_name;
};
void addAStudent() {
Course c;
cin.ignore();
getline(cin, c.course_title);
cin.ignore();
getline(cin, c.student_name);
courses.push_back(c);
if (c.course_ID == id) {
getline(cin, c.course_title);
cin.ignore();
cout << "Enter new Student Name: ";
getline(cin, c.student_name);
return;
cout << "Student with Course ID " << id << " not found!\n";
if (it->course_ID == id) {
courses.erase(it);
return;
cout << "Student with Course ID " << id << " not found!\n";
if (c.course_ID == id) {
return;
}
cout << "Student with Course ID " << id << " not found!\n";
void displayAllStudents() {
if (courses.empty()) {
return;
int main() {
do {
cin.ignore();
switch (choice) {
cin.ignore();
updateAStudent(id);
break;
case 3:
deleteAStudent(id);
break;
case 4:
searchAndDisplayAStudent(id);
break;
return 0;
Task 7
CODE:
#include <iostream>
#include <string>
struct Course {
int course_ID;
string course_title;
int credit_hrs;
string instructor;
};
// Array of courses
Course courses[5];
void addACourse() {
if (courseCount >= 5) {
return;
cin.ignore();
getline(cin, courses[courseCount].course_title);
cin.ignore();
getline(cin, courses[courseCount].instructor);
courseCount++;
void updateACourse() {
cin.ignore();
if (courses[i].course_ID == id) {
index = i;
break;
if (index == -1) {
cout << "Course with ID " << id << " not found!\n";
return;
getline(cin, courses[index].course_title);
cout << "Enter new Credit Hours: ";
cin.ignore();
getline(cin, courses[index].instructor);
void deleteACourse() {
if (courses[i].course_ID == id) {
index = i;
break;
if (index == -1) {
cout << "Course with ID " << id << " not found!\n";
return;
}
courseCount--;
void searchAndDisplayACourse() {
int id;
if (courses[i].course_ID == id) {
return;
cout << "Course with ID " << id << " not found!\n";
void displayAllCourses() {
if (courseCount == 0) {
return;
int main() {
char choice;
do {
cin.ignore();
switch (choice) {
Task 8
.Write a C++ program that creates (in main function) an array of type int
having 6 elements. Now write a function called arr_avg( ) that will find the
average of all the elements of the array.-How can you create an array of
integers in C++?-Could you provide a C++ function to calculate the
average of elements in an array?-Can you demonstrate how to call the
function to find the average of elements in the created array?
CODE:
#include <iostream>
double sum = 0;
sum += arr[i];
int main() {
cout << "The average of the array elements is: " << average << endl;
return 0;