50020. Generic Index Array

Problem Description

Implement a generic array that supports index starting from any integer. You must implement the following functions.

• void init(ARRAY *darray, int startIndex, int endIndex);
Initialize an array so that the index starts and ends at specified locations. It is guaranteed that the starting index is no more than the ending index. All element are considered "not yet set" after initialization.
• int set(ARRAY *array, int index, int value);
• if the index is within the range of ARRAY, set the array element at the specified index to the specified value, and return 1. It is guaranteed that we will never set an element to either -2147483648 or -2147483647.
• If the specified index is out of range then do nothing and return -2147483648.
• int get(ARRAY *array, int index);
• If the index is within the range of ARRAY then do the following.
• If element has been set before then return the value that was set last.
• If element has not been set before return -2147483647.
• If the index is out of range return -2147483648.
• void print(ARRAY *array);
Print the contents of the ARRAY. The function will print one element per line, from the start to the end of the index range. If an element has never been set before, print "No value" at that line.
• void sort(ARRAY *array);
Sort the elements in the ARRAY. Those elements that have not been set before should appear at the beginning of the array after sorting.

• 10pt. Implement init and print.
• 30pt. Implement init, set, and get, and the parameter index is always correct.
• 10pt. Implement init, set, and get, and the parameter index is not always correct.
• 30pt. Implement init, set, get, and print, and the parameter index is not always correct.
• 20pt. Implement init, set, get, print and sort, and the parameter index is not always correct.

array.h

1234567891011121314#ifndef _ARRAY_H#define _ARRAY_H /*    define structure ARRAY*/ void init(ARRAY *a,  int left, int right);int set(ARRAY *a, int index, int value);int get(ARRAY *a, int index);void print(ARRAY *a);void sort(ARRAY *a); #endif


main.c

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#include "array.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h> #define OUTOFBOUND -2147483648#define HAVENOTSET -2147483647#define SETSUCCESS 1void test_specall() {    int n, l, r;    int cmds, cmd, aid;    int index, value;    scanf("%d", &n);    ARRAY *a = (ARRAY *) malloc(sizeof(ARRAY)*n);    for (int i = 0; i < n; i++) {        scanf("%d %d", &l, &r);        init(&a[i], l, r);    }    scanf("%d", &cmds);    for (int i = 0; i < cmds; i++) {        scanf("%d %d", &cmd, &aid);        if (cmd == 1) {            print(&a[aid]);        } else if (cmd == 2) {            scanf("%d %d", &index, &value);            int ret = set(&a[aid], index, value);            printf("ARRAY ID %d, set(%d, %d) %s\n", aid, index, value,                 ret == SETSUCCESS ? "SUCCESS" : (ret == OUTOFBOUND ? "OUTOFBOUND" : "HAVENOTSET"));        } else if (cmd == 3) {            scanf("%d", &index);            int ret = get(&a[aid], index);            printf("ARRAY ID %d, get(%d) ", aid, index);            if (ret == OUTOFBOUND)                    printf(": OUTOFBOUND\n");            else if (ret == HAVENOTSET)                printf(": HAVENOTSET\n");            else                printf("= %d\n", ret);        } else if (cmd == 4) {            sort(&a[aid]);        } else {            assert(0);        }    }}int main() {    int spec = 0;    void (*FUNC[])() = {test_specall};    FUNC[spec]();    return 0;}


Sample Input

30 10-5 01000 2000241 02 0 2 42 0 3 92 0 5 251 02 1 -1 -12 1 -3 -92 1 0 01 13 0 53 1 -33 0 -13 1 12 0 -1 32 1 5 63 0 44 01 04 11 12 1 -3 5141 14 11 1


Sample Output

No valueNo valueNo valueNo valueNo valueNo valueNo valueNo valueNo valueNo valueNo valueARRAY ID 0, set(2, 4) SUCCESSARRAY ID 0, set(3, 9) SUCCESSARRAY ID 0, set(5, 25) SUCCESSNo valueNo value49No value25No valueNo valueNo valueNo valueNo valueARRAY ID 1, set(-1, -1) SUCCESSARRAY ID 1, set(-3, -9) SUCCESSARRAY ID 1, set(0, 0) SUCCESSNo valueNo value-9No value-10ARRAY ID 0, get(5) = 25ARRAY ID 1, get(-3) = -9ARRAY ID 0, get(-1) : OUTOFBOUNDARRAY ID 1, get(1) : OUTOFBOUNDARRAY ID 0, set(-1, 3) OUTOFBOUNDARRAY ID 1, set(5, 6) OUTOFBOUNDARRAY ID 0, get(4) : HAVENOTSETNo valueNo valueNo valueNo valueNo valueNo valueNo valueNo value4925No valueNo valueNo value-9-10ARRAY ID 1, set(-3, 514) SUCCESSNo valueNo value514-9-10No valueNo value-9-10514