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 10 1251
Mark Givens wrote: 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[i]);
HTH,
Michael
Mark Givens <si************@yahoo.com> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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
On Wed, 18 Aug 2004 08:35:51 +0200, Michael Kremser
<mk************@yahoo.de> wrote: Mark Givens wrote:
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[i]);
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
On Wed, 18 Aug 2004 08:02:09 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote: Mark Givens <si************@yahoo.com> wrote: 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.
Mark Givens <si************@yahoo.com> wrote: 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);
}
}
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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 ;
}
}
}
On Wed, 18 Aug 2004 12:15:55 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote: Mark Givens <si************@yahoo.com> wrote: 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); } }
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.
On Wed, 18 Aug 2004 23:55:01 GMT, Mark Givens
<si************@yahoo.com> wrote: 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 ; } }
}
On Wed, 18 Aug 2004 12:15:55 +0100, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Mark Givens <si************@yahoo.com> wrote: 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); } }
Mark Givens <si************@yahoo.com> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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;
}
}
On Thu, 19 Aug 2004 07:12:43 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote: Mark Givens <si************@yahoo.com> wrote: 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.
Mark Givens <si************@yahoo.com> wrote: 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));
?
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Canonical Latin |
last post by:
"Leor Zolman" <leor@bdsoft.com> wrote
> "Canonical Latin" <javaplus@hotmail.com> wrote:
>
> > ...
> >But I'm still curious as to the rational of having type
>...
|
by: Matteo Settenvini |
last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to...
|
by: JezB |
last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of
files that match one of 3 search patterns, so I need something like
DirectoryInfo ld = new DirectoryInfo(searchDir);...
|
by: Michel Rouzic |
last post by:
It's the first time I try using structs, and I'm getting confused with
it and can't make it work properly
I firstly define the structure by this : typedef struct { char *l1; int
*l2; int Nval; }...
|
by: Rob Griffiths |
last post by:
Can anyone explain to me the difference between an element type and a
component type?
In the java literature, arrays are said to have component types, whereas
collections from the Collections...
|
by: Rene Nyffenegger |
last post by:
Hello everyone.
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in...
|
by: Robert Bravery |
last post by:
Hi all,
Can some one show me how to achieve a cross product of arrays. So that if I
had two arrays (could be any number) with three elements in each (once again
could be any number) I would get:...
|
by: Doug_J_W |
last post by:
I have a Visual Basic (2005) project that contains around twenty
embedded text files as resources. The text files contain two columns
of real numbers that are separated by tab deliminator, and are...
|
by: mike3 |
last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.programmer.win32
since there's Windows material in here as well as questions related to
standard
C++. Not sure how that'd go over...
|
by: weaknessforcats |
last post by:
Arrays Revealed
Introduction
Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with
arrays and array syntax but is not clear on a )exactly how...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |