ARRAY OPERATIONS USING DYNAMIC MEMORY ALLOCATION.

Exported from Notepad++
#include <stdio.h> #include <stdlib.h> int *arrayPtr; int nElements; int * allocateMemory ( int n ) { int *temp; /*Allocate enough memory*/ temp = ( int * ) malloc ( n * sizeof ( int ) ); /*Check for unsuccessful allocation*/ if ( temp == NULL ) { printf ( "\n\tERROR: Malloc failed to allocate memory\n\n" ); getchar(); exit( EXIT_FAILURE ); } printf ( "\n\n\tAllocation Successful\n" ); return temp; } int * reAllocateMemory ( int *ptr, int n ) { int *temp; /*Modify previously allocated memory*/ temp = ( int * ) realloc ( ptr, n * sizeof ( int ) ); /*Check for unsuccessful allocation*/ if ( temp == NULL ) { printf ( "\n\tERROR: Realloc failed to allocate memory\n\n" ); getchar(); exit( EXIT_FAILURE ); } printf ( "\n\n\tReallocation Successful\n" ); return temp; } void readArrayElements ( void ) { int i; arrayPtr = allocateMemory ( nElements ); printf ( "\n\n\tEnter the array elements one by one\n\n\t" ); for ( i = 1; i <= nElements; ++ i ) { scanf ( "%d", ( arrayPtr + i ) ); printf ( "\n\t" ); } printf ( "\n\n\tArray elements read successfully\n\n" ); } void displayElements ( void ) { int i; if ( nElements == 0 ) { printf ( "\n\n\tArray List is Empty\n" ); return; } printf ( "\n\tThe array elements are..\n\n" ); printf ( "\t(index : value)\n\n" ); for ( i = 1; i <= nElements; ++ i ) { printf ( "\t [%d]\t:%5d\n\n", i, arrayPtr[i] ); } printf ( "\n" ); } void insertAnElement ( int element, int position ) { int counter; /*modify the previously allocated memory blocks*/ arrayPtr = reAllocateMemory ( arrayPtr, ( nElements + 1 ) ); /*Initialize counter*/ counter = nElements; while ( counter >= position ) { /*Move elements downwards*/ arrayPtr[counter + 1] = arrayPtr[counter]; counter = counter - 1; } arrayPtr[position] = element; /*Reset the number of elements in Array*/ nElements = nElements + 1; printf ( "\n\tItem Inserted successfully\n" ); } void deleteAnElement ( int position ) { int i; printf ( "\n\tDeleted Element: %d\n\n", arrayPtr[position] ); /* if only one element, reset array size and free memory */ if ( nElements == 1 ) { nElements = 0; free ( arrayPtr ); return; } for ( i = position; i < nElements; ++ i ) { /*Move elements upward*/ arrayPtr[i] = arrayPtr[i + 1]; } /*modify the previously allocated memory blocks*/ arrayPtr = reAllocateMemory ( arrayPtr, ( nElements - 1 ) ); /*Reset the number of elements in Array*/ nElements = nElements - 1; } int main ( void ) { int element, position; int ch; do{ //clrscr(); //system("clear"); printf ("\n\t\t\t ARRAY DEMONSTRATION\n\n" ); printf ("\n\t\t1. Create Array\t\t2. Display Array\n" ); printf ("\n\t\t3. Insert Element\t4. Delete Element\n" ); printf ("\n\t\t\t\t5. Exit\n\n" ); printf ( "\n\tEnter your choice: ?\b" ); scanf ( "%d", &ch ); switch ( ch ) { case 1: if ( nElements == 0 ) { printf ( "\n\n\tEnter the array size: ?\b" ); scanf ( "%d", &nElements ); free(arrayPtr); readArrayElements ( ); } else { printf ( "\n\n\tArray List already exist\n\n" ); } break; case 2: displayElements ( ); break; case 3: if ( nElements == 0 ) { printf ( "\n\n\tInsufficient Memory\n" ); printf ( "\n\tTry option 1 to Allocate Memory\n" ); } else { displayElements ( ); printf ( "\n\t Enter the valid position: ?\b" ); scanf ( "%d", &position ); if ( (position < 1) || (position > nElements) ) { printf ( "\n\t Invalid Position\n" ); } else { printf ( "\n\tEnter an Element to insert: ?\b" ); scanf ( "%d", &element ); insertAnElement ( element, position ); } } break; case 4: if ( nElements == 0 ) { printf ( "\n\n\tInsufficient Memory\n" ); printf ( "\n\tTry option 1 to Allocate Memory\n" ); } else { displayElements ( ); printf ( "\n\tEnter the valid position: ?\b" ); scanf ( "%d", &position ); if ( (position < 1) || (position > nElements) ) { printf ( "\n\tInvalid Position\n" ); } else { deleteAnElement ( position ); } } break; case 5: return 0; default: printf ( "\n\tInvalid Option\n\n" ); } fflush(stdin); getchar(); //getch(); }while ( 1 ); } /* OUTPUT: RUN - 1 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 1 Enter the array size: 5 Allocation Successful Enter the array elements one by one 10 20 30 40 50 Array elements read 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] : 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: 300 Reallocation Successful Item 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] : 300 [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] : 300 [4] : 30 [5] : 40 [6] : 50 Enter the valid position: 6 Deleted Element: 50 Reallocation Successful 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] : 300 [4] : 30 [5] : 40 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit OUTPUT: RUN - 2 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 1 Enter the array size: 1 Allocation Successful Enter the array elements one by one 10 Array elements read 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 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 Enter the valid position: 2 Invalid Position 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 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: 3 The array elements are.. (index : value) [1] : 10 Enter the valid position: 1 Enter an Element to insert: 100 Reallocation Successful Item 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] : 100 [2] : 10 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] : 100 [2] : 10 Enter the valid position: 3 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] : 100 [2] : 10 Enter the valid position: 2 Deleted Element: 10 Reallocation Successful 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] : 100 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] : 100 Enter the valid position: 1 Deleted Element: 100 ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 2 Array List is Empty ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 3 Insufficient Memory Try option 1 to Allocate Memory ARRAY DEMONSTRATION 1. Create Array 2. Display Array 3. Insert Element 4. Delete Element 5. Exit Enter your choice: 4 Insufficient Memory Try option 1 to Allocate Memory 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...