Arrays - Which One?

  • Thread starter Thread starter Mark Givens
  • Start date Start date
M

Mark Givens

Ok, I have read more website tutorials on C# arrays than I really
wanted too and am still completely blured on the subject. I normally
develop websites using PHP/MySql, but have been forced into a bit of
C# after the C# guy had a family emergency.

Anyway, if someone has the time and patience, here is my problem.

We have a form with four numericupdowns. On each click of any
mumericupdown, calculations take place in a couple of for loops based
on the numbers selected and the results are displayed in a textbox.

example of display:

15 30 1.25 64.85
16 30 1.29 66.21
17 30 1.38 65.02

....and so on.

The calculated results need to be placed in an array as they are
produced and then sort the output based on the fouth column of
numbers.

My logic tells me to create an empty array, then add the results of
each line to the array, then when done with the calculations, print
the array sorted by the fourth column. Also, the size of the array
will never be more than 50 or so rows so efficiciency is not at the
top of the priority list.

How is this done in C#??? Multidimensional arrays seemed like a good
choice, but fuzzy on the protocol to set it up and sort.

Any help that is in simple english would be most appreciated! My
brain is still hurting from trying to comprehend some of this :)

Using VS 2003 Architect if that helps.


Thanks so much,

Mark
 
Mark said:
My logic tells me to create an empty array, then add the results of
each line to the array, then when done with the calculations, print
the array sorted by the fourth column. Also, the size of the array
will never be more than 50 or so rows so efficiciency is not at the
top of the priority list.

How is this done in C#??? Multidimensional arrays seemed like a good
choice, but fuzzy on the protocol to set it up and sort.

Hi Mark!

You can create an array like this:

int[] myIntArray = new int[5];

This would create an array with 5 items, the first item is accessed by

myIntArray[0]

and the fifth one with

myIntArray[4]

You can, for example, iterate through it with a simple for-loop:

for (int i=0;i<myIntArray.Length;i++)
Console.WriteLine(myIntArray);

HTH,

Michael
 
Mark Givens said:
Ok, I have read more website tutorials on C# arrays than I really
wanted too and am still completely blured on the subject. I normally
develop websites using PHP/MySql, but have been forced into a bit of
C# after the C# guy had a family emergency.

Anyway, if someone has the time and patience, here is my problem.

We have a form with four numericupdowns. On each click of any
mumericupdown, calculations take place in a couple of for loops based
on the numbers selected and the results are displayed in a textbox.

example of display:

15 30 1.25 64.85
16 30 1.29 66.21
17 30 1.38 65.02

...and so on.

The calculated results need to be placed in an array as they are
produced and then sort the output based on the fouth column of
numbers.

My logic tells me to create an empty array, then add the results of
each line to the array, then when done with the calculations, print
the array sorted by the fourth column. Also, the size of the array
will never be more than 50 or so rows so efficiciency is not at the
top of the priority list.

It looks to me like each row should be an instance of a custom type.
What does each row actually represent?

Once you've worked that out and got a custom type, you can easily write
an IComparer to compare instances on the fourth column, and use either
Array.Sort or possibly ArrayList.Sort if you need a dynamic number of
rows.
 
Hi Michael, thanks so much for the reply. Is it possible to sort on
the fourth field when iterating through it? I thought I read
somewhere that array.sort was for single dimension arrays only.

