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

Data Stuctures and Algorithm Assignment 1

As

Uploaded by

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

Data Stuctures and Algorithm Assignment 1

As

Uploaded by

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

DATA STUCTURES AND ALGORITHM

ASSIGNMENT 1

Q1: 1. Creating Dynamic Arrays

Write a C++ program that dynamically allocates an array of integers


using the new operator. The

program should:

o Take the size of the array as input from the user.

o Allow the user to input values into the array.

o Print the contents of the array.

o Release the memory using the delete[] operator.

SOLUTION:

CODE:

#include <iostream>
using namespace std;

int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* arr = new int[size];

cout << "Enter " << size << " integers:" << endl;
for (int i = 0; i < size; i++) {
cout << "Element " << i + 1 << ": ";
cin >> arr[i];
}

cout << "Contents of the array: ";


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;

delete[] arr;

cout << "Memory has been released." << endl;

return 0;
}

OUTPUT:
Q2: Resizing a Dynamic Array

Implement a function in C++ to resize a dynamic array. The function


should:

o Take a pointer to the dynamic array and its current size as inputs.

o Allocate a new array of double the size.

o Copy the contents of the old array into the new array.

o Free the memory of the old array.

Demonstrate the function by dynamically creating an array, filling it


with values, resizing it,

and printing the new contents.

SOLUTION:

CODE:

#include <iostream>
using namespace std;

void ResizeArray(int*& arr, int& currentSize) {


int newSize = currentSize * 2;
int* newArr = new int[newSize];

// Initialize the new array and copy existing data


for (int i = 0; i < currentSize; i++) {
newArr[i] = arr[i];
}
for (int i = currentSize; i < newSize; i++) {
newArr[i] = 0; // Initialize remaining elements to 0
}

delete[] arr; // Free the old array memory


arr = newArr; // Update the pointer
currentSize = newSize; // Update the size
}

int main() {
int size;
cout << "Enter the initial size of the array: ";
cin >> size;

if (size <= 0) {
cerr << "Error: Size must be greater than 0." << endl;
return 1;
}

int* arr = new int[size];

cout << "Enter " << size << " integers:" << endl;
for (int i = 0; i < size; i++) {
cin >> arr[i];
}

ResizeArray(arr, size);

cout << "Array resized. New size: " << size << endl;
cout << "Contents of the resized array:" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;

delete[] arr;
return 0;
}
OUTPUT:
Q3: Dynamic Array-Based Stack

Create a simple stack implementation using a dynamic array in C++.


Your implementation should

include the following:

o Functions for push, pop, and isEmpty.

o Dynamically resize the array when the stack is full (double its size).

o A main function to test your stack implementation with sample


inputs.

SOLUTION:

CODE:

#include <iostream>
using namespace std;

class Stack {
private:
int* arr;
int capacity;
int top;

void resize() {
int newCapacity = capacity * 2;
int* newArr = new int[newCapacity];
for (int i = 0; i < capacity; i++) {
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
capacity = newCapacity;
}

public:
Stack(int size = 10) {
capacity = size;
arr = new int[capacity];
top = -1;
}

~Stack() {
delete[] arr;
}

void push(int value) {


if (top + 1 == capacity) {
resize();
}
arr[++top] = value;
}

int pop() {
if (isEmpty()) {
cerr << "Error: Stack underflow." << endl;
return -1;
}
return arr[top--];
}

bool isEmpty() const {


return top == -1;
}

void printStack() const {


if (isEmpty()) {
cout << "Stack is empty." << endl;
return;
}
cout << "Stack contents: ";
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};

int main() {
Stack stack;

stack.push(10);
stack.push(20);
stack.push(30);
cout << "Stack after pushing 10, 20, 30:" << endl;
stack.printStack();

stack.push(40);
stack.push(50);

cout << "Stack after pushing 40, 50:" << endl;


stack.printStack();

cout << "Popping element: " << stack.pop() << endl;


cout << "Popping element: " << stack.pop() << endl;

cout << "Stack after popping two elements:" << endl;


stack.printStack();

return 0;
}
OUTPUT:

You might also like