Arranging zeroes in an array at odd indices

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

In reply to Shashank Gurijala:

I tried to infer the algorithm you are intending to use from your code. Some things don’t make sense to me. Can you please try to explain the algorithm that you intend to use here ?

This is what I would do : (there could be better ways)

  • start from element 0 and walk to the end.
  • [list]
  • If even and non-zero → do nothing
  • else if odd and zeo → do nothing
  • else swap such that:: if even, swap with next non-zero element; if odd, swap with next zero element.

[/list]

Ofcourse, I assume that the input array always has a valid output available.

My implementation below :


module zero;
  int i,j; 
  int temp;
  
  initial begin
    int array[] = {0,0,4,5,1,0,9,0,8,0};
    
    $display("Before : The elements in the array are: %p", array);

    foreach(array[i]) begin
      if( (i%2 == 0) && array[i] !=0 )      continue;
      else if( (i%2 == 1) && array[i] ==0 ) continue;
      else swap_f(array, i, i%2);
    end
    
    $display("After : The elements in the array are: %p", array);

  end
      
  function automatic swap_f(ref int arr[], input int arr_idx, input bit odd);
    if(odd) begin 
      for(int ii = arr_idx+1; ii < arr.size(); ii++) begin
        if(arr[ii] != 0) continue;
        else begin
          arr[ii] = arr[arr_idx];
          arr[arr_idx] = 0;
          break;
        end
      end
    end else begin //even
      for(int ii = arr_idx+1; ii < arr.size(); ii++) begin
        if(arr[ii] == 0) continue;
        else begin
          arr[arr_idx] = arr[ii];
          arr[ii] = 0;
          break;
        end
      end
    end
  endfunction 
  
 
endmodule

In reply to KillSteal:

Hey!! Thanks for the solution. Also, I’ve tried a code and it works fine!! Here it goes:

module zero;

	int i,j,temp;
	
	int a[10];
	
	initial 
	
		begin
		
			a = {0,1,0,2,1,0,0,0,9,8};
			
			$display("\nThe elements in the array INITIALLY are:\n");
 
			foreach(a[i])
 
				begin
				
					if (i % 2 != 0)
				
					$display("a[i=%0d] = %0d --- INDEX = %0d",i,a[i],i);
					
					else 
					
					$display("a[i=%0d] = %0d",i,a[i]);
		
				end
				
			$display("\n\nThe elements in the array AFTER re-arranging is:\n");
			
			for (i = 0; i <= 9; i++)
			
				begin
				
					if (a[i] == 0 && i % 2 == 0)
					
						begin
					
							for (j = 0; j <= 9; j++)
						
								begin
								
									if (a[j] != 0 && j % 2 != 0)
									
										begin
										
											temp = a[j];
											
											a[j] = a[i];
											
											a[i] = temp;
											
											break;
											
										end
									
								end
								
						end
						
				end	
			
			    foreach(a[i])
 
				begin
				
					if (i % 2 != 0)
				
					$display("a[i=%0d] = %0d --- INDEX = %0d",i,a[i],i);
					
					else 
					
					$display("a[i=%0d] = %0d",i,a[i]);
		
				end
			
		end
		
endmodule

In reply to Shashank Gurijala:


module zero;
  int nonzero_val_q [$];

  initial begin
    int array[] = {0,0,4,5,1,0,9,0,8,0};
    
    nonzero_val_q = array.find with (item != 0);
    //$display("%p",nonzero_val_q);
    
    foreach(array[i])begin
        if(i%2 != 0) array[i] = 0;
        else array[i] = nonzero_val_q.pop_front(); 
    end
    $display("arranged array : %p",array);
  end

endmodule 


//output: 
arranged array : '{4, 0, 5, 0, 1, 0, 9, 0, 8, 0}