Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Coding in C Lab5.c template #include #include /* Definition of structure extendableArray */ struct extendableArray { int *arr; /* array of integers */ int size;

Coding in C

image text in transcribed

Lab5.c template

#include #include

/* Definition of structure extendableArray */

struct extendableArray { int *arr; /* array of integers */ int size; /* number of elements currently stored in the array */ int capacity;/* capacity of the currently allocated array */ };

/* Initializes the extendable array */

struct extendableArray *initArr() { struct extendableArray *a = ( struct extendableArray * ) malloc( sizeof( struct extendableArray ) ); if( !a ) { printf( "Insufficient memory!" ); exit( 1 ); } a->capacity = 4; /* initial capacity */ a->arr = ( int * ) malloc( a->capacity * sizeof( int )); if( !a->arr ) { printf( "Insufficient memory!" ); exit( 1) ; } a->size = 0; /* no element added to the array yet */ return a; }

/* Returns the size of the array */

int arrSize( struct extendableArray *a ) { return( a->size ); }

/* Returns the capacity of the array. */

int arrCapacity( struct extendableArray *a ) { return( a->capacity ); }

/* Returns true (1) if the array is empty, and false (0) otherwise. */

int isEmpty( struct extendableArray *a) { return( a->size == 0 ); }

/* Returns true (1) if the array is full, and false (0) otherwise. */

int isFull( struct extendableArray *a ) { return( a->size == a->capacity ); }

/* Prints the error message msg. */

void printErr( char *msg ) { printf( " %s ", msg ); }

/* Prints the content of the array. */

void printArray( struct extendableArray *a ) { int i;

if( isEmpty( a ) ) printErr( "printArray(): empty array." ); for( i = 0; i size; i++) printf( "%3d ", a->arr[i] );

printf(" "); }

/************* DO NOT MODIFY ANYTHING ABOVE THIS LINE, *************/ /************* EXCEPT THE HEADER CONTAINING YOUR INFO **************/

/* Inserts integer d at the rear of the array, right behind the last element. */ /* Assume all inputs are non-negative integers. */

void insertLast( struct extendableArray *a, int d ) {

/* Add your implementation here */

}

/* Removes and returns the last element of the array (the element that was inserted last). */ /* If the array is empty, call printErr() to display a message and return -1. */

int removeLast( struct extendableArray *a ) {

/* Add your implementation here */

return( 0 ); /* replace this line with your own code */ }

LAB 5 Pointers to Structures and Dynamic Memory Allocation 1. Specification The extendable array data structure allows the array capacity to be increased or reduced according to the current array utilization. We implement a simple extendable array data structure in which elements are inserted at and removed from only one end of the array (the rear in this exercise). Write a C program to implement the insertion and deletion operations, extending or shrinking the array as needed. 2. Implementation The program to be submitted is named lab5.c. Use the given template lab5.c and fill in your code. Submit only file lab5.c. You are also given a file named lab5main.c to test your code. Do not submit file lab5main.c. The first function to be implemented is insertLast(). See file lab5.c for its specification. The new element is to be inserted at the rear of the extendable array. When a new element is inserted into a full array, extend the array by doubling its current capacity C(e.g., if C == 4 then C is increased to 8). Use function malloc or calloc to allocate memory for an array. Allocate a new array; copy the content of the old (smaller) array to the new (bigger) array; free the old array. The second function to be implemented is removeLast(). See file lab5.c for its specification. The function removes and returns the last element of the array (i.e., the element that was inserted last). If the array is empty, the function calls function printErr() to display an error message and returns -1. After a deletion, if the number of elements in the array falls below C/4 (size

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions