Week 1 programs for performing read, write, search, insertion and deletion operations on arrays
#include <stdio.h>
int A[10];
int N, i, lb = 0, ub;
void writeArray() {
printf("\nWriting\n");
printf("Enter the number of elements you wish to have in the array (maximum 10): ");
scanf("%d", &N);
if (N <= 10) {
ub = N-1;
for (i = 0; i <= ub; i++) {
printf("Enter element %d: ", (i+1));
scanf("%d", &A[i]);
}
printf("\nArray written\n\n");
}
else {
printf("Desired length exceeded 10. Try again.");
}
}
void readArray() {
printf("\nReading\n");
if (ub > 0) {
printf("Array: ");
for (i = 0; i <= ub; i++) {
printf("%d\t", A[i]);
}
printf("\nArray printed\n\n");
}
else {
printf("\nEmpty array\n\n");
}
}
void insertElement() {
if (ub < 9) {
printf("\nInsert an element\n");
int input;
printf("Enter 1 to insert element at the beginning of the array\nEnter 2 to insert element in the middle of the array\nEnter 3 to insert element at the end of the array\n\n");
printf("Enter command: ");
scanf("%d", &input);
int insert;
switch(input) {
case(1):
printf("Inserting element at the start\n");
printf("Enter the element you wish to insert: ");
scanf("%d", &insert);
for (i = (ub+1); i > 0; i--) {
A[i] = A[i-1];
}
A[0] = insert;
break;
case(2):
printf("Inserting element in the middle\n");
printf("Enter the element you wish to insert: ");
scanf("%d", &insert);
int index;
printf("Enter the index where you wish to insert: ");
scanf("%d", &index);
if (lb <= index <= ub) {
for (i = (ub+1); i > index; i--) {
A[i] = A[i-1];
}
A[index] = insert;
}
else {
printf("Index out out range");
}
break;
case(3):
printf("Inserting element at the end\n");
printf("Enter the element you wish to insert: ");
scanf("%d", &insert);
A[ub+1] = insert;
break;
}
ub += 1;
printf("\nElement inserted\n\n");
}
else {
printf("\nThe array is already full, kindly consider a deletion operation before inserting another element.\n");
}
}
void deleteElement() {
printf("\nDelete an element\n");
int input;
printf("Enter 1 to delete element from the beginning of the array\nEnter 2 to delete element from the middle of the array\nEnter 3 to delete element from the end of the array\n\n");
printf("Enter command: ");
scanf("%d", &input);
switch(input) {
case(1):
printf("Deleting element from the start\n");
for (i = 0; i < ub; i++) {
A[i] = A[i+1];
}
break;
case(2):
printf("Deleting element fromt the middle\n");
int index;
printf("Enter the index which you wish to delete: ");
scanf("%d", &index);
if (lb <= index <= ub) {
for (i = index; i < ub; i++) {
A[i] = A[i+1];
}
}
else {
printf("Index out out range");
}
break;
case(3):
printf("Deleting element from the end\n");
break;
}
ub -= 1;
printf("\nElement deleted\n\n");
}
void searchArray() {
printf("\nSearching for an element\n");
int element, index = -1;
printf("Enter the element you wish to find: ");
scanf("%d", &element);
for (i = 0; i <= ub; i++) {
if (A[i] == element) {
index = i;
break;
}
}
if (index != -1) {
printf("\nElement found at the index: %d\n", index);
}
else {
printf("\nElement not found in the array\n\n");
}
}
void userChoice() {
int input;
printf("Enter 1 to write the array\nEnter 2 to read the array\nEnter 3 to insert an element\nEnter 4 to delete an element\nEnter 5 to search for an element\nEnter 6 to terminate the program\n\n");
printf("Enter command: ");
scanf("%d", &input);
switch(input) {
case(1):
printf("Writing the array\n");
writeArray();
userChoice();
break;
case(2):
printf("Reading the array\n");
readArray();
userChoice();
break;
case(3):
printf("Inserting an element\n");
insertElement();
userChoice();
break;
case(4):
printf("Deleting an element\n");
deleteElement();
userChoice();
break;
case(5):
printf("Search for an element\n");
searchArray();
userChoice();
break;
case(6):
printf("Terminating Program\n");
break;
}
}
int main() {
printf("\n================ Array Operations ================\n\n");
userChoice();
return(0);
}
================ Array Operations ================
Enter 1 to write the array
Enter 2 to read the array
Enter 3 to insert an element
Enter 4 to delete an element
Enter 5 to search for an element
Enter 6 to terminate the program
Enter command: 1
Writing the array
Writing
Enter the number of elements you wish to have in the array (maximum 10): 4
Enter element 1: 7567
Enter element 2: 253
Enter element 3: 2342
Enter element 4: 788
Array written
Enter 1 to write the array
Enter 2 to read the array
Enter 3 to insert an element
Enter 4 to delete an element
Enter 5 to search for an element
Enter 6 to terminate the program
Enter command: 2
Reading the array
Reading
Array: 7567 253 2342 788
Array printed
Enter 1 to write the array
Enter 2 to read the array
Enter 3 to insert an element
Enter 4 to delete an element
Enter 5 to search for an element
Enter 6 to terminate the program
Enter command: