0% found this document useful (0 votes)
18 views1 page

Dsa Lab Task

Uploaded by

aligujar7800
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)
18 views1 page

Dsa Lab Task

Uploaded by

aligujar7800
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

Array Operations

Objectives:
Reinforce introduction to data structures, array operations, and complexity from Lectures 1-9. 10
practical implementation questions based on the array operations covered in Lab 03 (array
declaration, initialization, traversal, insertion, deletion, searching, sorting, and reversing).

1. Array Declaration and Initialization: Write a C++ program that declares a static array of
size 10 and initializes it with even numbers from 2 to 20. Then, dynamically allocate another
array of the same size and initialize it with odd numbers from 1 to 19. Print both arrays and
ensure proper memory deallocation for the dynamic array.
2. Traversal: Modify the traversal code to print only the elements at even indices of the array
(e.g., for arr[] = {10, 20, 30, 40, 50}, print 10, 30, 50). Ensure the program calculates the
array size dynamically using sizeof.
3. Insertion: Write a C++ program that allows the user to input an array of 5 integers, a value
to insert, and the position for insertion. Implement the insertion operation and print the
updated array. Add a check to ensure the insertion position is valid (within the array
bounds).
4. Deletion: Modify the deletion code to allow the user to input the index of the element to
delete. Add error handling to check if the provided index is valid (i.e., within the range of 0
to n-1). If invalid, display an error message; otherwise, perform the deletion and print the
updated array.
5. Searching (Linear Search): Extend the linear search code to find all occurrences of a given
key in the array instead of stopping at the first match. For example, if the array is {5, 10, 15,
10, 25} and the key is 10, print the indices 1 and 3. If the key is not found, display an
appropriate message.
6. Sorting (Bubble Sort): Modify the bubble sort code to sort the array in descending order
instead of ascending order. Test the program with the array {40, 10, 30, 20, 50} and print
the sorted array.
7. Reversing: Rewrite the reversing code to reverse the array in-place (i.e., modify the original
array instead of just printing it in reverse order). For example, if the array is {10, 20, 30, 40,
50}, the modified array should become {50, 40, 30, 20, 10}. Print the reversed array.
8. Combined Operations: Write a C++ program that combines insertion and deletion. Start
with an array {10, 20, 30, 40, 50}. Allow the user to insert a value at a specified index and
then delete an element from another specified index. Print the array after each operation.
Ensure the array size does not exceed a maximum capacity of 10.
9. Searching and Sorting: Write a C++ program that first sorts an array of 6 user-input
integers using bubble sort and then performs a linear search to find a user-specified element
in the sorted array. Print the sorted array and the index of the found element (or a "not
found" message).
10. Comprehensive Array Operations: Create a menu-driven C++ program that allows the
user to perform the following operations on an array of maximum size 10: (1) Insert an
element, (2) Delete an element, (3) Search for an element, (4) Sort the array, (5) Reverse the
array, and (6) Traverse the array. The program should continue running until the user
chooses to exit. Ensure proper error handling for invalid inputs (e.g., invalid indices or full
array).

You might also like