Data Structures: Muhammad Hannan Farooq
Data Structures: Muhammad Hannan Farooq
//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()
{