Dsalabpdf
Dsalabpdf
#include<iostream>
using namespace std;
class Student {
private:
string name;
int roll_no;
float attendance;
float total_marks;
public:
void get_data() {
cout<<"Enter the following details "<<endl<<"1.Name
"<<endl<<"2.Roll no"<<endl<<"3.Attendance "<<endl<<"4.TOtal
Mark"<<endl;
cin>>name>>roll_no>>attendance>>total_marks;
}
void display_details() {
cout<<"Name : "<<name<<endl<<"Roll No :
"<<roll_no<<endl<<"Attendance :
"<<attendance<<"%"<<endl<<"Total MArks : "<<total_marks<<endl;
}
};
int main() {
Student S1;
S1.get_data();
S1.display_details();
}
// 2. Write a program to find the area of a square and a rectangle without inheritance
#include<iostream>
using namespace std;
class Square {
private:
float side;
public:
void get_data() {
cout<<"Enter the length of side "<<endl;
cin>>side;
}
void square_area() {
float area = side*side;
cout<<"Area of the Square is "<<area<<endl;
}
};
class Rectangle {
private:
float length;
float breadth;
public:
void read() {
cout<<"Enter the length and breadth : "<<endl;
cin>>length>>breadth;
}
void rect_area() {
float area = length * breadth;
cout<<"Area of the Rectangle is "<<area<<endl;
}
};
int main() {
int n;
cout<<"Select: 1.Square 2.Rectangle"<<endl;
cin>>n;
switch(n){
case 1:
Square S;
S.get_data();
S.square_area();
break;
case 2:
Rectangle R;
R.read();
R.rect_area();
break;
default:
cout<<"Invalid Choice"<<endl;
}
}
//3. Write a program to find the area of a square and a rectangle using inheritance
#include<iostream>
using namespace std;
class Shape {
protected:
float dimension1;
float dimension2;
public:
virtual void area() = 0;
};
int main() {
Square S;
Rectangle R;
int n;
cout<<"Select: 1.Square 2.Rectangle"<<endl;
cin>>n;
switch(n) {
case 1:
S.read();
S.area();
break;
case 2:
R.get_data();
R.area();
break;
default:
cout<<"Invalid choice"<<endl;
}
}
// 4. Write a program to find the area of various geometric shapes (square, rectangle, circle and triangle
[using the three sides equation]) using function overloading.
#include<iostream>
#include<cmath>
using namespace std;
class Geometry {
public:
float area(float side) {
return side * side;
}
float area(float length, float breadth) {
return length * breadth;
}
double area(double radius) {
return 3.141 * radius * radius;
}
float area(float a, float b, float c) {
float s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
};
int main() {
Geometry g;
int n;
float side;
cout<<"Select 1.Square 2.Rectangle 3.Circle 4.Triangle"<<endl;
cin>>n;
switch(n){
case 1:
cout << "Enter the side length of the square: ";
cin >> side;
cout << "Area of the Square: " << g.area(side) << endl;
break;
case 2:
float length, breadth;
cout << "Enter the length and breadth of the rectangle: ";
cin >> length >> breadth;
cout << "Area of the Rectangle: " << g.area(length, breadth) << endl;
break;
case 3:
double radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "Area of the Circle: " << g.area(radius) << endl;
break;
case 4:
float a, b, c;
cout << "Enter the three sides of the triangle: ";
cin >> a >> b >> c;
cout << "Area of the Triangle: " << g.area(a, b, c) << endl;
break;
default: cout<<"Invalid Choice "<<endl;
}
return 0;
}
//5.Insertion deletion and traversal
#include<iostream>
using namespace std;
class Array {
private:
int arr[100];
int size;
public:
Array(int sizee) : size(sizee) {
for (int i = 0; i < 100; i++)
arr[i] = 0;
}
void initialise() {
cout<<"Enter the elements of array"<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
}
void insertion(int item) {
int pos;
cout<<"Enter the position to insert"<<endl;
cin>>pos;
if(pos<=0 || pos>size)
cout<<"Insertion not possible!!"<<endl;
else {
for(int i=size;i>=pos;i--)
arr[i] = arr[i-1];
arr[pos-1] = item;
size++;
}
}
void deletion() {
int pos;
cout<<"enter the position to delete"<<endl;
cin>>pos;
if(pos<=size && pos>0) {
for(int i=pos;i<size;i++)
arr[i-1] = arr[i];
size--;
}
else
cout << "Invalid position! Valid positions are between 1 and " << size <<
"." << endl;
}
void traversal() {
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};
int main() {
int size,item,n;
cout<<"Enter the size of array"<<endl;
cin>>size;
Array A1(size);
A1.initialise();
cout<<"Select 1.Insertion 2.Deletion "<<endl;
cin>>n;
switch(n) {
case 1:
cout<<"Enter the element to insert"<<endl;
cin>>item;
A1.insertion(item);
break;
case 2:
A1.deletion();
break;
default:
cout<<"Invalid Entry "<<endl;
}
cout<<"Final Array : ";
A1.traversal();
}
//6.linear search
#include<iostream>
using namespace std;
int main() {
int arr[100],size,element;
cout<<"Enter the size of array "<<endl;
cin>>size;
cout<<"Enter the elements of array "<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"Enter the element to search "<<endl;
cin>>element;
bool result = linearSearch(arr,size,element);
if(result)
cout<<"Search successful "<<endl;
else
cout<<"Search unsuccesful"<<endl;
}
//7.binary search
#include<iostream>
using namespace std;
}
return false;
}
int main() {
int arr[100],size,element;
cout<<"Enter the size of array"<<endl;
cin>>size;
cout<<"Enter a sorted array"<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"Enter the element to search "<<endl;
cin>>element;
bool result = binarySearch(arr,size,element);
if(result)
cout<<"Search successful"<<endl;
else
cout<<"Search unsuccessful"<<endl;
}
//8.selection sort
#include<iostream>
using namespace std;
int main() {
int arr[100],size;
cout<<"Enter the size of array"<<endl;
cin>>size;
cout<<"Enter the elements of array"<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting: ";
display(arr,size);
selectionSort(arr,size);
cout<<"After Sorting: ";
display(arr,size);
}
//9.bubble sort
#include<iostream>
using namespace std;
int main() {
int arr[100],size;
cout<<"Enter the size of array"<<endl;
cin>>size;
cout<<"Enter the elements of array"<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting: ";
display(arr,size);
bubbleSort(arr,size);
cout<<"After Sorting: ";
display(arr,size);
}
//10.insertion sort
#include<iostream>
using namespace std;
int main() {
int arr[100],size;
cout<<"Enter the size of array"<<endl;
cin>>size;
cout<<"Enter the elements of array"<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting: ";
display(arr,size);
insertionSort(arr,size);
cout<<"After Sorting: ";
display(arr,size);