Array ADT

Time limit: 5000ms
Memory limit: 256mb

Description:

Complete the following implementation of Array ADT.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Array;
struct Array* createArray(int n);
int retrieve(struct Array* arr, int i);
void store(struct Array* arr, int i, int itemToStore);
void insertInplace(struct Array* arr, int i, int item);
void delete(struct Array* arr, int i);
int maximum(struct Array* arr);

int main() {
  int T;
  struct Array* arr;
  scanf("%d", &T);
  for (int i = 0; i < T; i++) {
    char cmd[1024];
    scanf("%s", cmd);
    if (strcmp("createArray", cmd) == 0) {
      int n;
      scanf("%d", &n);
      arr = createArray(n);
    }
    if (strcmp("retrieve", cmd) == 0) {
      int i;
      scanf("%d", &i);
      printf("%d\n", retrieve(arr, i));
    }
    if (strcmp("store", cmd) == 0) {
      int i, item;
      scanf("%d%d", &i, &item);
      store(arr, i, item);
    }
    if (strcmp("insertInplace", cmd) == 0) {
      int i, item;
      scanf("%d%d", &i, &item);
      insertInplace(arr, i, item);
    }
    if (strcmp("delete", cmd) == 0) {
      int i;
      scanf("%d", &i);
      delete(arr, i);
    }
    if (strcmp("maximum", cmd) == 0) {
      printf("%d\n", maximum(arr));
    }
  }
}

/* Please keep the code above unchanged
 * You can only edit the following code */

// The Array ADT
struct Array {
  int size;
  int *data;
};

// Initialize an array of size n and set all elements to 0.  If n < 0, make an
// array of size 0 and output "error".
struct Array* createArray(int n) {
  // WRITE YOUR OWN CODE HERE
}

// Return the item stored in the i-th position of the array. If i is out of
// range, return -1 and output "error".
int retrieve(struct Array* arr, int i) {
  // WRITE YOUR OWN CODE HERE
}

// Store item to the i-th position of array. If i is out of range, just output
// "error".
void store(struct Array* arr, int i, int itemToStore) {
  // WRITE YOUR OWN CODE HERE
}

// Drop the last item, move i-th item and other items after the i-th position
// afterward, and insert item to the i-th position. If i is out of range, just
// output "error".
void insertInplace(struct Array* arr, int i, int item) {
  // WRITE YOUR OWN CODE HERE
}

// Delete item in the i-th position, move other items after the i-th position
// forward and set the last element to 0. If i is out of range, just output
// "error".
void delete(struct Array* arr, int i) {
  // WRITE YOUR OWN CODE HERE
}

// Return the maximum element in the array. If the size of array is 0, return -1
// and output 'error'.
int maximum(struct Array* arr) {
  // WRITE YOUR OWN CODE HERE
}

Sample input:
25
createArray 3
retrieve 0
retrieve 1
retrieve 2
store 0 1
store 1 2
store 2 3
store 3 4
retrieve 0
retrieve 1
retrieve 2
retrieve 3
maximum
insertInplace 1 3
retrieve 0
retrieve 1
retrieve 2
delete 0
retrieve 0
retrieve 1
retrieve 2
delete -1
retrieve -1
createArray -100
maximum

Sample output:
0
0
0
error
1
2
3
error
-1
3
1
3
2
3
2
0
error
error
-1
error
error
-1
Submit