I think I tried something quite similar to your post and was able to
add data, and retrieve data, but I could not get it to sort :(

Mark



Mark said:
My logic tells me to create an empty array, then add the results of
each line to the array, then when done with the calculations, print
the array sorted by the fourth column. Also, the size of the array
will never be more than 50 or so rows so efficiciency is not at the
top of the priority list.

How is this done in C#??? Multidimensional arrays seemed like a good
choice, but fuzzy on the protocol to set it up and sort.

Hi Mark!

You can create an array like this:

int[] myIntArray = new int[5];

This would create an array with 5 items, the first item is accessed by

myIntArray[0]

and the fifth one with

myIntArray[4]

You can, for example, iterate through it with a simple for-loop:

for (int i=0;i<myIntArray.Length;i++)
Console.WriteLine(myIntArray);

HTH,

Michael
 
Hi Jon, thanks for the reply.

The fields represent gears and ratios and time to complete.

Column 1: Gear 1
Coumnl 2: Gear 2
Column 3: Ratio of 1 and 2
Column 4: Time to complete based on gears

Field4 is the field I need to sort by ...the number of records/rows is
determined by what gears the user selects for fields 1 and 2.
Performing the calculatons is no problem.

Have read a bit about IComparer and it sounds like something that may
do the trick, but have no idea about making each an instance of a
custom type ...sorry, but some parts of C# is Greek to me, but I am
learning!

Thanks again,

Mark
 
Mark Givens said:
The fields represent gears and ratios and time to complete.

Column 1: Gear 1
Coumnl 2: Gear 2
Column 3: Ratio of 1 and 2
Column 4: Time to complete based on gears

Right. That very much sounds like something you should be
encapsulating.
Field4 is the field I need to sort by ...the number of records/rows is
determined by what gears the user selects for fields 1 and 2.
Performing the calculatons is no problem.

Have read a bit about IComparer and it sounds like something that may
do the trick, but have no idea about making each an instance of a
custom type ...sorry, but some parts of C# is Greek to me, but I am
learning!

Well, there are two options, really:

1) implement IComparer in a class which can compare a gear pair.

2) implement IComparable in your GearPair (or whatever) class

IComparer is used to compare other objects, IComparable is used to
compare the implementing class with something else (usually another
instance of the same class).

It could well be that implementing IComparable will be simpler for you.
You could have a class such as:

public class GearPair : IComparable
{
int first;
/// <summary>
/// The number of teeth on the first gear
/// </summary>
public int First
{
get { return first; }
set { first = value; }
}

int second;
/// <summary>
/// The number of teeth on the second gear
/// </summary>
public int Second
{
get { return second; }
set { second = value; }
}

/// <summary>
/// Ratio of first to second
/// </summary>
public double Ratio
{
get { return first/second; }
}

/// <summary>
/// Time to complete
/// </summary>
public TimeSpan Time
{
// Just an example - I don't know what your logic is
get { return TimeSpan.FromMilliseconds(Ratio*10000); }
}

public GearPair (int first, int second)
{
this.first = first;
this.second = second;
}

public int CompareTo (object obj)
{
GearPair other = obj as GearPair;
if (other==null)
{
throw new ArgumentException("obj");
}

return Time.CompareTo(other.Time);
}
}
 
Hi Jon, Thanks for the example. I tried to use it, but for some
reason could not get it to work, and so I ventured down the IComparer
road.

I seem to have it almost working, but am having difficulty getting my
array with all the calculated conversions in it to be recognized
before it is sent to be compared. Hope that made sense.

Portion that errors:
mysort s = new mysort();
Array.Sort(c,s); // ERRORS (The name "c" does not exist...)
foreach (conversions s in c)
snip...

I guess I need to assign a value to my array???? Below is what I
have....Thanks again for the help.

Mark


private void ApplyCalculations()
{

------------------------------snip------------------------------
-----------Lots of pre-calculate stuff was here-----------
------------------------------snip------------------------------

// place header in textbox

this.textBoxResults.Text = "RATIO TIME
\r\n------------------------\r\n";


// create array

ArrayList conversions = new ArrayList();

// run inputs through loops for each gear1/gear2 combination

for (int i = 0; i < gearOneSteps; i++)
{
for (int x = 0; x < gearTworSteps; x++)
{

------------------------------snip------------------------------
------------ Lots of "do logic" stuff was here--------------
------------------------------snip------------------------------

// add results to array
conversions.Add( RATIO + "|" + TIME);

} // end inside for loop
} // end outside for loop


mysort s = new mysort();
Array.Sort(c,s); // ERRORS (The name "c" does not exist...)
foreach (conversions s in c)
{

// output results to textbox

this.textBoxResults.Text += s.ToString() +
"\r\n--------------------------------------------------------\r\n";
}
return 0;

}
}


---------------------------------------------------------------
---------Below appears to be valid - No errors---------
---------------------------------------------------------------

