 |
|
| .NET DotNet Forum Index » Visual C# Forum » Pointers to arrays... |
|
Page 1 of 1 |
|
| Author |
Message |
| jd... |
Posted: Tue Oct 27, 2009 1:30 am |
|
|
|
Guest
|
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[i]; }
break;
}
case 2:
{
for (int i = 0; i < Array2.Length; i++)
{ Total += Array2[i]; }
break;
}
case 3:
{
for (int i = 0; i < Array3.Length; i++)
{ Total += Array3[i]; }
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 |
|
|
| Back to top |
|
|
|
| Alberto Poblacion... |
Posted: Tue Oct 27, 2009 6:54 am |
|
|
|
Guest
|
"jd" <jamesdorringtonuk at (no spam) googlemail.com> wrote in message
news:ef3e4c9a-9b13-40db-aaa1-de8f59a8491e at (no spam) v30g2000yqm.googlegroups.com...
Quote: 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. |
|
|
| Back to top |
|
|
|
| Family Tree Mike... |
Posted: Tue Oct 27, 2009 6:59 am |
|
|
|
Guest
|
"jd" wrote:
Quote: 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[i]; }
break;
}
case 2:
{
for (int i = 0; i < Array2.Length; i++)
{ Total += Array2[i]; }
break;
}
case 3:
{
for (int i = 0; i < Array3.Length; i++)
{ Total += Array3[i]; }
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[i]; }
}
return Total;
}
Mike |
|
|
| Back to top |
|
|
|
| Mythran... |
Posted: Tue Oct 27, 2009 9:09 am |
|
|
|
Guest
|
"Family Tree Mike" <FamilyTreeMike at (no spam) discussions.microsoft.com> wrote in
message news:16233A58-445E-4478-BB50-6605FC01F81D at (no spam) microsoft.com...
Quote:
"jd" wrote:
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[i]; }
}
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 |
|
|
| Back to top |
|
|
|
| henk holterman... |
Posted: Tue Oct 27, 2009 2:49 pm |
|
|
|
Guest
|
jd wrote:
Quote: 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[i];
}
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;
} |
|
|
| Back to top |
|
|
|
| Mythran... |
Posted: Tue Oct 27, 2009 3:10 pm |
|
|
|
Guest
|
"henk holterman" <rentahacker at (no spam) xs4all.nl> wrote in message
news:4ae75cec$0$83251$e4fe514c at (no spam) news.xs4all.nl...
Quote: 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[i];
}
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 |
|
|
| Back to top |
|
|
|
|
|
All times are GMT - 5 Hours
The time now is Fri Dec 11, 2009 6:04 am
|
|