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

Data structure Lab 1 program

The document contains two C programs: the first computes the minimum and maximum values of a given array, while the second is a menu-based program that allows users to perform various array operations such as printing, inserting, and deleting elements. The first program initializes an array and iterates through it to find the min and max values, while the second program includes functions for printing the array, inserting an element at a specified position, and deleting an element from a specified position. Both programs demonstrate fundamental array manipulation techniques in C.

Uploaded by

pooja.malhotra
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)
4 views

Data structure Lab 1 program

The document contains two C programs: the first computes the minimum and maximum values of a given array, while the second is a menu-based program that allows users to perform various array operations such as printing, inserting, and deleting elements. The first program initializes an array and iterates through it to find the min and max values, while the second program includes functions for printing the array, inserting an element at a specified position, and deleting an element from a specified position. Both programs demonstrate fundamental array manipulation techniques in C.

Uploaded by

pooja.malhotra
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/ 2

Data structure Lab 1 program

1. Write a program to compute minimum/maximum of a given array.

Program
#include <stdio.h>

int main() {
int arr[] = {12, 45, 3, 67, 22, 90, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int min = arr[0], max = arr[0];

for (int i = 1; i < size; i++) {


if (arr[i] < min)
min = arr[i];
if (arr[i] > max)
max = arr[i];
}

printf("Minimum: %d\n", min);


printf("Maximum: %d\n", max);

return 0;
}

2. Write a menu-based program to perform array operations: deletion of an element from the
specified position, inserting an element at the specified position, printing the array
elements.

Programs
#include <stdio.h>

void printArray(int arr[], int size) {


printf("Array elements: ");
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int insertElement(int arr[], int size, int pos, int value) {


if (pos < 0 || pos > size) {
printf("Invalid position!\n");
return size;
}
for (int i = size; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = value;
return size + 1;
}

int deleteElement(int arr[], int size, int pos) {


if (pos < 0 || pos >= size) {
printf("Invalid position!\n");
return size;
}
for (int i = pos; i < size - 1; i++)
arr[i] = arr[i + 1];
return size - 1;
}

int main() {
int arr[100] = {10, 20, 30, 40, 50};
int size = 5, choice, pos, value;

do {
printf("\nMenu:\n");
printf("1. Print Array\n");
printf("2. Insert Element\n");
printf("3. Delete Element\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printArray(arr, size);
break;
case 2:
printf("Enter position to insert (0-based index): ");
scanf("%d", &pos);
printf("Enter value to insert: ");
scanf("%d", &value);
size = insertElement(arr, size, pos, value);
break;
case 3:
printf("Enter position to delete (0-based index): ");
scanf("%d", &pos);
size = deleteElement(arr, size, pos);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice, try again.\n");
}
} while (choice != 4);

return 0;
}

You might also like