C Array Manipulation: Insertion, Deletion, and Sorting
This document presents C code for manipulating arrays, including insertion, deletion, sorting, and display functionalities.
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define MAX 5
int enter();
void insert(int item, int vector[], int pos);
int remove(int deletion, int vector[], int pos);
void modify(int modif, int vector[], int pos);
void display(int array[], int position, int available);
void sort(int array[], int pos);
int menu();
int main() {
int array[MAX], available, pos = 0;
int opt;
do {
available = MAX - pos;
opt = menu();
switch (opt) {
case 1:
printf("\nDISPONIBLE = %d\n", available);
if (available > 0) {
insert(enter(), array, pos);
pos++;
sort(array, pos);
} else {
printf("\nNO SPACE\n");
}
available = MAX - pos;
printf("\nDISPONIBLE = %d\n", available);
display(array, pos, available);
break;
case 2:
printf("\nDISPONIBLE = %d\n", available);
if (available != MAX) {
pos = remove(enter(), array, pos);
sort(array, pos);
} else {
printf("\nThe array is empty, cannot delete");
}
available = MAX - pos;
printf("\nDISPONIBLE = %d\n", available);
display(array, pos, available);
break;
case 3:
if (available != MAX) {
modify(enter(), array, pos);
sort(array, pos);
} else {
printf("The vector is empty\n");
}
available = MAX - pos;
printf("\nDISPONIBLE = %d\n", available);
display(array, pos, available);
break;
case 4:
system("cls");
printf("\nDISPONIBLE = %d\n", available);
display(array, pos, available);
break;
}
getch();
} while (opt != 0);
return 0;
}
int menu() {
int opt;
do {
printf("\t\tMENU\n");
printf("1 Insert\n");
printf("2 Remove\n");
printf("3 Change\n");
printf("4 Show Elements\n");
printf("0 Exit\n");
printf("digit option:");
scanf("%d", &opt);
} while (opt < 0 || opt > 4);
return opt;
}
void display(int array[], int position, int available) {
int i;
if (available != MAX) {
printf("\nMostrando vector: \n\n\t");
for (i = 0; i < position; i++) {
printf("|%2d", array[i]);
}
printf("|\n");
} else {
printf("\nvector EMPTY\n");
}
}
int enter() {
int item;
printf("Enter element:");
scanf("%d", &item);
return item;
}
void insert(int item, int vector[], int pos) {
vector[pos] = item;
}
int remove(int deletion, int vector[], int pos) {
int posElim, i;
posElim = -1;
for (i = 0; i < pos; i++) {
if (vector[i] == deletion) {
posElim = i;
break;
}
}
if (posElim > -1) {
printf("\nElement found at position: %d\n", posElim);
for (i = posElim; i < pos - 1; i++) {
vector[i] = vector[i + 1];
}
pos--;
} else {
printf("\nElement not found\n");
}
return pos;
}
void modify(int modified, int vector[], int pos) {
int postmodern, i, replacement;
postmodern = -1;
for (i = 0; i < pos; i++) {
if (vector[i] == modified) {
postmodern = i;
break;
}
}
if (postmodern > -1) {
printf("Enter replacement value:");
scanf("%d", &replacement);
vector[postmodern] = replacement;
printf("Value successfully replaced\n");
} else {
printf("Value is not searched\n");
}
}
void sort(int received[MAX], int pos) {
int i, j, aux;
for (i = 0; i < pos - 1; i++) {
for (j = i + 1; j < pos; j++) {
if (received[i] > received[j]) {
aux = received[i];
received[i] = received[j];
received[j] = aux;
}
}
}
}