Array management 2021 (Required)

Time limit: 500ms
Memory limit: 256mb

Description:
In this exercise, you are required to maintain an array of sorted integers in ascending order and support the following operations.

1.	Insert an integer
2.	Delete an integer
3.	Check whether an integer exists
4.	Update one matching integer
5.	Update all matching integers
6.	Print all elements in the array in ascending order

You can assume that there are 100 integers at most, so an array A[100] is enough to store all the integers (i.e. length A <= 100). What’s more, all the integers of A[] are not distinct, which means that A[0] <= A[1] <= … <= A[n-1].

Then the program performs Insert, Delete, Update and Print operations. The user will be prompted to input the integers for insertion, deletion, update.

The code template has been provided. Your task is to complete the following functions.

int Insert(int value, int A[], int n); 
Insert one value into A[] of length n and return the new length. After insertion, A[] should still be in ascending order.
Example:  insert 1 to [1,1,3,4,7]
Array will be something like: [1,1,1,3,4,7], the function returns new length n’ = 6

int Delete(int value, int A[], int n); 
Delete one value from A[] of length n and return the new length. You could assume that the value exists in A[] before deletion. After deletion, A[] should still  be in ascending order.
Example:  
delete 4
Array will be something like: [1,1,1,3,7], the function returns new length n’ = 5

int Exist(int value, int A[], int n);
Check whether an integer exists in the array A[]. If exist, return 1, if not, return 0.
Example
Check exist 3
The function will return 1 as 3 exists.

Check exist 5
The function will return 0 as 5 does not exists.

int Update_one(int old_value, int new_value, int A[], int n);
Change one old_value to new_value in A[] of length n and return the new length. After the update, A[] should still be in ascending order.
Example:
update 1 to 2
Array will be something like: [1,1,2,3,7] (i.e. only one 1 becomes 2), the function returns length n’ = 5

int Update_all(int old_value, int new_value, int A[], int n);
Change all matching old_value to new_value in A[] of length n and return the new length. After the update, A[] should still be in ascending order.
Example:
update 1 to 7
Array will be something like: [2,3,7,7,7] (i.e. all 1s will become 7s), the function returns length n’ = 5

void Print(int A[], int n);
Example:
2 3 7 7 7

Tips: You may use Exist(), Insert() and Delete() to implement the Update() and do not forget to update n after insertion and deletion.

Sample input:
6
5 3 7 9 3 1
3
5
7 10
3 4

Sample output:
1 4 4 4 9 10

-------------------------Copy the following code, complete it and submit-------------------------

#include <stdio.h>

int Insert(int value, int A[], int n)
{
    // insert value into A[], return new length n

}

int Delete(int value, int A[], int n)
{
    // delete value from A[], return new length n

}

int Exist(int value, int A[], int n){
    // check whether value exists in array A[]

}

int Update_one(int old_value, int new_value, int A[], int n)
{
    // change old_value to new_value, return new length n

}

int Update_all(int old_value, int new_value, int A[], int n)
{
    // change old_value to new_value, return new length n

}

void Print(int A[], int n)
{
    // print A[0], A[1], ..., A[n-1] in one line, every two integers are separated by a space

}

int main()
{
    int A[100];
    int n; // the number of integers in A
    int value, new_value;

    // printf("Array initialization: \n");
    // printf("How many integers in array initially: ");
    scanf("%d", &n);
    // printf("What are the integers: ");
    int temp = 0;
    for(int i=0; i<n; i++){
        scanf("%d", &value);
        temp = Insert(value, A, temp);
    }

    // printf("Input a value for insertion: ");
	scanf("%d", &value);
	n = Insert(value, A, n);

	// printf("Input a value for deletion: ");
	scanf("%d", &value);
	n = Delete(value, A, n);

	// printf("Input the old and new values for update(one): ");
	scanf("%d%d", &value, &new_value);
	n = Update_one(value, new_value, A, n);

    // printf("Input the old and new values for update(all): ");
	scanf("%d%d", &value, &new_value);
	n = Update_all(value, new_value, A, n);

    // printf("Current array: \n");
	Print(A, n);

	return 0;
}

-------------------------------------------End of Code-------------------------------------------

Submit