Create a program that declares an array of 10 integers. The program should initi
ID: 3567900 • Letter: C
Question
Create a program that declares an array of 10 integers. The program should initialize the array with the values 1 - 5. Then the program should request and allow the user to input the value of the array element that she would like to be deleted. The program should also request and allow the user to designate any value she would like to insert into the array, and the position where she would like to insert the value. Note that a users do not typically count beginning with the value 0, so a user indicating that she would like to insert a value into the fourth position would really like a value to be inserted into the array at index 3.
Deleting an array element requires that
(1) all elements subsequent to the eliminated value be moved forward one position
(2) the number of elements in the array is decreased by one
(3) if the array is empty, that is, it has no explicitly given values, no value can be deleted.
For example, given the array:
1 2 3 4 5
and eliminating the value at array element 3 (the value "4") would result in the following array
1 2 3 5
The number of elements in the array decreases from 5 to 4.
Inserting an array element requires that
(1) all elements at and subsequent to the insertion site be pushed back one position
(2) the desired value being inserted into the array
(3) the number of elements in the array is incremented by one
(a) if the array is completely filled, no further value can be inserted into the array
For example, given the filled array
1 2 3 4 5
and the request to insert the value "14" at index 3 would result in the following array:
1 2 3 14 4 5
The number of elements in the array changes from 5 to 6.
Your program should use three functions, declared thus:
int fillArray(int arrayName[], int sizeOfArray);
This function receives an empty array and fills it with the values 1 - 5. It returns the number of items in the array.
int deleteValue(int arrayName[], int target, int noOfElements);
This function receives an array, the value to delete from the array, and the number of elements in the array. It searches the array to ascertain that the target value is present in the array. If the value is present, it is deleted. If it is not present, the user is alerted to its absence. The number of elements remaining in the array is returned.
Note that if the array is empty, the function should not be invoked.
int insertValue(int arrayName[], int target, int position,
int noOfElements);
This function receives an array, the value to insert into the array, the insertion index, and the number of elements in the array. It inserts the target value at the insertion site. The number of elements remaining in the array is returned.
Note that if the array is full when the user requests an insertion, the function should not be invoked.
The best solution is as "elegant" as possible and should avoid a situation such that a user is asked if she would like to delete or insert a value from/to the array and then denied the action.
Explanation / Answer
#include <stdio.h>
int main()
{
int array[100], position, c, n, value,i,pos,size;
printf("Enter number of elements in array ");
scanf("%d", &n);
printf("Enter %d elements ", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an element ");
scanf("%d", &position);
printf("Enter the value to insert ");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is ");
for (c = 0; c <= n; c++)
printf("%d ", array[c]);
printf(" Enter position where to delete: ");
scanf("%d",&pos);
i=0;
while(i!=pos-1)
i++;
while(i<10){
array[i]=array[i+1];
i++;
}
size--;
for(i=0;i<size;i++)
printf(" %d",array[i]);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.