Tuesday, September 27, 2016

Array Initialization.

Java:
// anArray = new int[10];
int[] anArray = { 
    100, 200, 300,
    400, 500, 600, 
    700, 800, 900, 1000
};

anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // and so forth

System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);


Python:
myList=[]
for i in range(10):
    myList[i]=1

for i in range(10):
    myList.append(1)

myList=[i*i for i in range(10)]
myArray=[[1,2],[3,4]]

list_of4 = [3, "test", True, 7.4]

s = ["Lee", "Walsh", "Roberson"]
s2 = ["Williams", "Redick", "Ewing", "Dockery"]
s3 = [s, s2] # 2x2 list
s4 = s + s2; # concatenation two lists

list1, list2, list3 = [1,2,3], ['a','b','c'], [7,8,9]
all_lists = sum([list1, list2, list3], [])
# all_lists == [1, 2, 3, 'a', 'b', 'c', 7, 8, 9]

listanimal.append("cat")
listanimal.extend(["dog", "mouse"])

# This is tuple
b = ("Bob", 19, "CS")       # tuple packing
(name, age, studies) = b    # tuple unpacking

# data structure with lists and tuples
students = [
    ("John", ["CompSci", "Physics"]),
    ("Vusi", ["Maths", "CompSci", "Stats"]),
    ("Jess", ["CompSci", "Accounting", "Economics", "Management"]),
    ("Sarah", ["InfSys", "Accounting", "Economics", "CommLaw"]),
    ("Zuki", ["Sociology", "Economics", "Law", "Stats", "Music"])]

julia_more_info = ( ("Julia", "Roberts"), (8, "October", 1967),
                     "Actress", ("Atlanta", "Georgia"),
                     [ ("Duplicity", 2009),
                       ("Notting Hill", 1999),
                       ("Pretty Woman", 1990),
                       ("Erin Brockovich", 2000),
                       ("Eat Pray Love", 2010),
                       ("Mona Lisa Smile", 2003),
                       ("Oceans Twelve", 2004) ])



Perl:
my @other_array = (0,0,0,1,2,2,3,3,3,4);
my @zeroes = (0) x 5; 
my @zeroes = (0) x @other_array; # A zero for each item in @other_array.
                                 # This works because in scalar context
                                 # an array evaluates to its size.
# To get the "length" or "size" of an array, simply use it in a scalar context. 
$count = @array;
# Get the highest index
$highest_index = $#array;


Ruby:
names = Array.new(20)
puts names.size  # This returns 20
puts names.length 

names = Array.new(4, "mac")
puts "#{names}"  # This returns "["mac", "mac", "mac", "mac"]"

nums = Array.new(10) { |e| e = e * 2 }
puts "#{nums}"   # This returns "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]"

nums = Array.[](1, 2, 3, 4,5)
nums = Array[1, 2, 3, 4,5]

digits = Array(0..9)
num = digits.at(6)


JavaScript:
var cars = ["Saab", "Volvo", "BMW"];
var cars = new Array("Saab", "Volvo", "BMW");
var name = cars[0];
document.getElementById("demo").innerHTML = cars[0];

// This is array
var person = ["John", "Doe", 46];
// This is object
var person = {firstName:"John", lastName:"Doe", age:46};


C++:
#include <iostream>
using namespace std;

int foo [] = {16, 2, 77, 40, 12071};
int another_foo [5] = { 16, 2, 77, 40, 12071 };

int jimmy [3][5];   // is equivalent to
int jimmy [15];     // (3 * 5 = 15) 
 
#define WIDTH 5
#define HEIGHT 3

int jimmy [HEIGHT][WIDTH];
int n,m;
int main ()
{
    for (n=0; n<HEIGHT; n++)
        for (m=0; m<WIDTH; m++) {
            jimmy[n][m]=(n+1)*(m+1);
        }
}


SystemVerilog:

  • Fixed-size (multi-dimension)
  • dynamic (single dimension)
  • queue (single dimension)
  • associative (single dimension)

typedef enum {IDLE, TEST, START} state;
enum bit[2:0] {S0 = 'b001, S1 = 'b010, S2 = 'b100} st;
state cst, nst = IDLE;
$display ("st = %3b, nst = %s", st, nst.name;
// showing on screen: st = 0, nst = IDLE

typedef reg [7:0] octet;
octet b;
// same as reg [7:0] b;
typedef octet [3:0]
quadOctet;
quadOctet qBytes [1:10];
// same as 
// reg [3:0][7:0] qBytes [1:10];
typedef enum { circle, ellipse, freeform } ClosedCurve;
ClosedCurve c;
// same as
// enum { circle, ellipse, freeform } c;
struct {
  int x, y;
} p;
p.x = 1;
p = {1,2};
typedef struct packed {
  int x, y;
} Point;
Point p;

integer numbers[5]; // array of 5 integers, indexed 0-4
int b[2] = '{3,7};
int c[2][3] = '{{3,7,1},{5,1,9}};
byte d[7][2] = '{default:-1};
bit[31:0] a[3][2] = c;
for (int i=0; i<$dimensions(a);i++) begin
   $display ($size(a, i+1));
end

// queues
int j = 1;
int q[$] = {0,1,3,6}; // note, no' as in arrays
int b[$] = {4,5};   // no '
q.insert (2, j);    // {0,1,2,3,6}
q.insert (4, b);    // {0,1,2,3,4,5,6}
q.delete (1);       // {0,2,3,4,5,6}
q.push_front (7);   // {7,0,2,3,4,5,6}
j = q.pop_back();   // {7,0,2,3,4,5}   j = 6
q.push_back(8);     // {7,0,2,3,4,5,8}
$display($size(q)); // 7
q.delete();         // delete all elements
$display($size(q)); // 0

// Associative Arrays
integer aa[*];
integer aa_too[int];
// use aa.delete(), aa.first(), aa.next(), aa.prev(), aa.last()
// to traverse
byte ba[string], t[*], a[*];
int index;
ba["byte0"] = -8;
for (int i=0; i < 10; i++) 
   t[1<<i] = i;
a=t;
$display ("size of t array is: %0d", t.num()); // array size

// Array Methods
// num(), delete(), exists(), first(), last(), next(), prev()

byte ba[string], t[*], a[*];
int index;
ba["byte0"] = -8;
for (int i=0; i mmm 10; i++) begin
   t[1<<i] = i;
end
// t[1]=0, t[2]=1; t[4]=2, t[8]=3,...
a=t;
$display ("size of t array is: %0d", t.num()); // array size

// Array Loop : foreach (array[i]) 
// Array Methods:
// function array[$] array.find() with (item < 3) // returns {value}
// function int[$] array.find_index() with (item < 3) // returns {index}
// function array[$] array.find_first() [with (exp)]
// function int[$] array.find_first_index() [with (exp)]



No comments:

Post a Comment