Given the array:
int numbers[]={-2,-1,3,2,-8,6,-10,-2,5,1}
the dimension and a certain number to look for as parameters to a function
i have to insert the number 2 into the array after every x given as a parameter
and return the number of insertions.
result example inserting 2 after every -2
Number of insertions: 2;
-2, 2, -1, 3, 2, -8,6,-10,-2,2,5,1
My code so far;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int inserare(int v[], int n, int x) {
int insertions=0;
bool ok;
do { ok=false;
for(int i=0;i<n;i++)
if(v[i]==x)
{
n=n+1;
insertions=insertions+1;
ok=true;
for(int j=n;j>i;--j)
{
v[j]=v[j-1];
}
v[i+1]=2;
}
}
while(ok==true);
return insertions;
}
|