In: Computer Science
Make a function definition in C for the following:
void insert (double *b, int c, double s, int pos);
//Insert value s at position pos in array.
//needs:
// c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist.
//this will do:
//Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos].
//Note: the data that was in b[c-1] when the function starts will be lost.
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
#include <stdio.h>
void insert (double *b, int c, double s, int pos){
for(int i=c;i>pos;i--){
b[i] = b[i-1];
}
b[pos] = s;
}
void print(double *b, int c){
int i;
for(i=0;i<c;i++){
printf("%.2f ",b[i]);
}
}
int main()
{
double array[10];
array[0] = 1;
array[1] = 3;
array[2] = 4;
int c=4;
int s=2;
int pos=1;
printf("Array after insert...\n");
insert(array,c,s,pos);
print(array,c);
return 0;
}