Pointers to arrays

J

jd

Hi guys, I have a very small method which calculates the total of 1 of
3 possible arrays. The arrays are stored locally, and the index to
which array we wish to total is passed as a parameter:

private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int Total = 0;
switch (SomeArrayID)
{
case 1:
{
for (int i = 0; i < Array1.Length; i++)
{ Total += Array1; }
break;
}
case 2:
{
for (int i = 0; i < Array2.Length; i++)
{ Total += Array2; }
break;
}
case 3:
{
for (int i = 0; i < Array3.Length; i++)
{ Total += Array3; }
break;
}
}

return Total;
}

At the moment I'm checking the input paramter to see which array I
should be totalling, then looping through it, but I'd prefer to do
some kind of check on which array to use first, then set a pointer
accordingly and then only use one loop.

Has anyone got any suggestions on how to do this?

TIA
 
A

Alberto Poblacion

jd said:
Hi guys, I have a very small method which calculates the total of 1 of
3 possible arrays. The arrays are stored locally, and the index to
which array we wish to total is passed as a parameter:
[...]
At the moment I'm checking the input paramter to see which array I
should be totalling, then looping through it, but I'd prefer to do
some kind of check on which array to use first, then set a pointer
accordingly and then only use one loop.

Has anyone got any suggestions on how to do this?

You could write the three arrays as a single "array of arrays". Then
your method parameter would just be the index for the first level of your
array of arrays.

Note that I am suggesting an array of arrays and not a bidimensional
array, since each of the arrays is of different length.
 
F

Family Tree Mike

jd said:
Hi guys, I have a very small method which calculates the total of 1 of
3 possible arrays. The arrays are stored locally, and the index to
which array we wish to total is passed as a parameter:

private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int Total = 0;
switch (SomeArrayID)
{
case 1:
{
for (int i = 0; i < Array1.Length; i++)
{ Total += Array1; }
break;
}
case 2:
{
for (int i = 0; i < Array2.Length; i++)
{ Total += Array2; }
break;
}
case 3:
{
for (int i = 0; i < Array3.Length; i++)
{ Total += Array3; }
break;
}
}

return Total;
}

At the moment I'm checking the input paramter to see which array I
should be totalling, then looping through it, but I'd prefer to do
some kind of check on which array to use first, then set a pointer
accordingly and then only use one loop.

Has anyone got any suggestions on how to do this?

TIA
.


Just set an array equal to the one you want to sum, and use it. See below:


private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int[] Target = null;

int Total = 0;
switch (SomeArrayID)
{
case 1:
{
Target = Array1;
break;
}
case 2:
{
Target = Array2;
break;
}
case 3:
{
Target = Array3;
break;
}
}

if (Target != null)
{
for (int i = 0; i < Target.Length; i++)
{ Total += Target; }
}

return Total;
}

Mike
 
M

Mythran

Family Tree Mike said:
Just set an array equal to the one you want to sum, and use it. See
below:


private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int[] Target = null;

int Total = 0;
switch (SomeArrayID)
{
case 1:
{
Target = Array1;
break;
}
case 2:
{
Target = Array2;
break;
}
case 3:
{
Target = Array3;
break;
}
}

if (Target != null)
{
for (int i = 0; i < Target.Length; i++)
{ Total += Target; }
}

return Total;
}

Mike


In this type of case, I would use an inline if instead of a switch and a
foreach instead of a for. Makes the method a bit more "elegant". I did not
test the code, so there may be syntactical errors or something :)

private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int[] Target =
SomeArrayID == 1 ? Array1 : SomeArrayID == 2 ? Array2 :
SomeArrayID == 3 ? Array3 : null;

int Total = 0;
if (Target != null) {
foreach (int i in Target) {
Total += i;
}
}
return Total;
}

HTH,
Mythran
 
H

henk holterman

jd said:
Hi guys, I have a very small method which calculates the total of 1 of
3 possible arrays. The arrays are stored locally, and the index to
which array we wish to total is passed as a parameter:

private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int Total = 0;
switch (SomeArrayID)
{
}

return Total;
}

At the moment I'm checking the input paramter to see which array I
should be totalling, then looping through it, but I'd prefer to do
some kind of check on which array to use first, then set a pointer
accordingly and then only use one loop.

Has anyone got any suggestions on how to do this?

Your switch isn't all that bad but you will want to factor out the summing:

private static int SumArray(int[] array)
{
int sum = 0;
for (int i = 0; i < array.Length; i++)
sum += array;
}

And one way of doing your TotalArray method:

private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int[][] arrays = { Array1, Array2, Array3 };

int total = SumArray(arrays[SomeArrayID-1]);
return total;
}
 
M

Mythran

henk holterman said:
jd wrote:
Your switch isn't all that bad but you will want to factor out the
summing:

private static int SumArray(int[] array)
{
int sum = 0;
for (int i = 0; i < array.Length; i++)
sum += array;
}

And one way of doing your TotalArray method:

private int TotalArray(int SomeArrayID)
{
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int[][] arrays = { Array1, Array2, Array3 };

int total = SumArray(arrays[SomeArrayID-1]);
return total;
}



After thinking about this some more, I've come up with some more ideas :D

First off, SumArray is already factored out (it's an existing extension
method for IEnumerable<int>)....

Also, for the Array of Arrays, the methods from post I'm replying to and the
other replies that use array of arrays, there is a difference in the return
value between the OP's method and the new methods.....these methods need to
check for an index outside the bounds of the array....otherwise an exception
will occur.

For scalability, I would actually do the following instead:

private int TotalArray(int SomeArrayID)
{
int[] zeroArray = new int[] { };
int[] Array1 = new int[] { 1, 2, 3 };
int[] Array2 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] Array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

return TotalArray(SomeArrayID, Array1, Array2, Array3);
}

private int TotalArray(int SomeArrayID, params int[][] Arrays)
{
return
--SomeArrayID < Arrays.GetLowerBound(0) ||
SomeArrayID > Arrays.GetUpperBound(0) ? 0 :
Arrays[SomeArrayID].Sum();
}

This covers the above mentioned problems in other examples and allows more
than 3 arrays to be passed into TotalArray....

Anywho :) Enjoy!

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top