Professional Documents
Culture Documents
Dsa Lab 2 Alyan
Dsa Lab 2 Alyan
#2
ALYAN HUSSAIN
UET-4
DATA ALGORITHMS AND
PROBLEM SOLVING
TO: MS. SOBIA YOUSAF
DATE OF SUBMISSION: March 20, 2021
Complete Code of Program
#include<bits/stdc++.h>
using namespace std;
template <typename T> class dynamic_array
{
int size;
int capacity;
T* array;
public:
dynamic_array()
{
size = 0;
capacity = 1;
array = new T[capacity];
}
void push(T data)
{
if(capacity== size)
{
T* temp = new T[capacity*2];
for (size_t i = 0; i < size; i++)
{
temp[i] = array[i];
}
delete [] array;
array = temp;
}
array[size] = data;
size++;
DSA
}
void print()
{
for (size_t i = 0; i < size; i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}
void get_values(int n)
{
T input;
cout<<"Enter "<<n<<" values : "<<endl;
for (size_t i = 0; i < n; i++)
{
cin>>input;
this->push(input);
}
}
void sort(char order)
{
if(order=='A' || order=='a')
for (size_t i = 0; i < size-1; i++)
for (size_t j = 0; j < size-i-1; j++)
if(array[j] > array[j+1])
{
T temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
DSA
if(order=='D' || order=='d')
for (size_t i = 0; i < size-1; i++)
for (size_t j = 0; j < size-i-1; j++)
if(array[j] < array[j+1])
{
T temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
void print_even()
{
for (size_t i = 0; i < size; i++)
{
if(array[i]%2==0)
cout<<array[i]<<" ";
}
cout<<endl;
}
void print_odd()
{
for (size_t i = 0; i < size; i++)
{
if(array[i]%2==1)
cout<<array[i]<<" ";
}
cout<<endl;
}
void print_prime()
DSA
{
int count = 0;
for (size_t i = 0; i < size; i++)
{
for (size_t j = 2; j < array[i]; j++)
{
if(array[i]%j==0)
count++;
}
if(count==0)
{
cout<<array[i]<<" ";
}
count = 0;
}
cout<<endl;
}
void print_square()
{
for (size_t i = 0; i < size; i++)
for (size_t j = 2; j < (array[i]/2)+1; j++)
if(j*j==array[i])
cout<<array[i]<<" ";
cout<<endl;
}
};
int main()
{
dynamic_array <int> a; // size of a by default is 1
a.get_values(7);
a.print();
system("pause");
DSA
return 0;
}
Description of Code
This code is based on a Generic Class “dynamic_array”.
It has three private data members i.e. size, array and capacity.
It has one default constructor and 8 public functions (Ctrl + Click on names to go to their
respective code):
Default Constructor
Push()
Print()
Print_even()
Print_odd()
Print_prime()
Get_values()
Print_square()
Sort()
The names of these functions are enough to describe their functionality.
Lab Tasks
1. Declare an array whose size u can change at run time and insert new
elements:
Hold Ctrl and Click here to go to complete implementation of this task.
The array has been declared in the Default constructor of class dynamic_array. Initially the capacity of this
array is 1 and size is 0.
2. Write a Program to create a dynamic array has N = 7 elements. Get integer values into
array and then provide the following functionality:
Hold Ctrl and Click here to go to complete implementation of this task.
DSA
There is a function name get_values() which will accept one int parameter and then it will get that much values
from user. So I have passed 7 as an argument so this it will be able to accept 7 values from user.
Hold Ctrl and Click here to view the function.
Here, push() function is responsible to store values in the array and create a new array dynamically if required.
DSA
4. Print all even numbers available in the array:
Hold Ctrl and Click here to view the function.
DSA
7. Print all perfect squares available in the array:
Hold Ctrl and Click here to view the function.
These statements will dynamically create a new array, copy older elements in to that array and will get
more values from user.
Output of Code
DSA
OUTPUT OF TASK 3:
The data is sorted in descending order:
OUTPUT OF TASK 4:
Printing all even numbers:
OUTPUT OF TASK 5:
Printing all odd numbers:
OUTPUT OF TASK 6:
Printing all prime numbers:
DSA
OUTPUT OF TASK 7:
Printing all complete squares:
DSA