I need some assistance in C language.
Assuming I have an array of 10 elements:
int arr[10] = {1,2,999,4,5,999,7,999,9,10};
I want to add the number 1000
at every position where 999
is found without deleting it of course. Or in that case positions where 1000 has to be added: arr[2]
, arr[5]
, arr[7]
So my result buffer would be after compiling (of course increased by the amount of positions where 999 has been added):
temp[100] = {1,2,1000,999,4,5,1000,999,7,1000,999,9,10};
Can you help me with that?
CodePudding user response:
You can do this by using conditionals like given below.
//I assume that the arrays are already declared
int i,j;
for(i = 0, j = 0; i < n; i , j ){ //here n is the size of the array
if(arr[j] == 999){
temp[i] = 1000;
i ; n ;
temp[i] = 999;
}
else
temp[i] = arr[j];
}
Try this out. This code snippet may not seem so standard but this gives you your desired output...