Array

Hi
can someone tell me how can i do subtraction using 2 dynamic arrays which has not been initialized?

In reply to Pooja.balachandran:
What do you want to do here? As without initialization, it’s nothing but the declaration without holding any value, so you can’t substract.

here the size is unknown because i will get values into the array during simulation so i cant mention the size/ number of array elements

In reply to Pooja.balachandran:

Once you have some elements in dynamic array, you can just iterate based on its size and substract element by element, if both the arrays are having same size and same index to compare. Else you have to put some logic to find respective index and substract.

If size and index are same, then you can do

foreach(dyn_arr1[i]) result_arr[i] = dyn_arr1[i] - dyn_arr2[i];

could you please elaborate a little more on the example you have given?

so here dont i need to mention foreach (dyn_arr2[i])?

In reply to Pooja.balachandran:
You can go through the examples given in: Systemverilog Dynamic Array - Verification Guide

In reply to Pooja.balachandran:

so here dont i need to mention foreach (dyn_arr2[i])?

If size of both the arrays are equal then you can use any one of them.

Simplistically you can do this

int results[$];
if ((dyn_arr1.size() > 0) && ( dyn_arr1.size() == dyn_arr2.size())) begin
foreach(dyn_arr1[i] begin
result[i] = dyn_arr1[i] - dyn_arr2[i];
end
end