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

Data Structures: Muhammad Hannan Farooq

The document contains code for three problems related to data structures in C++. Problem 1 contains code to find common elements between two arrays and store them in a third array. Problem 2 contains code for a jagged array that allows input and display of a matrix with variable column lengths per row, along with encoding and decoding of the sum. Problem 3 contains code for a List class with methods to insert, delete, and retrieve elements from a dynamic array.

Uploaded by

Rafay Farooq
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)
24 views

Data Structures: Muhammad Hannan Farooq

The document contains code for three problems related to data structures in C++. Problem 1 contains code to find common elements between two arrays and store them in a third array. Problem 2 contains code for a jagged array that allows input and display of a matrix with variable column lengths per row, along with encoding and decoding of the sum. Problem 3 contains code for a List class with methods to insert, delete, and retrieve elements from a dynamic array.

Uploaded by

Rafay Farooq
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/ 7

DATA STRUCTURES

MUHAMMAD HANNAN FAROOQ


PROBLEM 1
/*This Code Finds Common Names Between Two Array And Store Them in Third*/
#include <iostream>
using namespace std;

//It is used to find common namesin two arrays and store them in third
void findCommon(string arr1[], int s1, string arr2[], int s2, string arr3[], int s3)
{
int i3 = 0;
for (int i = 0; i < s1; i++)
{
for (int j = 0; j < s2; j++)
{
if (arr1[i] == arr2[j])
{
arr3[i3] = arr1[i];
i3++;
}
}
}
}
//It is used to display array
void display(string arr3[], int s3)
{
cout << "Array 3" << endl;
for (int i = 0; i < s3; i++)
{
cout << arr3[i] << " ";
}
}
int main()
{
int s1, s2, s3;
cout << "Enter Size of First Array: " << endl;
cin >> s1;
string *arr1 = new string[s1];
cout << "Enter Size of Second Array: " << endl;
cin >> s2;
if (s1 < s2)
s3 = s1;
else
s3 = s2;
string* arr2 = new string[s2];
string* arr3 = new string[s3];
cout << "Enter Names For First Array: " << endl;
for (int i = 0; i < s1; i++)
cin >> arr1[i];
cout << "Enter Names For Second Array: " << endl;
for (int i = 0; i < s2; i++)
cin >> arr2[i];
findCommon(arr1, s1, arr2, s2, arr3, s3);
display(arr3, s3);
delete[] arr1;
delete[] arr2;
delete[] arr3;
}
Problem 2
/*Creating Jacked Array And Encoding And Decoding*/
#include <iostream>
using namespace std;

//Inputting Elements
void inputtingElements(int** matrix, int row, int* col)
{
cout << "Enter Values:" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col[i]; j++)
{
cin >> matrix[i][j];
}
}
}
//Displaying Elements
void display(int** matrix, int row, int* col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col[i]; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int row;
cout << "Enter Number of Rows:" << endl;
cin >> row;
int** matrix = new int* [row];
int* col = new int[row];
for (int i = 0; i < row; i++)
{
cout << "Enter Number of Coloumns: " << endl;
cin >> col[i];
matrix[i] = new int[col[i]];
}
inputtingElements(matrix, row, col);
display(matrix, row, col);
int sum = 0;
for (int i = 0; i < row; i++)
for (int j = 0; j < col[i]; j++)
sum += matrix[i][j];
cout << "Sum: " << sum << endl;
double encode = (sum - 10) / 2.0;
cout << "Encoded: " << encode << endl;
double decode = (encode * 2) + 10;
cout << "Decoded: " << decode << endl;
for (int i = 0; i < row; i++)
{
delete[] matrix[i];
}
delete matrix;
return 0;
}
Problem 3
#include <iostream>
using namespace std;

class List
{
private:
int size;
int* array;
int count;
public:
List() : size(0), count(0)
{
array = NULL;
}
List(int noElements) : size(noElements), count(0)
{
array = new int[size];
}
~List()
{
delete[] array;
}
void insertElement(int X)
{
if (count >= size)
{
cout << "Your Position is greater than size of array" << endl;
}
else
{
array[count] = X;
count++;
}
}
void insertElementAt(int X, int pos)
{
if (pos >= size)
{
cout << "Your Position is greater than size of array" << endl;
}
else
array[pos] = X;
}
void printList()
{
cout << "Display: " << endl;
for (int i = 0; i < count; i++)
cout << array[i] << " ";
cout << endl;
}
bool deleteElement(int X)
{
for (int i = 0; i < size; i++)
{
if (array[i] == X)
{
array[i] == -1;
return true;
}
}
return false;
}
bool deleteElementAtAndShift(int X, int pos)
{
for (int i = 0; i < count; i++)
{
if (array[i] == X)
{
for (int j = i + 1; j < count; j++)
array[j - 1] = array[j];
count--;
return true;
}
}
return false;
}
bool isFull()
{
if (count == size)
return true;
else
return false;
}
bool isEmpty()
{
if (count == 0)
return true;
else
return false;
}
int length()
{
return count;
}
};

int main()
{

int noElements, choice;


cout << "Enter Size of Array: " << endl;
cin >> noElements;
List list(noElements);
cout << "M E N U" << endl;
cout << "1-Insert Element" << endl;
cout << "2-Insert Element At" << endl;
cout << "3-Print List" << endl;
cout << "4-Delete List" << endl;
cout << "5-Delete List At And Shift" << endl;
cout << "6-Full" << endl;
cout << "7-Empty" << endl;
cout << "8-Length" << endl;
cout << "Enter Choice: " << endl;
cin >> choice;
switch (choice)
{
case 1:
int X;
cout << "Enter Element To Insert: " << endl;
cin >> X;
list.insertElement(X);
break;
}
}

You might also like