Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program to remove an element from an array at the given position k an

ID: 3545383 • Letter: 1

Question

1.      Write a program to remove an element from an array at the given position k and push the rest of the array elements one position up front. Then append the removed element at the end. Position k is entered through keyboard.

For example, if the original array x is {2, 3, 5, 7, 11, 13, 17, 19} and k = 2, the array will be changed to {2, 3, 7, 11, 13, 17, 19, 5}.

Hint: Sequence of moving the element is important. You need to define a temp variable to hold value of x[k] before you move the rest of the array elements one position up front. At the end, you put the saved temp variable to the last element of the array.

Below is the codde that I am trying to work with but right now am lost and confused.I am trying to come up with a formula to switch to positions in an array but I cannot figure out how.

Explanation / Answer

#include<stdio.h>

int main()

{

int a[]={2,3,5,7,11,13,17,19};

int i;

scanf("%d",&i);

int temp=a[i];//storing the ith indxed number

//now we have to move number at ith position to end and the rest of others a step to left..

int k;

a[i]=0;

for(k=i+1;k<7;k++)

a[k-1]=a[k];//moving all other numbers after ith index to left by one position


a[k]=temp;//appending ith indexed number to last of the array


for(k=0;k<8;k++)//printing the array

printf("%d ",a[k]);

printf(" ");

return 0;

}