Hello! I’m trying to arrange the zeroes in an array at odd index positions and below is the array and the logic applied. It’s working fine if there are no zeroes preceding at even positions. But it is failing when there are consecutive zeroes. What are changes required in the logic?
THIS CODE IS WORKING FINE FOR THE FOLLOWING ARRAY AND NOT PASSING FOR FEW OTHER CASES
module zero;
int i,j;
int temp;
initial
begin
int array [10] = {0,0,4,5,1,0,9,0,8,0};
$display("The elements in the array are:\n");
foreach(array[i])
$display("%0d",array[i]);
for (i = 0; i <= 9; i++)
begin
if (array [i] == 0)
begin
for (j = i; j <= 9; j++)
begin
if (i % 2 == 0 )
begin
temp = array [j];
array [j] = array [i];
array [i] = temp;
end
end
end
end
$display("The new arrangement is:\n");
foreach (array[i])
$display("%0d at i = %0d",array[i],i);
end
endmodule