0% found this document useful (0 votes)
10 views

lab TASK 1-2

The document outlines several tasks involving C++ programming, including implementing functions for swapping values, creating classes for quadrilaterals and arcs, managing mobile device information, calculating grades, and performing CRUD operations on student courses. Each task includes example code demonstrating the required functionality, such as member functions for setting and displaying attributes, as well as using pointers and arrays. The tasks are structured to progressively build on C++ concepts, including object-oriented programming and data management.

Uploaded by

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

lab TASK 1-2

The document outlines several tasks involving C++ programming, including implementing functions for swapping values, creating classes for quadrilaterals and arcs, managing mobile device information, calculating grades, and performing CRUD operations on student courses. Each task includes example code demonstrating the required functionality, such as member functions for setting and displaying attributes, as well as using pointers and arrays. The tasks are structured to progressively build on C++ concepts, including object-oriented programming and data management.

Uploaded by

Ali Jatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Task 1

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>

using namespace std;

int swap(int &x, int &y)

int t;

t=x;

x=y;

y=t;

int main()

int a,b;

cout<<"enter 2 values: ";

cin>>a>>b;

cout<<" ---values before swap: "<<a<<" and "<<b<<endl;

swap(a,b);

cout<<"---values after swap: "<<a<<" and "<<b<<endl;

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>

using namespace std;

