Question

How to use C to output this:

indigo1 376 % lab9
Capacity = 4
Capacity = 8
0 5 10 15 20
Capacity = 16
0 5 10 15 20 25 30 35 40
Capacity = 32
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155
Capacity = 16
0 5 10 15 20 25 30
Capacity = 8
0 5 10
Capacity = 4
0

printArray(): empty array.

indigo1 377 %

2. Implementation The codes to be submitted are util.c and util.h. Use the given template util.c and util.h and fill in yourc

This is Lab9.c:

#include <stdio.h> #include <stdlib.h> #include util.h main() { int i, j, cap, numberOfTests = 32; cap = struct extendableA

This is util.c:

#include
#include


struct extendableArray {
int *arr;
int size;  
int capacity;
};


struct extendableArray *initArr()
{
struct extendableArray *a = ( struct extendableArray * ) malloc( sizeof( struct extendableArray ) );
if( !a ) {
   printf( "Insufficient memory!" );
   exit( 1 );
}
a->capacity = 4;  
a->arr = ( int * ) malloc( a->capacity * sizeof( int ));
if( !a->arr ) {
   printf( "Insufficient memory!" );
   exit(1) ;
}
a->size = 0;  
return a;
}


struct extendableArray *initArrCapacity(int capacity)
{
struct extendableArray *a = ( struct extendableArray * ) malloc(sizeof( struct extendableArray ) );
if( !a ) {
   printf( "Insufficient memory!" );
   exit( 1 );
}
a->capacity = capacity;
a->arr = ( int * ) malloc( a->capacity * sizeof( int ));

if( !a->arr ) {
   printf( "Insufficient memory!" );
   exit(1) ;
}
a->size = 0;  
return a;
}

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

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


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

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

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

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

if( isEmpty( a ) )
   printErr( "printArray(): empty array." );

for( i = 0; i < a->size; i++)
   printf( "%3d ", a->arr[i] );

printf("\n");
}

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

/* Inserts integer d at the rear of the array, right behind the last element. */


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 */
}

(util.h can add any code)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//util.h

struct extendableArray {
int *arr;
int size;
int capacity;
};

struct extendableArray *initArr();
struct extendableArray *initArrCapacity(int capacity);
int arrSize( struct extendableArray *a );
int isEmpty( struct extendableArray *a);
int isFull( struct extendableArray *a );
void printErr( char *msg );
void printArray( struct extendableArray *a );
void insertLast( struct extendableArray **a, int d );
int removeLast( struct extendableArray **a );


//util.c

#include<stdio.h>
#include<stdlib.h>
#include"util.h"

struct extendableArray *initArr()
{
struct extendableArray *a = ( struct extendableArray * ) malloc( sizeof( struct extendableArray ) );
if( !a ) {
   printf( "Insufficient memory!" );
   exit( 1 );
}
a->capacity = 4;
a->arr = ( int * ) malloc( a->capacity * sizeof( int ));
if( !a->arr ) {
   printf( "Insufficient memory!" );
   exit(1) ;
}
a->size = 0;
return a;
}


struct extendableArray *initArrCapacity(int capacity)
{
struct extendableArray *a = ( struct extendableArray * ) malloc(sizeof( struct extendableArray ) );
if( !a ) {
   printf( "Insufficient memory!" );
   exit( 1 );
}
a->capacity = capacity;
a->arr = ( int * ) malloc( a->capacity * sizeof( int ));

if( !a->arr ) {
   printf( "Insufficient memory!" );
   exit(1) ;
}
a->size = 0;
return a;
}

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

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


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

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

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

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

if( isEmpty( a ) )
   printErr( "printArray(): empty array." );

for( i = 0; i < a->size; i++)
   printf( "%3d ", a->arr[i] );

printf("\n");
}

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

/* Inserts integer d at the rear of the array, right behind the last element. */


void insertLast( struct extendableArray **a, int d )
{
   int i;
   if((*a)->size == (*a)->capacity){
       (*a)->capacity = (*a)->capacity * 2;
       int *array = (int*) malloc(((*a)->capacity)*(sizeof (int)));
      
       for(i=0;i<(*a)->size;i++) array[i] = (*a)->arr[i];
      
       free((*a)->arr);
       (*a)->arr = array;
   }
  
   (*a)->arr[(*a)->size] = d;
   (*a)->size = (*a)->size + 1;
}

/* 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 )
{
   if((*a)->size == 0){
       printErr("Array is Empty");
       return -1;
   }
   int val = (*a)->arr[--(*a)->size];
   int i;
      
   if((*a)->capacity> 4 && (*a)->size < (*a)->capacity /4) {
       if((*a)->capacity > 8) (*a)->capacity = (*a)->capacity/2;
       else (*a)->capacity = 4;
      
       int *array = (int*) malloc(((*a)->capacity)*(sizeof (int)));
      
       for(i=0;i<(*a)->size;i++) array[i] = (*a)->arr[i];
      
       free((*a)->arr);
       (*a)->arr = array;
      
   }

   return val;
}

Add a comment
Know the answer?
Add Answer to:
How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void...

    ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void func(int **a) {int tmp; for (int i = 0; i < size; i++) {for (int j = i; j < size, j++) {tmp = *(*(a+i)+j); *(*(a+i)+j) = *(*(a+j)+i); *(*(a+j)+i) = tmp;}}} int main() {int **arr = malloc(sizeof(int*) * size); for(int i = 0; i < size; i++) {arr[i] = malloc(sizeof(int) * size); for (int j = 0; j < size, j++) arr[i][j] = 2*i...

  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • 1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int...

    1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int start, int end, int *results) { int length = end - start; results = malloc(sizeof(int) * length); for (int i=start; i<end; i++) { *results = i; results++; } } void printArray(int *arr, int length) { for (int i=0; i<length; i++) { printf("%d ", arr[i]); } } int main() { int *x; int s = 2; int e = 11; range(s, e, x); printArray(x, e...

  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int #...

    Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int # define TYPE_SIZE sizeof(int) # endif # ifndef LT # define LT(A, B) ((A) < (B)) # endif # ifndef EQ # define EQ(A, B) ((A) == (B)) # endif typedef struct DynArr DynArr; /* Dynamic Array Functions */ void initDynArr(DynArr *v, int capacity); DynArr *newDynArr(int cap); void freeDynArr(DynArr *v); void deleteDynArr(DynArr *v); int sizeDynArr(DynArr *v); void addDynArr(DynArr *v, TYPE val); TYPE getDynArr(DynArr *v, int...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Hi, I need to make a program in C that reads the type of currency and...

    Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node {    int...

  • I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any...

    I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT