Hi, I want to implement a dynamic array of 10 elements . If I insert in the 5th location from that point elements will move to next locations. After insertion, the array will have 11 locations.
ex:-
5th value moves to 6th loc
6th value moves to 7th loc and so on.
In reply to Abhijeet Anand:
You can use array concatenation. This creates a new 11 element array from the 10 element array and copies it back.
module top;
int da[];
initial begin
da = {0,11,22,33,44,55,66,77,88,99};
da = {da[0:4],1010,da[5:9]};
$display("%p",da);
end
endmodule
But a queue is better suited for this kind insertion of single elements without haven to reallocate a new alway every time.
module top;
int q[$];
initial begin
q = {0,11,22,33,44,55,66,77,88,99};
q.insert(5,1010);
$display("%p",q);
end
endmodule
In reply to Abhijeet Anand:
Agreed with Dave. If you want to insert element then queue is the better option than dynamic array. But still if you want to insert in a dynamic array then you can create a function where you can pass the index and value to insert.
module main();
initial begin
int a[];
a = {0,1,2,3,4,5,6,7,8,9};
dyn_insert(a,5,10);
$display("a=%0p",a);
end
function automatic void dyn_insert(ref int b[],input int idx, val);
int q[$] = b;
q.insert(idx,val);
b = q;
endfunction
endmodule