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

Code Report

Uploaded by

Zulaikha Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Code Report

Uploaded by

Zulaikha Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Question#1

Using Dynamic array, make a list in which you have to use all the
list function which can be made for example.
createList( )
copy( )
clear( )
insert( ) //from start //from end //in desired position
remove( ) //from start //from end //in desired position
get( )
update( )
find( )
length( )
start( )
tail( )
next( )
back( )
end( )

Solution:

Code Report
Overall Assessment:
The provided C++ code implements a dynamic array class with various methods for
manipulating the array. The code is well-structured, readable, and follows good coding practices.

Strengths:
1. Modularity: The code is divided into smaller functions, each performing a specific task,
making it easier to understand and maintain.
2. Error Handling: The code checks for invalid inputs and handles errors gracefully.
3. Memory Management: Dynamic memory allocation and deallocation are properly
handled to prevent memory leaks.

List Function Descriptions:


1. Create List
Purpose: Initializes a new dynamic array.
Function: createList()
Description: Resets the array to its initial state, deleting any existing data.
2. Enter Data to List
Purpose: Allows users to input data into the dynamic array.
Function: enterData()
Description: Prompts the user to enter the number of elements and then input each element.
3. Display List
Purpose: Prints the contents of the dynamic array.
Function: displayList()
Description: Displays all elements in the array.
4. Copy List
Purpose: Creates a copy of the dynamic array.
Function: copy()
Description: Allocates new memory, copies the existing data, and prints the copied list.
5. Clear List
Purpose: Removes all elements from the dynamic array.
Function: clear()
Description: Resets the size to 0, effectively clearing the array.
6. Delete List
Purpose: Deletes the dynamic array and frees memory.
Function: deleteList()
Description: Deallocates memory, resets size and capacity, and creates a new array.
7. Insert at Start
Purpose: Adds an element at the beginning of the dynamic array.
Function: insertStart(int value)
Description: Shifts existing elements to the right and inserts the new value at the start.
8. Insert at End
Purpose: Adds an element at the end of the dynamic array.
Function: insertEnd(int value)
Description: Inserts the new value at the end of the array.
9. Insert at Position
Purpose: Inserts an element at a specified position in the dynamic array.
Function: insertPosition(int index, int value)
Description: Shifts elements to the right and inserts the new value at the specified position.
10. Remove from Start
Purpose: Removes the first element from the dynamic array.
Function: removeStart()
Description: Shifts elements to the left and decreases the size.
11. Remove from End
Purpose: Removes the last element from the dynamic array.
Function: removeEnd()
Description: Decreases the size.
12. Remove from Position
Purpose: Removes an element at a specified position from the dynamic array.
Function: removePosition(int index)
Description: Shifts elements to the left and decreases the size.
13. Get Element at Position
Purpose: Retrieves an element at a specified position from the dynamic array.
Function: get(int index)
Description: Returns the element at the specified position.
14. Update Element at Position
Purpose: Updates an element at a specified position in the dynamic array.
Function: update(int index, int value)
Description: Replaces the existing value with the new value.
15. Find Element
Purpose: Searches for an element in the dynamic array.
Function: find(int value)
Description: Returns the position of the element if found.
16. Get List Length
Purpose: Returns the number of elements in the dynamic array.
Function: length()
Description: Returns the current size.
17. Get Start Element
Function: start() const
Purpose: Retrieves the first element of the dynamic array.
Description: Checks if the array is not empty, then displays the first element.
18. Get End Element
Function: tail() const
Purpose: Retrieves the last element of the dynamic array.
Description: Checks if the array is not empty, then displays the last element.
19. Get Next Element
Function: next(int index) const
Purpose: Retrieves the element next to a specified index in the dynamic array.
Description: Checks if the index is valid and not the last element, then displays the next
element.
20. Get Previous Element
Function: back(int index) const
Purpose: Retrieves the element previous to a specified index in the dynamic array.
Description: Checks if the index is valid and not the first element, then displays the previous
element.
21. End Program
- This option does not correspond to a function within the DynamicArray class. Instead, it is an
option in the menu that terminates the program when selected.

Code
#include <iostream>
using namespace std;

class DynamicArray {
private:
int* arr; // Pointer to the array
int size; // Current number of elements in the array
int capacity; // Maximum number of elements the array can hold

void resize(int newCapacity) {


int* newArr
= new int[newCapacity];
for (int i = 0; i < size; i++) {
newArr[i] = arr[i]; // Copy old elements to new array
}
delete[] arr; // Free the old array memory
arr = newArr; // Point to the new array
capacity = newCapacity; // Update capacity
}

public:
DynamicArray() : size(0), capacity(1) {
arr = new int[capacity]; // Initialize the array with capacity of 1
}

~DynamicArray() {
delete[] arr; // Free memory on destruction
}

void createList() {
delete[] arr; // Delete old array
size = 0; // Reset size
capacity = 1; // Reset capacity
arr = new int[capacity]; // Allocate new array
cout << "New list created.\n";
}

void enterData() {
cout << "Enter number of elements: ";
int numElements;
cin >> numElements;

if (numElements > capacity) {


resize(numElements); // Resize if the number of elements exceeds current capacity
}

cout << "Enter " << numElements << " numbers:\n";


for (int i = 0; i < numElements; ++i) {
cout << (i + 1) << ": ";
cin >> arr[i]; // Store input directly in the dynamic array
}
size = numElements; // Update size
}

void displayList() const {


cout << "The list contains: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " "; // Print each element
}
cout << endl;
}

void copy() const {


int* copiedList = new int[size];
for (int i = 0; i < size; i++) {
copiedList[i] = arr[i]; // Copy elements to new array
}
cout << "Copied List: ";
for (int i = 0; i < size; i++) {
cout << copiedList[i] << " "; // Print copied elements
}
cout << endl;
delete[] copiedList; // Free the copied list memory
}