class Quadrilateral {

private:

float side1, side2, side3, side4;

float angle1, angle2;

public:

void setSides(float s1, float s2, float s3, float s4) {

side1 = s1;

side2 = s2;

side3 = s3;

side4 = s4;

void setAngles(float a1, float a2) {

angle1 = a1;

angle2 = a2;

float getSide1() const { return side1; }

float getSide2() const { return side2; }

float getSide3() const { return side3; }

float getSide4() const { return side4; }


float getAngle1() const { return angle1; }

float getAngle2() const { return angle2; }

float computePerimeter() const {

return side1 + side2 + side3 + side4;

float computeArea() const {

float s = computePerimeter() / 2;

float angle1_rad = angle1 * M_PI / 180.0;

float angle2_rad = angle2 * M_PI / 180.0;

float area = sqrt((s - side1) * (s - side2) * (s - side3) * (s - side4) -

side1 * side2 * side3 * side4 * cos((angle1_rad +


angle2_rad) / 2) *

cos((angle1_rad + angle2_rad) / 2));

return area;

void displayQuadrilateralDetails() const {

cout << "\nQuadrilateral Details:\n";

cout << "Sides: " << side1 << ", " << side2 << ", " << side3 << ",
" << side4 << " units"<<endl;

cout << "Angles: " << angle1 << "°, " << angle2 << endl;

cout << "Perimeter: " << computePerimeter() << " units"<<endl;

cout << "Area: " << computeArea() << " square units"<<endl;

};

int main() {

Quadrilateral quad;

quad.setSides(10, 12, 8, 14);

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>

using namespace std;

class Arc {

private:

float radius;

float angle;

float arc_length;

public:

void setRadius(float r) {

radius = r;

void setAngle(float a) {

angle = a;

void setArcLength(float length) {

arc_length = length;

void displayArcDetails() const {

cout << "\nArc Details:\n";


cout << "Radius: " << radius << " units" << endl;

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>

using namespace std;

class Mobile {

private:

int IMEIno;

string Type;

string Make;

int Modelno;
float Memory;

string Operating_System;

public:

void inputDetails() {

cout << "Enter IMEI Number: ";

cin >> IMEIno;

cout << "Enter Type (e.g., Smartphone/Feature Phone): ";

getline(cin, Type);

cout << "Enter Make (Brand): ";

getline(cin, Make);

cout << "Enter Model Number: ";

cin >> Modelno;

cout << "Enter Memory (GB): ";

cin >> Memory;

cout << "Enter Operating System: ";

getline(cin, Operating_System);

void displayDetails() {

cout << "\nMobile Details:\n";

cout << "IMEI Number: " << IMEIno << endl;

cout << "Type: " << Type << endl;

cout << "Make: " << Make << endl;

cout << "Model Number: " << Modelno << endl;

cout << "Memory: " << Memory << " GB" << endl;

cout << "Operating System: " << Operating_System << 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>

using namespace std;

class beis5A {

public:

int x;

void intro() {

cout << "---this is a program to check the grade" << endl <<
endl;

void input() {

cout << "enter marks: ";

cin >> x;

void grade() {

if (x <= 100 && x >= 90)

cout << "grade A";

else if (x < 90 && x >= 80)

cout << "grade B";

else if (x < 80 && x >= 70)


cout << "grade C";

else if (x < 70 && x >= 60)

cout << "grade D";

else if (x < 60 && x >= 0)

cout << "grade F";

else

cout << "---invalid input---enter marks between 0 - 100";

};

int main() {

beis5A b1, b2;

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;

void modifyValue(int* ptr) {

*ptr = 50;

void displayArray(int* arr, int size) {

cout << "Array elements using pointers: ";

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

cout << *(arr + i) << " ";

cout << endl;

int main() {

int num = 10;

int* ptr = &num;

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;

int arr[] = {1, 2, 3, 4, 5};

int size = sizeof(arr) / sizeof(arr[0]);

displayArray(arr, size);
return 0;

Task 6

Create a structure course with some attributes i.e course_ID, course_title,


credit_hrs etc.. Then Implement following functions (Known as CRUDS
operations which means CREATE, READ, UPDATE, DELETE, SEARCH
operations): 1. addAStudent 2. updateAStudent 3. deleteAStudent 4.
searchAndDisplayAStudent 5. displayAllstudents

CODE:
#include <iostream>

#include <string>

using namespace std;

struct Course {

int course_ID;

string course_title;
int credit_hrs;

string student_name;

};

void addAStudent() {

Course c;

cout << "Enter Course ID: ";

cin >> c.course_ID;

cin.ignore();

cout << "Enter Course Title: ";

getline(cin, c.course_title);

cout << "Enter Credit Hours: ";

cin >> c.credit_hrs;

cin.ignore();

cout << "Enter Student Name: ";

getline(cin, c.student_name);

courses.push_back(c);

cout << "Student added successfully!\n";

void updateAStudent(int id) {

for (auto& c : courses) {

if (c.course_ID == id) {

cout << "Enter new Course Title: ";

getline(cin, c.course_title);

cout << "Enter new Credit Hours: ";

cin >> c.credit_hrs;

cin.ignore();
cout << "Enter new Student Name: ";

getline(cin, c.student_name);

cout << "Student details updated!\n";

return;

cout << "Student with Course ID " << id << " not found!\n";

void deleteAStudent(int id) {

for (auto it = courses.begin(); it != courses.end(); ++it) {

if (it->course_ID == id) {

courses.erase(it);

cout << "Student record deleted successfully!\n";

return;

cout << "Student with Course ID " << id << " not found!\n";

void searchAndDisplayAStudent(int id) {

for (const auto& c : courses) {

if (c.course_ID == id) {

cout << "\nCourse ID: " << c.course_ID

<< "\nCourse Title: " << c.course_title

<< "\nCredit Hours: " << c.credit_hrs

<< "\nStudent Name: " << c.student_name << "\n";

return;

}
cout << "Student with Course ID " << id << " not found!\n";

void displayAllStudents() {

if (courses.empty()) {

cout << "No students found!\n";

return;

for (const auto& c : courses) {

cout << "\nCourse ID: " << c.course_ID

<< "\nCourse Title: " << c.course_title

<< "\nCredit Hours: " << c.credit_hrs

<< "\nStudent Name: " << c.student_name << "\n";

int main() {

int choice, id;

do {

cout << "\n--- Student Course Management ---\n";

cout << "1. Add a Student\n";

cout << "2. Update a Student\n";

cout << "3. Delete a Student\n";

cout << "4. Search and Display a Student\n";

cout << "5. Display All Students\n";

cout << "6. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

cin.ignore();

switch (choice) {

case 1: addAStudent(); break;


case 2:

cout << "Enter Course ID to update: ";

cin >> id;

cin.ignore();

updateAStudent(id);

break;

case 3:

cout << "Enter Course ID to delete: ";

cin >> id;

deleteAStudent(id);

break;

case 4:

cout << "Enter Course ID to search: ";

cin >> id;

searchAndDisplayAStudent(id);

break;

case 5: displayAllStudents(); break;

case 6: cout << "Exiting program...\n"; break;

default: cout << "Invalid choice! Try again.\n";

} while (choice != 6);

return 0;

Task 7

After that, create an array of 5 courses in main function. Create a menu in


main function to enable user to select and perform the operations we
created above. Program must not exit until and unless user wants to do
so. Sample Menu: Main Menu-------------- Press 1 to add a Course Press 2 to
update a Course Press 3 to delete a Course Press 4 to search and display a
Course Press 5 to display all Courses Press e to exit the program

CODE:
#include <iostream>

#include <string>

using namespace std;

struct Course {

int course_ID;

string course_title;

int credit_hrs;

string instructor;

};

// Array of courses

Course courses[5];

int courseCount = 0; // Track the number of added courses

void addACourse() {

if (courseCount >= 5) {

cout << "Cannot add more courses. Maximum limit reached!\n";

return;

cout << "Enter Course ID: ";

cin >> courses[courseCount].course_ID;

cin.ignore();

cout << "Enter Course Title: ";

getline(cin, courses[courseCount].course_title);

cout << "Enter Credit Hours: ";


cin >> courses[courseCount].credit_hrs;

cin.ignore();

cout << "Enter Instructor Name: ";

getline(cin, courses[courseCount].instructor);

courseCount++;

cout << "Course added successfully!\n";

void updateACourse() {

int id, index = -1;

cout << "Enter Course ID to update: ";

cin >> id;

cin.ignore();

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

if (courses[i].course_ID == id) {

index = i;

break;

if (index == -1) {

cout << "Course with ID " << id << " not found!\n";

return;

cout << "Enter new Course Title: ";

getline(cin, courses[index].course_title);
cout << "Enter new Credit Hours: ";

cin >> courses[index].credit_hrs;

cin.ignore();

cout << "Enter new Instructor Name: ";

getline(cin, courses[index].instructor);

cout << "Course updated successfully!\n";

void deleteACourse() {

int id, index = -1;

cout << "Enter Course ID to delete: ";

cin >> id;

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

if (courses[i].course_ID == id) {

index = i;

break;

if (index == -1) {

cout << "Course with ID " << id << " not found!\n";

return;

for (int i = index; i < courseCount - 1; i++) {

courses[i] = courses[i + 1];

}
courseCount--;

cout << "Course deleted successfully!\n";

void searchAndDisplayACourse() {

int id;

cout << "Enter Course ID to search: ";

cin >> id;

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

if (courses[i].course_ID == id) {

cout << "\nCourse ID: " << courses[i].course_ID

<< "\nCourse Title: " << courses[i].course_title

<< "\nCredit Hours: " << courses[i].credit_hrs

<< "\nInstructor: " << courses[i].instructor << "\n";

return;

cout << "Course with ID " << id << " not found!\n";

void displayAllCourses() {

if (courseCount == 0) {

cout << "No courses available!\n";

return;

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

cout << "\nCourse ID: " << courses[i].course_ID

<< "\nCourse Title: " << courses[i].course_title


<< "\nCredit Hours: " << courses[i].credit_hrs

<< "\nInstructor: " << courses[i].instructor << "\n";

int main() {

char choice;

do {

cout << "\n--- Course Management System ---\n";

cout << "1. Add a Course\n";

cout << "2. Update a Course\n";

cout << "3. Delete a Course\n";

cout << "4. Search and Display a Course\n";

cout << "5. Display All Courses\n";

cout << "e. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

cin.ignore();

switch (choice) {

case '1': addACourse(); break;

case '2': updateACourse(); break;

case '3': deleteACourse(); break;

case '4': searchAndDisplayACourse(); break;

case '5': displayAllCourses(); break;

case 'e': cout << "Exiting program...\n"; break;

default: cout << "Invalid choice! Try again.\n";

} while (choice != 'e');


return 0;

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>

using namespace std;


double arr_avg(int arr[], int size) {

double sum = 0;

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

sum += arr[i];

return sum / size;

int main() {

int numbers[6] = {10, 20, 30, 40, 50, 60};

double average = arr_avg(numbers, 6);

cout << "The average of the array elements is: " << average << endl;

return 0;

After that, create an array of 5 courses in main function. Create a menu in


main function to enable user to select and perform the operations we
created above. Program must not exit until and unless user wants to do
so. Sample Menu: Main Menu-------------- Press 1 to add a Course Press 2 to
update a Course Press 3 to delete a Course Press 4 to search and display a
Course Press 5 to display all Courses Press e to exit the program

You might also like