# Assigning the elements in a 3d array

**URL:** <https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403>\
**Category:** SystemVerilog\
**Tags:** logic-array-packed-unsigned, systemverilog-distribution-constraints, SystemVerilog, uvm-sv-constraints, constraint-foreach-loop, systemverilog-Arrays-logics\
**Created:** [May 16, 2022, 7:05am UTC](https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403 "2022-05-16T07:05:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShashankGurijala](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@ShashankGurijala](https://verificationacademy.com/forums/u/ShashankGurijala)\
**Post date:** [May 16, 2022, 7:05am UTC](https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403/1 "2022-05-16T07:05:57Z")

</div>

Hello! Look at the below code. Im trying to assign the elements as follows:

```verilog
			
class packet;

	rand int array [] [] []; //diff b/w randc bit [3:0] [1:0] array [] [] [] and randc bit [7:0] array [] [] []
	
	randc int size_i, size_j, size_k;
	
	static constraint c1 {
	
				array.size == size_i;

				foreach (array [size_i]) {
						
					array [size_i].size == size_j;
													
					foreach (array [size_i,size_j]) {
													
																						 
                                              array [size_i][size_j].size == size_k;
																						
																					 
                                                                            }
																					
												 }
												 
				   } //c1 is the constraint to randomize rows, columns and height of a 3d array
	
	static constraint c2 {size_i > 0; size_j > 0; size_k > 0;}
	
	constraint c3 {size_i <= 5; size_j <= 5; size_k <= 5;}
	
	constraint c4 {size_i != size_j && size_j != size_k && size_i != size_k;}
	
	constraint c5 {unique {array};}
	
	static constraint c6 {
				foreach (array [size_i]) {
						
					foreach (array [size_j]) {
													
						foreach (array [size_k]) {
																				
						       if (size_i % 2 == 0) {
																											
								if (size_j % 2 == 0) {
																																
								   if (size_k % 2 == 0) {
																																					
							               array [size_i] [size_j] [size_k] == 222;
																																										
									                 }
																																									
								    else if (size_k % 2 != 0) {
																																					
									array [size_i] [size_j] [size_k] == 221;
																																										
								                     }
																																				
								}
																																						
							if (size_j % 2 != 0) {
																																
									if (size_k % 2 == 0) {
																																					
										array [size_i] [size_j] [size_k] == 212;
																																									
							                                     }
																																									
					                                if (size_k % 2 != 0) {
																																					
										array [size_i] [size_j] [size_k] == 211;
																																											
									                   }
																																										 
								}
																				
									}
																															
			if (size_i % 2 != 0) {
																											
					if (size_j % 2 == 0) {
																																	
								if (size_k % 2 == 0) {
																																						
										array [size_i] [size_j] [size_k] == 122;
																																											
								}
																																										
						if (size_k % 2 != 0) {
																																						
					array [size_i] [size_j] [size_k] == 121;
																																												
									}
																																										
						}
																																				
		if (size_j % 2 != 0) {
																																	
					if (size_k % 2 == 0) {
																																						
						array [size_i] [size_j] [size_k] == 112;

						}
																																										
				if (size_k % 2 != 0) {
																																						
			array [size_i] [size_j] [size_k] == 111;
    																																												
						}
																																											 
						}
																																					
		}
																																 
																										}

																			}
																			
												}
												
					}
				 
endclass
				
module md_array_foreach_constraint;

	initial 
	
		begin
		
			packet pkt = new;
			
			$display("\n----------------------------------------DISPLAYING ARRAY ELEMENTS-----------------------------------------------\n");
			
			repeat (2)
			
				begin
			
					pkt.randomize();
					
$display ("\nThe dimensions of the randomized 3d array: Rows [i] = %0d , Columns [j] = %0d , Height [k] = %0d",pkt.size_i,pkt.size_j,pkt.size_k);
					
					for (int i = 0; i < pkt.size_i; i++)
					
						begin
							
							for (int j = 0; j < pkt.size_j; j++)
							
								begin
								
					$display ("\t"); //how is this tab space working in the program?
									
									for (int k = 0; k < pkt.size_k; k++)
									
										begin
										
					$display ("array [%0d]\t[%0d]\t[%0d] \t=\t %0d",i,j,k,pkt.array[i][j][k]);
											
										end
										
								end
								
						end
					
	$display ("\n-----------------------------------------------------------------------------------------------------------");
					
				end
				
		end
		
endmodule

```

Can you please tell me whats wrong with the code

---

<div class="post-metadata">

**Author:** ![rag123](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rag123](https://verificationacademy.com/forums/u/rag123)\
**Post date:** [May 17, 2022, 12:26am UTC](https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403/2 "2022-05-17T00:26:03Z")

</div>

you have a contradicting constraint in C5 and C6. you are asking the array to be unique at the same time from constraint C6 you are trying to assign the solver fails. It would be helpful to show what you want to achieve with expected input and expected output.

```verilog

   (array[0][0][1] == 221);
   (array[2][0][1] == 221);

```

---

<div class="post-metadata">

**Author:** ![ShashankGurijala](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@ShashankGurijala](https://verificationacademy.com/forums/u/ShashankGurijala)\
**Post date:** [May 17, 2022, 2:08am UTC](https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403/3 "2022-05-17T02:08:48Z")

</div>

_In reply to [rag123](https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403/2):_

Say for array [1] [0] [1] where i is odd, j is even and k is odd, I want 121 to be printed in that index and hence the pattern follows.

I understood the unique constraint and removed it. Then too i didn’t get the output as I wanted.

---

<div class="post-metadata">

**Author:** ![ShashankGurijala](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@ShashankGurijala](https://verificationacademy.com/forums/u/ShashankGurijala)\
**Post date:** [May 17, 2022, 9:56am UTC](https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403/4 "2022-05-17T09:56:16Z")

</div>

_In reply to [Shashank Gurijala](https://verificationacademy.com/forums/t/assigning-the-elements-in-a-3d-array/40403/3):_

Got the right answer after replacing the nested foreach loops with a single foreach(size\_i,size\_j,size\_k) {

And also comment the constraint c5 (or remove/disable)

Posting thee code here:

```verilog
class packet;

	rand bit [25:0] array [] [] [];
	
	rand int size_i, size_j, size_k;
	
	static constraint c1 {
	
						array.size == size_i;

						foreach (array [size_i]) {
						
													array [size_i].size == size_j;
													
													foreach (array [size_i,size_j]) {
													
																						array [size_i][size_j].size == size_k;
																						
																					}
																					
												 }
												 
				   } //c1 is the constraint to randomize rows, columns and height of a 3d array
	
	constraint c2 {size_i > 0; size_j > 0; size_k > 0;}
	
	constraint c3 {size_i <= 5; size_j <= 5; size_k <= 5;}
	
	constraint c4 {size_i != size_j && size_j != size_k && size_i != size_k;}
	
	constraint c5 {
	
					foreach (array [size_i,size_j,size_k]) {
						
															if (size_i % 2 == 0) {
																											
																				  if (size_j % 2 == 0) {
																																
																										if (size_k % 2 == 0) {
																																					
														   																	    array [size_i] [size_j] [size_k] == 222;
																																										
																															 }
																																									
																										if (size_k % 2 != 0) {
																																					
																																array [size_i] [size_j] [size_k] == 221;
																																										
																															 }
																																				
																										}
																																						
																				  if (size_j % 2 != 0) {
																																
																										if (size_k % 2 == 0) {
																																					
																																array [size_i] [size_j] [size_k] == 212;
																																									
									 																		 	             }
																																									
																										if (size_k % 2 != 0) {
																																					
																																array [size_i] [size_j] [size_k] == 211;
																																											
																															 }
																																										 
																										}
																				
																				  }
																															
															if (size_i % 2 != 0) {
																											
												  								  if (size_j % 2 == 0) {
																																	
																										if (size_k % 2 == 0) {
																																						
																																array [size_i] [size_j] [size_k] == 122;
																																											
																														     }
																																										
																										if (size_k % 2 != 0) {
																																						
																																array [size_i] [size_j] [size_k] == 121;
																																												
						   																								      }
																																										
																										}
																																				
																				   if (size_j % 2 != 0) {
																																	
																										if (size_k % 2 == 0) {
																																						
																																array [size_i] [size_j] [size_k] == 112;

																															 }
																																										
																										if (size_k % 2 != 0) {
																																						
																																array [size_i] [size_j] [size_k] == 111;
																																												
																															 }
																																											 
																									    }
																																					
																				 }
																																 
															}

				  }
																					 
endclass
		

		
module md_array_foreach_constraint;

	initial 
	
		begin
		
			packet pkt = new;
			
			$display("\n----------------------------------------DISPLAYING ARRAY ELEMENTS-----------------------------------------------\n");
			
			repeat (5)
			
				begin
			
					pkt.randomize();
					
					$display ("\nThe dimensions of the randomized 3d array: Rows [i] = %0d , Columns [j] = %0d , Height [k] = %0d",pkt.size_i,pkt.size_j,pkt.size_k);
					
					for (int i = 0; i < pkt.size_i; i++)
					
						begin
							
							for (int j = 0; j < pkt.size_j; j++)
							
								begin
								
									$display ("\t"); //how is this tab space working in the program?
									
									for (int k = 0; k < pkt.size_k; k++)
									
										begin
										
											$display ("array [%0d]\t[%0d]\t[%0d] \t=\t %0d",i,j,k,pkt.array[i][j][k]);
											
										end
										
								end
								
						end
					
					$display ("\n-----------------------------------------------------------------------------------------------------------");
					
				end
				
		end
		
endmodule

```

What I want to now know is the difference b/w foreach(size\_i){foreach(size\_j){foreach(size\_k)  
…}}} and foreach(size\_i,size\_j,size\_k){…} in general and in this specific example as well.

Thank you!
