check the number of unique number - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

209


65
 
1
// Code your testbench here
2
// or browse Examples
3
4
5
class A;
6
  rand int num[10];
7
8
  rand int helper[10];
9
  
10
  constraint num_c{
11
    foreach(num[i]){
12
      num[i] inside {[0:9]};
13
    }
14
  }
15
      
16
  task print_t();
17
    int i = 2;
18
    fork
19
      while(i) begin
20
        #5;
21
        $display("time stamp %t, i=%0d", $time, i);
22
        i--;
23
      end
24
    join_none
25
  endtask
26
27
endclass
28
            
29
            
30
module top;
31
  initial begin
32
    A a = new();
33
    a.print_t();
34
    $display("outside of the loop, timestamp = %t", $time);
35
    repeat(10) begin
36
      a.randomize();
37
      $display("randomized number: %0p", a.num);
38
      check_un(a.num);
39
    end
40
  end
41
  
42
endmodule
43
44
// check the number of unqiue number in a array
45
46
function int check_un(int arr[]);
47
  int hash_arr[int];
48
  int unique_arr[$];
49
  int result;
50
  for(int i=0;i<arr.size();i++) begin
51
    if(hash_arr.exists(arr[i])) hash_arr[arr[i]]++;
52
    else hash_arr[arr[i]] = 1;
53
  end
54
55
  
56
  unique_arr = hash_arr.find_index() with (item == 1);
57
  if(unique_arr.size() != 0)
58
    result = unique_arr.size();
59
  else 
60
    result = -1;
61
  $display("hash arr is %p", hash_arr);
62
  return result;
63
64
endfunction
65
    
xxxxxxxxxx
1
 
1
// Code your design here
2
40 views and 0 likes     
A short description will be helpful for you to remember your playground's details
 
100:0