void clear() {
size = 0; // Reset size, effectively clearing the list
cout << "List cleared.\n";
}

void deleteList() {
delete[] arr; // Free the array memory
size = 0; // Reset size
capacity = 1; // Reset capacity
arr = new int[capacity]; // Allocate a new array with capacity of 1
cout << "List deleted.\n";
}

void insertStart(int value) {


if (size == capacity) {
resize(2 * capacity); // Resize if full
}
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1]; // Shift elements to the right
}
arr[0] = value; // Insert at start
size++; // Update size
cout << "Inserted " << value << " at the start.\n";
}

void insertEnd(int value) {


if (size == capacity) {
resize(2 * capacity); // Resize if full
}
arr[size] = value; // Insert at end
size++; // Update size
cout << "Inserted " << value << " at the end.\n";
}

void insertPosition(int index, int value) {


if (index < 0 || index > size) {
cout << "Invalid position.\n";
return;
}
if (size == capacity) {
resize(2 * capacity); // Resize if full
}
for (int i = size; i > index; i--) {
arr[i] = arr[i - 1]; // Shift elements to the right
}
arr[index] = value; // Insert at specified position
size++; // Update size
cout << "Inserted " << value << " at position " << index << ".\n";
}

void removeStart() {
if (size == 0) {
cout << "List is empty.\n";
return;
}
for (int i = 0; i < size - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements to the left
}
size--; // Update size
cout << "Removed element from the start.\n";
}

void removeEnd() {
if (size == 0) {
cout << "List is empty.\n";
return;
}
size--; // Update size
cout << "Removed element from the end.\n";
}

void removePosition(int index) {


if (index < 0 || index >= size) {
cout << "Invalid position.\n";
return;
}
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements to the left
}
size--; // Update size
cout << "Removed element from position " << index << ".\n";
}

void get(int index) const {


if (index < 0 || index >= size) {
cout << "Invalid position.\n";
return;
}
cout << "Element at position " << index << " is " << arr[index] << ".\n";
}

void update(int index, int value) {


if (index < 0 || index >= size) {
cout << "Invalid position.\n";
return;
}
arr[index] = value; // Update the value at specified index
cout << "Updated position " << index << " to " << value << ".\n";
}

void find(int value) const {


for (int i = 0; i < size; i++) {
if (arr[i] == value) {
cout << "Element " << value << " found at position " << i << ".\n";
return;
}
}
cout << "Element " << value << " not found.\n";
}

void length() const {


cout << "Length of the list: " << size << "\n";
}

void start() const {


if (size > 0) {
cout << "First element: " << arr[0] << "\n";
} else {
cout << "List is empty.\n";
}
}

void tail() const {


if (size > 0) {
cout << "Last element: " << arr[size - 1] << "\n";
} else {
cout << "List is empty.\n";
}
}

void next(int index) const {


if (index < 0 || index >= size - 1) {
cout << "No next element or invalid index.\n";
return;
}
cout << "Next element after index " << index << " is " << arr[index + 1] << ".\n";
}

void back(int index) const {


if (index <= 0 || index >= size) {
cout << "No previous element or invalid index.\n";
return;
}
cout << "Previous element before index " << index << " is " << arr[index - 1] << ".\n";
}

void end() const {


cout << "End of list reached.\n";
}
};
void menu() {
DynamicArray array;
int choice, value, index;

while (true) {
cout << "\n--- Menu ---\n";
cout << "1. Create List\n";
cout << "2. Enter Data to List\n";
cout << "3. Display List\n"; // Corrected here
cout << "4. Copy List\n";
cout << "5. Clear List\n";
cout << "6. Delete List\n";
cout << "7. Insert at Start\n";
cout << "8. Insert at End\n";
cout << "9. Insert at Position\n";
cout << "10. Remove from Start\n";
cout << "11. Remove from End\n";
cout << "12. Remove from Position\n";
cout << "13. Get Element at Position\n";
cout << "14. Update Element at Position\n";
cout << "15. Find Element\n";
cout << "16. Get List Length\n";
cout << "17. Get Start Element\n";
cout << "18. Get End Element\n";
cout << "19. Get Next Element\n";
cout << "20. Get Previous Element\n";
cout << "21. End Program\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: array.createList(); break;
case 2: array.enterData(); break;
case 3: array.displayList(); break; // Corrected here
case 4: array.copy(); break;
case 5: array.clear(); break;
case 6: array.deleteList(); break;
case 7:
cout << "Enter value to insert at start: ";
cin >> value;
array.insertStart(value);
break;
case 8:
cout << "Enter value to insert at end: ";
cin >> value;
array.insertEnd(value);
break;
case 9:
cout << "Enter index and value to insert: ";
cin >> index >> value;
array.insertPosition(index, value);
break;
case 10: array.removeStart(); break;
case 11: array.removeEnd(); break;
case 12:
cout << "Enter index to remove from: ";
cin >> index;
array.removePosition(index);
break;
case 13:
cout << "Enter index to get element: ";
cin >> index;
array.get(index);
break;
case 14:
cout << "Enter index and new value: ";
cin >> index >> value;
array.update(index, value);
break;
case 15:
cout << "Enter value to find: ";
cin >> value;
array.find(value);
break;
case 16: array.length(); break;
case 17: array.start(); break;
case 18: array.tail(); break;
case 19:
cout << "Enter index to get next element: ";
cin >> index;
array.next(index);
break;
case 20:
cout << "Enter index to get previous element: ";
cin >> index;
array.back(index);
break;
case 21:
cout << "Program Ended.\n";
return;
default:
cout << "Invalid choice. Try again.\n";
}
}
}

int main() {
menu();
return 0;
}

You might also like