(memory.c) Write a C program that implements the following three functions a) in
ID: 3567500 • Letter: #
Question
(memory.c) Write a C program that implements the following three functions
a) int * intToPointer(int num)
b) int * appendElement(int *input, int totNum, int * newElement)
c) int * removeElement(int * input, int totNum, int index)
intToPointer takes an int as input returns a pointer to an int where the pointer variable points to the value of num (not the variable itself!). This function must use the calloc function to allocate memory for the pointer to int.
appendElement takes a pointer to an array input, the extent of the 1d array is passed as totNum, and a pointer to the newElement to be added to the end of the array input. This function must use the realloc function to increase the size of the array by 1 every time an element is added.
removeElement takes a pointer to an array input, the extent of the 1d array is passed as totNum, and the index of the element to be removed. You may assume that the index value will always be between 0 and (totNum-1). This function must use the realloc function to decrease the size of the array by 1 element every time an element is removed without destroying any of the values in the array (besides the element that was removed)
Your main function must add the input elements in the array by calling the intToPointer function and passing the returned pointer to the appendElement. After all of the elements have been added, your program should remove the third element at index 2 then print out the elements in the pointer to array. You will need to keep track of the number of elements in your newly created pointer to array of ints.
Input array: int input[] = {38, 62, 1, 14, 72, 54, 34, 91, 62};
Sample ouput screenshot:
(memory.c) Write a C program that implements the following three functions a) int * intToPointer(int num) b) int * appendElement(int *input, int totNum, int * newElement) c) int * removeElement(int * input, int totNum, int index) intToPointer takes an int as input returns a pointer to an int where the pointer variable points to the value of num (not the variable itself!). This function must use the calloc function to allocate memory for the pointer to int. appendElement takes a pointer to an array input, the extent of the 1d array is passed as totNum, and a pointer to the newElement to be added to the end of the array input. This function must use the realloc function to increase the size of the array by 1 every time an element is added. removeElement takes a pointer to an array input, the extent of the 1d array is passed as totNum, and the index of the element to be removed. You may assume that the index value will always be between 0 and (totNum-1). This function must use the realloc function to decrease the size of the array by 1 element every time an element is removed without destroying any of the values in the array (besides the element that was removed) Your main function must add the input elements in the array by calling the intToPointer function and passing the returned pointer to the appendElement. After all of the elements have been added, your program should remove the third element at index 2 then print out the elements in the pointer to array. You will need to keep track of the number of elements in your newly created pointer to array of ints. Input array: int input[] = {38, 62, 1, 14, 72, 54, 34, 91, 62}; Sample ouput screenshot:Explanation / Answer
Please note that there are no functions to retrieve size of array when it is referenced by pointer (int *input <instead of> int input[10] ). Therefore a global variable 'n' is used to maintain size following good programming practice.
#include<stdio.h>
#include<stdlib.h>
void arrP(int* ,int);
int n;
int main(){
int* intToPointer(int num);
int* appendElement(int* input, int totNum, int* newElement);
int* removeElement(int* input, int totNum, int index);
int* input = NULL;
n = 0;
input = appendElement(input, n, intToPointer(38));
input = appendElement(input, n, intToPointer(62));
input = appendElement(input, n, intToPointer(1));
input = appendElement(input, n, intToPointer(14));
input = appendElement(input, n, intToPointer(72));
input = appendElement(input, n, intToPointer(54));
input = appendElement(input, n, intToPointer(34));
input = appendElement(input, n, intToPointer(91));
input = appendElement(input, n, intToPointer(62));
input = removeElement(input,n,2);
arrP(input,n);
return 0;
}
int* intToPointer(int num){
int* temp = calloc(1,sizeof(int));
*temp = num;
return temp;
}
int* appendElement(int* input,int totNum, int* newElement){
input = (int* )realloc(input,(totNum+1)*sizeof(int));
//arrP(input,totNum+1);
input[totNum] = *newElement;
n=n+1;
return input;
}
void arrP(int * input,int totNum){
int i;
for(i=0;i<totNum;i++){
printf("%d, ",input[i]);
}
}
int* removeElement(int* input, int totNum, int index){
int i=index;
for(;i<totNum-1;i++){
input[i] = input[i+1];
}
input = realloc(input,(totNum-1)*sizeof(int));
n=n-1;
return input;
}
Cheers :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.