0% found this document useful (0 votes)
17 views11 pages

Dsalabpdf

dassda

Uploaded by

eesama30april04
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)
17 views11 pages

Dsalabpdf

dassda

Uploaded by

eesama30april04
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/ 11

// 1.

Write a program to create a class 'Student' to get and display student


details.

#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;

};

class Square : public Shape {


public:
void read() {
cout<<"Enter the length of sides of square "<<endl;
cin>>dimension1;
}
void area() {
float s_area = dimension1 * dimension1;
cout<<"Area of the Square is "<<s_area<<endl;
}
};

class Rectangle : public Shape {


public:
void get_data() {
cout<<"Enter the dimensions of rectangle "<<endl;
cin>>dimension1>>dimension2;
}
void area() {
float r_area = dimension1 * dimension2;
cout<<"Area of Rectangle is "<<r_area<<endl;
}
};

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;

bool linearSearch(int arr[],int size,int element) {


for(int i=0;i<size;i++) {
if(arr[i] == element)
return true;
}
return false;
}

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;

bool binarySearch(int arr[],int size,int element) {


int low = 0,high = size-1;
while(low <= high) {
int mid = (low+high)/2;
if(arr[mid] == element)
return true;
else if(element > arr[mid])
low = mid + 1;
else
high = mid - 1;

}
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;

void selectionSort(int arr[],int size) {


for(int i=0;i<size-1;i++) {
int minIndex = i;
for(int j=i+1;j<size;j++) {
if(arr[minIndex]>arr[j])
minIndex = j;
}
if(minIndex!=i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
void display(int arr[],int size) {
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
}

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;

void bubbleSort(int arr[],int size) {


for(int i=0;i<size-1;i++) {
bool swapped = false;
for(int j=0;j<size-i-1;j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
void display(int arr[],int size) {
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
}

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;

void insertionSort(int arr[],int size) {


int key,i,j;
for(i=1;i<size;i++) {
key = arr[i];
j=i-1;
while(j>=0 && arr[j]>key) {
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}

void display(int arr[],int size) {


for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
}

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);

You might also like