# #280 – Implicitly-Typed Arrays

March 24, 2011 8 Comments

In the same way that you can declare an implicitly-typed variable, letting the compiler figure out the type of the variable’s value, you can declare implicitly-typed arrays.

In the example below, we declare and initialize both implicitly-typed variables and implicitly-typed arrays. The compiler infers the type–shown in the associated comment.

// Implicitly-typed variables var x = 42; // int var s = "Hooey"; // string // Implicitly-typed arrays var someArray = new[] { 5, 6, 7 }; // int[] var arrB = new[] { new Dog("Bob"), null }; // Dog[]

Advertisements

Hi Sean, nice to meet you.

Playing with arrays.

With myArray1 [320,18] and myArray2 [8,18]

How do this:

myArray2 [0,0] = MAXIMUM (myArray1 [11,0] ~ myArray1 [17,0])

myArray2 [1,0] = AVERAGE (myArray1 [18,0] ~ myArray1 [24,0])

myArray2 [2,0] = AVERAGE (myArray1 [25,0] ~ myArray1 [75,0])

myArray2 [3,0] = AVERAGE (myArray1 [76,0] ~ myArray1 [18,0])

myArray2 [4,0] = AVERAGE (myArray1 [181,0] ~ myArray1 [320,0])

?

Regards,

ocaccy

It’s not quite clear what your question is, or what you’re trying to do. Can you describe the problem more completely? Also, I’m not sure why you have the tilde (~) in your expressions – is this meant to be a minus (-) sign?

Thank you, Sean.

With myArray1 [320,18] and myArray2 [8,18]

How do this:

myArray2 [0,0] = MINIMUM from here (myArray1 [11,0] to here myArray1 [17,0]

myArray2 [1,0] = MAXIMUM from here (myArray1 [11,0] to here myArray1 [17,0])

myArray2 [2,0] = AVERAGE from here (myArray1 [18,0] to here myArray1 [24,0])

myArray2 [3,0] = AVERAGE from here (myArray1 [25,0] to here myArray1 [75,0])

myArray2 [4,0] = AVERAGE from here (myArray1 [76,0] to here myArray1 [180,0])

myArray2 [5,0] = AVERAGE from here (myArray1 [181,0] to here myArray1 [320,0])

?

Regards,

ocaccy

Sounds like you’ll want to iterate through the elements in your array for which you want a minimum, maximum or average and calculate the relevant values. For minimum, just store the lowest value you find in that range of the array, for maximum the highest value and for average, sum up the elements and divide by the count.

I have tried various solutions.

I would like to see how would your solution to this.

I have done as below but still very confusing to explain to my children.

int max(int array[][WIDTH], int first_col, int last_col, int first_row, int last_row)

{

int i_max = array[first_col][last_col];

for(int i = first_col; i <= last_col; i++)

for(int j = first_row; j i_max)

i_max = myArray[i][j];

return i_max;

}

double average(int array[][WIDTH], int first_col, int last_col, int first_row, int last_row)

{

double the_avg = 0;

for(int i = first_col; i <= last_col; i++)

for(int j = first_row; j i_max)

the_avg += myArray[i][j];

the_avg /= ( (last_col – first_col + 1)*(last_row – first_row + 1) );

return the_avg;

}

Sean,

how to convert string myArray[310,120] to int myArray[310.120]

or

string myArray[310,120] to int myArray2[310,120].

Best Regards,

ocaccy

Pingback: #596 – Implicitly-Typed Arrays and Best Type Inference « 2,000 Things You Should Know About C#

Pingback: #775 – Copying an Array of Anonymously-Typed Objects « 2,000 Things You Should Know About C#