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

Task 01

The document describes an ArrayADT class that implements basic array operations like insertion, deletion, retrieval of elements. It includes a main function that demonstrates using the ArrayADT class to perform these operations on an array of size determined by user input. The user can choose from menu options to insert, delete, retrieve elements or display the 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)
35 views

Task 01

The document describes an ArrayADT class that implements basic array operations like insertion, deletion, retrieval of elements. It includes a main function that demonstrates using the ArrayADT class to perform these operations on an array of size determined by user input. The user can choose from menu options to insert, delete, retrieve elements or display the 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/ 13

Task 01

#include <iostream>
using namespace std;

//Creating Class ArrayADT


class ArrayADT
{
private:
//Initializing Private Members
int* arr;
int size;
int count = 0;
public:
//Constructor To Dynamically Allocate An Array
ArrayADT(int sz) : size(sz)
{
arr = new int[size];
}
//Destructor To Delete An Array
~ArrayADT()
{
delete[] arr;
}
//Returning Element At Specific Index
int get(int pos)
{
if (pos >= count || pos < 0)
return -1;
return arr[pos];
}
//Inserting Data
bool insert(int data, int pos)
{
if (pos > count || pos < 0 || count == size)
return false;
for (int i = count; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = data;
count++;
return true;
}
//Deleting Data
bool delet(int pos)
{
if (pos >= count || pos < 0)
return false;
for (int i = pos; i < count - 1; i++)
arr[i] = arr[i + 1];
count--;
return true;
}
//Appending Element
bool append(int data)
{
if (count == size)
{
cout << "Array is Full So Cant Append" << endl;
return false;
}
arr[count] = data;
count++;
return true;
}
//Returning Length of Array
int getSize()
{
return count;
}
//EvenOdd Function Not Working
/*int EvenOdd(int data)
{
if (count == size)
{
return false;
}
for (int i = 0; i < size; i++)
{
if (data % 2 == 0 && i % 2 == 0)
{
for (int j = count; j > i; j--)
arr[j] = arr[j - 1];
arr[i] = data;
count++;
return true;
}
else if (data % 2 != 0 && i % 2 != 0)
{
for (int j = count; j > i; j--)
arr[j] = arr[j - 1];
arr[i] = data;
count++;
return true;
}
}*/
/*}*/
//Printing Divisors
void printDivisors()
{
for (int i = 0; i < count; i++)
{
cout << arr[i] << ":" << " ";
for (int j = 1; j <= arr[i] / 2; j++)
{
if (arr[i] % j == 0)
cout << j << " ";
}
cout << endl;
}
}
//Displaying Array
void display()
{
cout << "List is" << endl;
for (int i = 0; i < count; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main()
{
int choice, x, size, pos, exit = 0;
cout << "Enter Size of the Array: " << endl;
cin >> size;
ArrayADT list(size);
while (true)
{
cout << "Enter Choice : " << endl;
cout << "1.Insert" << endl;
cout << "2.Delete" << endl;
cout << "3.Get an Element" << endl;
cout << "4.Size" << endl;
cout << "5.Append" << endl;
cout << "6.Display" << endl;
cout << "7.Exit" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter Element:" << endl;
cin >> x;
cout << "Enter Position:" << endl;
cin >> pos;
if (list.insert(x, pos) == true)
cout << "Element Inserted Successfully" << endl;
else if (list.insert(x, pos) == false)
cout << "Incorrect Position or Array is Full" << endl;
break;
case 2:
cout << "Enter Position:" << endl;
cin >> pos;
if (list.delet(pos) == true)
cout << "Element Deleted Successfully" << endl;
else if (list.delet(pos) == false)
cout << "Incorrect Position or Array is Empty" << endl;
break;
case 3:
cout << "Enter Position:" << endl;
cin >> pos;
cout << "Your Desired Index Data:" << endl;
cout << list.get(pos) << endl;
break;
case 4:
cout << "Length: " << list.getSize() << endl;
break;
case 5:
cout << "Enter Element to Append: " << endl;
cin >> x;
if (list.append(x) == true)
cout << "Element Appended" << endl;
else
cout << "Element Not Appended" << endl;
break;
case 6:
list.display();
break;
case 7:
exit = -1;
break;
/*case 8:
cout << "Enter Element:" << endl;
cin >> x;
if (list.EvenOdd(x) == true)
cout << "Entered Correctly" << endl;
break;*/
case 8:
cout << "Divisors:" << endl;
list.printDivisors();
break;
}
if (exit == -1)
break;
}
return 0;
}
Task 02
#include <iostream>
using namespace std;

//Inputting Elements
void userInput(int *arr, int size)
{
cout << "Enter Inputs:" << endl;
for (int i = 0; i < size; i++)
{
cout << "Element " << i + 1 << endl;
cin >> *(arr + i);
}
}
//Displaying Elements
void display(int* arr, int size)
{
cout << "Original Array" << endl;
for (int i = 0; i < size; i++)
cout << *(arr + i) << " ";
cout << endl;
}
//Displaying Desired Elements
void displayDesiredArray(int* arr, int size)
{
cout << "Desired Array" << endl;
for (int i = 0; i < size; i++)
{
int count = 0;
for (int j = 0; j < size; j++)
{
if (*(arr + i) < *(arr + j))
count++;
}
if (count >= 2)
cout << *(arr + i) << " ";
}
cout << endl;
}
int main()
{
const int size = 10;
int array[size];
int* arr = array;
userInput(arr, size);
display(arr, size);
displayDesiredArray(arr, size);
return 0;
}
Task 03
#include<iostream>
using namespace std;

//Inputting Elements
void userInput(int* arr, int size)
{
cout << "Enter Inputs:" << endl;
for (int i = 0; i < size; i++)
{
cout << "Element " << i + 1 << ": ";
cin >> *(arr + i);
}
}

//Displaying Elements
void display(int* arr, int size)
{
for (int i = 0; i < size; i++)
cout << *(arr + i) << " ";
cout << endl;
}

//Sorting According To Given Question


void sort(int* arr, int size)
{
//Sorting First Half of Array in Ascending Order
for (int i = 0; i < size / 2; i++)
{
for (int j = 0; j < size / 2 - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//Sorting Second Half of Array in Descending Order
for (int i = size / 2; i < size; i++)
{
for (int j = size / 2; j < size - 1; j++)
{
if (arr[j] < arr[j + 1])
{
int temp2 = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp2;
}
}
}
cout << "Sorted Array" << endl;
display(arr, size);
}
//Updating Array
void updatedArray(int* arr, int size)
{
int* arr2 = new int[size];
for (int i = 0; i < size; i++)
{
if (i == 0)
arr2[i] = arr[i] * arr[i + 1] * arr[size - 1];
else if(i == size - 1)
arr2[i] = arr[i] * arr[i - 1] * arr[0];
else
arr2[i] = arr[i] * arr[i + 1] * arr[i - 1];
}
for (int i = 0; i < size; i++)
arr[i] = arr2[i];
delete[]arr2;
cout << "Updated Array" << endl;
display(arr, size);
}
int main()
{
int size = 10;
int* arr = new int[size];
userInput(arr, size);
cout << "Original Array" << endl;
display(arr, size);
sort(arr, size);
updatedArray(arr, size);
delete[]arr;
return 0;
}

Task 04
#include <iostream>
using namespace std;

//Structure Containing Student Details


struct Student
{
string name = "";
int id = 0;
float gpa = 0.0;
};

//Initializing Records
void initializeRecords(Student*& records, int num)
{
records = new Student[num];
}

//Adding Record
void addRecord(Student* records, int& count, int num)
{
if (count < 0 || count >= num || num == 0)
{
cout << "Invalid Operation" << endl;
return;
}
cout << "Enter Name: " << endl;
cin >> records[count].name;
cout << "Enter ID: " << endl;
cin >> records[count].id;
cout << "Enter GPA: " << endl;
cin >> records[count].gpa;
count++;
}

//Displaying Records
void display(Student* records, int count)
{
if (count == 0)
{
cout << "No records to show" << endl;
return;
}
for (int i = 0; i < count; i++)
{
cout << "Student " << i + 1 << endl;
cout << "Name: " << records[i].name << endl;
cout << "ID: " << records[i].id << endl;
cout << "GPA: " << records[i].gpa << endl;
cout << endl;
}
}

//Updating GPA
void updateGPA(Student* records, int count)
{
int id, gpa;
cout << "Enter ID of Student whose GPA you want to update:" << endl;
cin >> id;
for (int i = 0; i < count; i++)
{
if (records[i].id == id)
{
cout << "Enter new GPA:" << endl;
cin>> records[i].gpa;
}
}
}

//Displaying Highest GPA


void displayHighestGPA(Student* records, int count)
{
if (count == 0)
{
cout << "No Records to Show" << endl;
return;
}
float highest = records[0].gpa;
int index = 0;
for (int i = 0; i < count; i++)
{
if (records[i].gpa > highest)
{
highest = records[i].gpa;
index = i;
}
}
cout << "Student " << index + 1 << endl;
cout << "Name: " << records[index].name << endl;
cout << "ID: " << records[index].id << endl;
cout << "GPA: " << records[index].gpa << endl;
cout << endl;
}

int main()
{
Student* records = NULL;
int choice, exit = 0, numStudents, count = 0;
cout << "Enter Number of Students:" << endl;
cin >> numStudents;
while (1)
{
cout << "Enter Choice:" << endl;
cout << "1.Initialize Records" << endl;
cout << "2.Add Record" << endl;
cout << "3.Update GPA" << endl;
cout << "4.Display All Records" << endl;
cout << "5.Display Highest GPA" << endl;
cout << "6.Exit" << endl;
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
initializeRecords(records, numStudents);
cout << "Records Initialized" << endl;
break;
case 2:
addRecord(records, count, numStudents);
break;
case 3:
updateGPA(records, count);
break;
case 4:
display(records, count);
break;
case 5:
displayHighestGPA(records, count);
break;
case 6:
exit = -1;
break;
}
if (exit == -1)
break;
cout << endl;
}
return 0;
}

You might also like