public class mysort : IComparer
{
public int Compare ( object a, object b )
{
int m1 = ((conversions)a).mph ;
int m2 = ((conversions)b).mph ;
int r1 = ((conversions)a).ratio ;
int r2 = ((conversions)b).ratio ;

if ( m1 == m2 )
{
return r1.CompareTo(r2) ;
}

if ( m1 < m2 )
{
return -1 ;
}
else
{
return 1 ;
}
}

class conversions
{
public int ratio ;
public int mph ;

public conversions ( int m, int r)
{
mph = m ;
ratio = r ;
}

public new string ToString()
{
return mph + " " + ratio ;
}
}

}
 
Ok, getting closer ...

After my for each loops, I added...

conversions.Sort(new mysort());

and got rid of ...for now, the following. Not really interested in
seein the results ...can make that happen whenever everything else is
ok.

mysort s = new mysort();
Array.Sort(c,s); // ERRORS (The name "c" does not exist...)
foreach (conversions s in c)
{
// output results to textbox

this.textBoxResults.Text += s.ToString() +
"\r\n--------------------------------------------------------\r\n";
}
return 0;

}

-------------------------

It compiles, but IComparer throws an exception when it tries to work.
 
Mark Givens said:
Hi Jon, Thanks for the example. I tried to use it, but for some
reason could not get it to work, and so I ventured down the IComparer
road.

I seem to have it almost working, but am having difficulty getting my
array with all the calculated conversions in it to be recognized
before it is sent to be compared. Hope that made sense.

Portion that errors:
mysort s = new mysort();
Array.Sort(c,s); // ERRORS (The name "c" does not exist...)
foreach (conversions s in c)
snip...

I guess I need to assign a value to my array???? Below is what I
have....Thanks again for the help.

The problem is that you haven't got a variable called c - there's
nothing to sort! What were you trying to do?

Note that you can sort the ArrayList instead of the array very easily.
 
Ok, scrapped most of what Ii was doing, since I was getting nowhere
with it ...what I have now (referenced below) I believe is very close
to working, but it doesn't.

It does compile, but the IComparer throws an exception when a
calculatis made and I don't understand why. Man, this stuff is so
entirely different than my PHP world ...but it has been challenging,
and I am learning bits and pieces ...here and there.

Thanks,

Mark




private void ApplyCalculations()
{

------------------------------snip------------------------------
-----------Lots of pre-calculate stuff was here-----------
------------------------------snip------------------------------

// place header in textbox

this.textBoxResults.Text = "RATIO TIME
\r\n------------------------\r\n";

// Start the array

ArrayList conversions = new ArrayList();

for (int i = 0; i < gearOneSteps; i++)
{
for (int x = 0; x < gearTwoSteps; x++)
{

------------------------------snip------------------------------
------------ Lots of "do logic" stuff was here--------------
------------------------------snip------------------------------

// after logic, add data to array
conversions.Add(RATIO + TIME);
}
}


// Sort the data via IComparer

conversions.Sort(new mysort());

//Loop through and add it to the textbox

for(int z = 0; z < conversions.Count; z++)
{
this.textBoxResults.Text +=
(conversions[z].ToString()) +
"\r\n--------------------------------------------------------\r\n";
}
}


public class mysort : IComparer
{
public int Compare ( object a, object b )
{
int m1 = ((conversions)a).mph;
int m2 = ((conversions)b).mph;
int r1 = ((conversions)a).ratio;
int r2 = ((conversions)b).ratio;

if ( m1 == m2 )
return 0;
if ( m1 < m2 )
return -1;
return +1;
}
}

public class conversions
{
public int ratio;
public int mph;

public conversions ( int m, int r)
{
mph = m;
ratio = r;
}
}
 
Mark Givens said:
Ok, scrapped most of what Ii was doing, since I was getting nowhere
with it ...what I have now (referenced below) I believe is very close
to working, but it doesn't.

It does compile, but the IComparer throws an exception when a
calculatis made and I don't understand why. Man, this stuff is so
entirely different than my PHP world ...but it has been challenging,
and I am learning bits and pieces ...here and there.

Well, you're adding RATIO+TIME to the conversions list. It's not
entirely clear what RATIO and TIME are, but my guess is that the sum
isn't an instance of the "conversions" class. You've made this rather
more confusing for yourself and others by using the identifier
"conversions" as both a type and a variable name, by the way.

Perhaps you meant to write

conversions.Add (new conversions(TIME, RATIO));

?
 
Back
Top