ARRAY OPERATIONS USING A SIMPLE ARRAY.

Exported from Notepad++
#include <stdio.h> #include <stdlib.h> //#include <conio.h> int linearArray[100]; int noOfElements = 0; int choice (void) { int ch; //system("clear"); printf ("\n\t\t\t ARRAY DEMONSTRATION\n\n" ); printf ("\n\t\t 1. Create Array\t 2. Display Array\n" ); printf ("\n\t\t 3. Insert Element\t 4. Delete Element\n" ); printf ("\n\t\t\t\t 5. Exit\n\n" ); printf ("\n\t Enter your choice: ?\b"); scanf ("%d", &ch); return ch; } void createArray (void) { int i; printf ("\n\t Enter the size of the array: ?\b"); scanf ("%d", &noOfElements); printf ("\n\t Enter the array elements one by one\n\t"); for (i = 1; i <= noOfElements; ++ i) { scanf ("%d", &linearArray[i]); printf ("\n\t"); } } void displayElements (void) { int i; printf ("\n\t The array elements are..\n\n"); printf ("\t (index : value)\n\n"); for (i = 1; i <= noOfElements; ++ i) { printf ("\t [%d]\t:%5d\n\n", i, linearArray[i]); } printf ("\n"); } int validatePos (int position) { return ((position <= 0) || (position > noOfElements)); } void insertAnElement (int position, int element) { int counter; /*Initialize counter*/ counter = noOfElements; while (counter >= position) { /*Move elements downwards*/ linearArray[counter + 1] = linearArray[counter]; counter = counter - 1; } linearArray[position] = element; /*Reset the number of elements in Array*/ noOfElements = noOfElements + 1; printf ("\n\t Element Inserted successfully\n"); } void deleteAnElement (int position) { int i; printf ("\n\t Deleted Element: %d\n\n", linearArray[position]); for (i = position; i < noOfElements; ++ i) { /*Move elements upward*/ linearArray[i] = linearArray[i + 1]; } /*Reset the number of elements in Array*/ noOfElements = noOfElements - 1; } int main (void) { int element; int position; do{ switch (choice() ) { case 1: createArray(); break; case 2: if (noOfElements == 0) { printf ( "\n\n\tArray List is Empty\n" ); } else { displayElements(); } break; case 3: if (noOfElements == 0) { printf ("\n\n\tArray List is Empty\n"); } else { displayElements(); printf ("\n\tEnter the valid position: ?\b"); scanf ("%d", &position); if (validatePos(position)) { printf ("\n\tInvalid Position\n"); } else { printf("\n\tEnter an element to insert: ?\b"); scanf ("%d", &element); insertAnElement(position, element); } } break; case 4: if(noOfElements == 0) { printf ("\n\n\tArray List is Empty\n"); } else { displayElements(); printf ("\n\tEnter the valid position: ?\b"); scanf ("%d", &position); if (validatePos(position)) { printf ("\n\tInvalid Position\n"); } else { deleteAnElement (position); } } break; case 5: return 0; default: printf ("\n\tInvalid Option\n\n"); } fflush(stdin); getchar(); getchar(); //getch(); }while(1); } /* ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 1 Enter the size of the array: 5 Enter the array elements one by one 10 20 30 40 50 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 2 The array elements are.. (index : value) [1] : 10 [2] : 20 [3] : 30 [4] : 40 [5] : 50 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 3 The array elements are.. (index : value) [1] : 10 [2] : 20 [3] : 30 [4] : 40 [5] : 50 Enter the valid position: 3 Enter an element to insert: 100 Element Inserted successfully ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 2 The array elements are.. (index : value) [1] : 10 [2] : 20 [3] : 100 [4] : 30 [5] : 40 [6] : 50 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 4 The array elements are.. (index : value) [1] : 10 [2] : 20 [3] : 100 [4] : 30 [5] : 40 [6] : 50 Enter the valid position: 6 Deleted Element: 50 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 2 The array elements are.. (index : value) [1] : 10 [2] : 20 [3] : 100 [4] : 30 [5] : 40 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 3 The array elements are.. (index : value) [1] : 10 [2] : 20 [3] : 100 [4] : 30 [5] : 40 Enter the valid position: 6 Invalid Position ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 4 The array elements are.. (index : value) [1] : 10 [2] : 20 [3] : 100 [4] : 30 [5] : 40 Enter the valid position: 0 Invalid Position ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 5 */

No comments:

Post a Comment

Online Certifications

Python for Beginners Offered by: Christian Drumm, Stephan Jacobs Course dates: 2022-04-05 to 2022-06-01 Topics Python Fundamentals Lists and...