C Programming

C Programming Task to Write 1 Function

/*Assignm
* run many code samples & provide output
*
* L: the list
* n: number of items stored in the list, not the size of the array
* val: number to insert into the list, the data
* loc: the index at which new data should be inserted
* all other data in list is preserved
* All parameters will be valid; no error checking needed
* Sample output: 20 10 40 30
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void insertValAt( int L[], int n, int val, int loc );

void printL( int L[], int N )
{
int k;
for ( k = 0; k < N; k++ )
printf(“%2d “, L[k]);
printf(“\n”);
}
int main()
{
int L[100];
insertValAt(L,0,10,0);
insertValAt(L,1,20,0);
insertValAt(L,2,30,2);
insertValAt(L,3,40,2);
printL(L,4);
return 0;
}

void insertValAt( int L[], int n, int val, int loc )
{
int size = n+1;
size++;
for (int i = size; i > loc; i–)
{
L[i] = L[i – 1];
}
L[loc] = val;
}