Dsa Lab 1
Dsa Lab 1
UNIVERSITY OF SARGODHA
Lab 1 Manual
Introduction to Arrays
Marks obtained
Instructor Signature
CLO-1 Illustrate understanding of key concepts of Data structures
and Algorithms using Dev C++ Platform
CLO-2 Design a programming application by applying concepts of
Data Structures and algorithms leading to the solution of a moderate
scale-programming problem.
CLO-3 Write lab notes, effective communication and the analysis of
the given problem to perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or
individually with respect to the contribution.
OBJECTIVE:
Design, Develop and Implement a menu driven program in C for the following Array
operations
a. Creating Array of N Integer elements.
b. Display of Array elements with suitable headings.
c. Inserting an element (ELEM) at a given valid position (POS).
d. Deleting an element at a given valid position (POS).
e. Exit.
Support the program with functions for each of the above operations
Algorithm:
Step 1: Start.
Step 2: Read N value.
Step 3: Read Array of N integer elements
Step 4: Print array of N integer elements.
Step 5: Insert an element at given valid position in an array.
Step 6: Delete an element at given valid position from an array.
Step 7: Stop.
C++ Program Code:
#include <iostream>
using namespace std;
int arr[MAX_SIZE];
int size = 10;
void displayArray() {
if (size == 10) {
cout << "Array is empty" << endl;
return;
}
cout << "Array Elements:" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void insertElement(int elem, int pos) {
if (pos < 0 || pos > size) {
cout << "Invalid position for insertion." << endl;
return;
}
if (size == MAX_SIZE) {
cout << "Array is full. Cannot insert more elements." << endl;
return;
}
for (int i = size; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = elem;
size++;
cout << "Element inserted successfully." << endl;
}
void deleteElement(int pos) {
if (pos < 0 || pos >= size) {
cout << "Invalid position for deletion." << endl;
return;
}
for (int i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
cout << "Element deleted successfully." << endl;
}
int main() {
int choice, elem, pos;
do {
cout << "\nMenu:\n";
cout << "1. Display Array\n";
cout << "2. Insert Element\n";
cout << "3. Delete Element\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
displayArray();
break;
case 2:
cout << "Enter the element to insert: ";
cin >> elem;
cout << "Enter the position to insert: ";
cin >> pos;
insertElement(elem, pos);
break;
case 3:
cout << "Enter the position to delete: ";
cin >> pos;
deleteElement(pos);
break;
case 4:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);
return 0;
}
Output:
1. Menu:
2. Display:
3. Insert:
4. Delete:
5. Exit:
Assessment Rubric for Lab 1
Method of Evaluation: Lab report.
Outcomes Assessed:
CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using Dev
C++ Platform.
CLO-2 Design a programming application by applying concepts of Data Structures and
algorithms leading to the solution of a moderate scale-programming problem.
CLO-3 Write lab notes, effective communication and analysis of the given problem to perform
in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually with respect to the
contribution.
Total