SRL Archive - Help and Support

Help me with my University Coursework!


Sp0rky

2009-05-01 17:38:22

O hi, well, basically, i need some help, and I think the good people at srl will be able to help me =D

It's in c#, but i really just need help with the concept.

I'm not used to working with 2d arrays.

Here is the question:


Or

http://img100.imageshack.us/img100/253/helllp.jpg



Basically, I got the initial concept, I'll create a 2d array, which is a table of the values, but I don't really know how to lookup, and my idea of the problem I think is also screwed.

Anyone mind helping? Any kind of example code (in delphi, scar, c#, anything) just to get my head around it, along with an explanation, would be fantastttiiccc.

This probably seems really easy to a lot of you, and it would be to me if I could see through the problem, but am not used to working with 2d arrays.

~Sp0rky

HyperSecret

2009-05-01 17:54:48

Most a 2 dimensional array would be like storing a value at a certain row and column. Your first [] is your rows and the second [] is your column.

example: cities[rows][columns];

then you would have to loop through using 2 for loops and set a value at each location.


for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; i++) {
cities[i][j] = distance;
}
}

You would also need to know which point in the array corresponds to which cities you are traveling between. That is something you would have to determine (make yourself a little diagram that cities[0][0] = here to here, cities[0][1] = here to here …. as a side note type of thing).

The drop down menus i really can't help you with. I can be more thorough if need and also if you want i can write what i did above in SCAR if needed. (I wrote it in c++ above).

If c# language is closely coded to c++ I could try to code the part I can do (not the drop down menu stuff but the array and looking up of distances) in c++ after I get off work and you could try to convert it to c#. I could also do it in SCAR I guess if I had to.

Sp0rky

2009-05-01 17:57:20

From: HyperSecret
Most a 2 dimensional array would be like storing a value at a certain row and column. Your first [] is your rows and the second [] is your column.

example: cities[rows][columns];

then you would have to loop through using 2 for loops and set a value at each location.


for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; i++) {
cities[i][j] = distance;
}
}

You would also need to know which point in the array corresponds to which cities you are traveling between. That is something you would have to determine (make yourself a little diagram that cities[0][0] = here to here, cities[0][1] = here to here …. as a side note type of thing).

The drop down menus i really can't help you with. I can be more thorough if need and also if you want i can write what i did above in SCAR if needed. (I wrote it in c++ above).


Why aren't you on msn? =D

Yeah i really do need help with this one, dont understand it still.

HyperSecret

2009-05-01 17:58:34

From: Sp0rky
Why aren't you on msn? =D

Yeah i really do need help with this one, dont understand it still.


I am at work :). Give me a little bit I will try to piece the basic concept of it all together.

Sp0rky

2009-05-01 18:21:12

Well, this is what i have so far:


int[,] cities = new int[,]
{
{0, 0},
{1, 0},
{2, 189},
{3, 75},
{4, 196},
{5, 114},
{6, 209},
{7, 70},
{8, 164},
{9, 217}
};

That's trying to set the array up. Is that completely wrong? 0.o

This is due in in 9 hours by the way :x

HyperSecret

2009-05-01 18:28:50


/**
* [0][0] = Bristol - Bristol (0)
* [0][1] = Bristol - Dover (189)
* [0][2] = Bristol - Exeter (75)
* [0][3] = Bristol - Leeds (196)
* [0][4] = Bristol - London (114)
* ....
*/

const int ROWS = 9;
const int COLUMNS = 9;
int *dCities = new int[ROWS][COLUMNS]; //dynamic array in c++
//Use one or the other
int dCities[ROWS][COLUMNS]; //non dynamic array

for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLUMNS; j++) {
dCities[i][j] = (some distance);
}
}

So what this is doing I will explain. As you can see up top I started to show you some of the possible routes that you are wanting to determine. In the multi-dimensional array location [0][0] is going to hold the distance of Bristol to Bristol, location [0][1] is going to hold distance of Bristol to Dover, location [0][2] location of Bristol to Exeter, etc… after you go through the first 9 columns of the first row (for loop with variable j) then variable i in the first for loop will increment to start filling in row to columns 1 through 9. It will continue to do that until you have looped through all 9 rows (for loop variable i) and all 9 columns (for loop variable j).

All that is doing it storing a distance at a certain location in the 2dimensional array. For getting to them it would just be if then statements or a couple of case loops whichever you feel more comfortable with I guess.

Now as for getting the distance in the (some distance) part will be up to you how you do that. If I were doing it in c++ I would store those distances into a txt file and have a function read that text file and grab the appropriate distance that I wanted. I am not familiar with c# so I wouldn't know how I would do it.

Sp0rky

2009-05-01 18:45:40


const int ROWS = 9;
const int COLUMNS = 9;
int[,] dCities = new int[ROWS, COLUMNS]; //non dynamic array

for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLUMNS; j++)
{
dCities[i, j] = (some distance);
}
}

So that populates the array? and loops until it's all populated? With "some distance" being the distance it's being populated with?

If you could make your solution in c++, it would really help me to understand. A lot. Really appreciate your help so far by the way. I love you :p

HyperSecret

2009-05-01 18:51:02

Yes that so far just populates the array.

Ok I will start to piece things together. I will work on getting the distances next. I will post as I complete certain things.

Sp0rky

2009-05-01 18:53:42

From: HyperSecret
Yes that so far just populates the array.

Ok I will start to piece things together. I will work on getting the distances next. I will post as I complete certain things.


I fuckin love you so much :)

Serioously owe you big time for this.


MrPrime says:
ur const for the row and columns should be 10


Is that correct? =o

HyperSecret

2009-05-01 19:12:09


/**
* [0][0] = Bristol - Bristol (0)
* [0][1] = Bristol - Dover (189)
* [0][2] = Bristol - Exeter (75)
* [0][3] = Bristol - Leeds (196)
* [0][4] = Bristol - London (114)
* ....
*/

const int ROWS = 9;
const int COLUMNS = 9;

int tempSlot;
int distances[37]={0, 189, 75, 196, 114, 209, 70, 164, 217, 203, 257,
74, 156, 129, 229, 264, 271, 170, 282, 141, 240,
292, 191, 174, 164, 34, 25, 112, 56, 160, 194,
141, 146, 181, 130, 174, 53};

int dCities[ROWS][COLUMNS]; //non dynamic array

for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLUMNS; j++) {

if(i < j) {
tempSlot = (j - i);
} else if(j < i) {
tempSlot = (i - j);
} else {
tempSlot = 0;
}

dCities[i][j] = distances[tempSlot];
}
}

In my little scaled down theory of your chart (4x4 section) my arithemtic worked out to getting the correct spot for the corresponding distances in the distances array.

Output a couple of the array slots and see if they output the correct distances.

cout << dCities[0][0] << endl;

or in the for loop

cout << dCities[i][j] << endl;

cout in c++ is like a writeln in scar. so whatever that is in c#.

Let me know if they do or don't get the correct distances.

Btw, the distances in the array are sorted as follows. first 0 distances then, then row bristol to all, then starting at the empty space row Dover then down, then starting at empty space row Exeter and down, etc… starting at teh empty space and down for each row.



EDIT: in C++ i intialize the array to start at 0 and there are 9 rows/colums so… (0,1,2,3,4,5,6,7,8) is 9 units. so we set it to 9 so that the loops are i = 0 while i < ROWS/COLUMNS (9) do the loop….so it fills all 9 correctly. if you did 10 you would have an empty set in the 2d array.

This would all be empty:

dCities [9][0]
dCities [9][1]
dCities [9][2]
dCities [9][3]
etc…

Also so would be:

dCities[0][9]
dCities[1][9]
dCities[2][9]
dCities[3][9]
etc…
I am correct ;).

Also fixed how the array appears in the code box.

HyperSecret

2009-05-01 19:36:11

It actually only works for the first 9 slots I believe. I am trying to re-work the arithemtic. You can work on it too and let me know if you get it. ;)

Sp0rky

2009-05-01 19:42:21

O shi =o

Very nice. Means i actually might be able to get this finished before the evening and go out and party with my girlfriend ^_^

Ok, so, heres what I've got on my lookup button:


private void btnLookup_Click(object sender, EventArgs e)
{
if (listBoxDeparture.Text == "Bristol")
{
d1 = 0;
}
if (listBoxDeparture.Text == "Dover")
{
d1 = 1;
}
if (listBoxDeparture.Text == "Exeter")
{
d1 = 2;
}
if (listBoxDeparture.Text == "Leeds")
{
d1 = 3;
}
if (listBoxDeparture.Text == "London")
{
d1 = 4;
}
if (listBoxDeparture.Text == "Norwich")
{
d1 = 5;
}
if (listBoxDeparture.Text == "Oxford")
{
d1 = 6;
}
if (listBoxDeparture.Text == "Sheffield")
{
d1 = 7;
}
if (listBoxDeparture.Text == "York")
{
d1 = 8;
}

if (listBoxDestination.Text == "Bristol")
{
d2 = 0;
}
if (listBoxDestination.Text == "Dover")
{
d2 = 1;
}
if (listBoxDestination.Text == "Exeter")
{
d2 = 2;
}
if (listBoxDestination.Text == "Leeds")
{
d2 = 3;
}
if (listBoxDestination.Text == "London")
{
d2 = 4;
}
if (listBoxDestination.Text == "Norwich")
{
d2 = 5;
}
if (listBoxDestination.Text == "Oxford")
{
d2 = 6;
}
if (listBoxDestination.Text == "Sheffield")
{
d2 = 7;
}
if (listBoxDestination.Text == "York")
{
d2 = 8;
}

lblResult.Text = Convert.ToString((dCities[d1, d2]));
}


}

But it always returns 0…. >_>

Help! ;(

whereas if i replace


lblResult.Text = Convert.ToString((dCities[d1, d2]));

with raw numbers, such as


lblResult.Text = Convert.ToString((dCities[0, 1]));

it displays the correct result 0.o

HyperSecret

2009-05-01 19:57:52

Hmm, that is wierd. It should never return 0 unless the spot is on the diagnol. But try like

dCities[9, 7]

tell me what that outputs

(this would be a lot easier if I had a compiler, instead of wordpad :rolleyes: )

Sp0rky

2009-05-01 20:08:29

From: HyperSecret
Hmm, that is wierd. It should never return 0 unless the spot is on the diagnol. But try like

dCities[9, 7]

tell me what that outputs

(this would be a lot easier if I had a compiler, instead of wordpad :rolleyes: )


Kk, debuged it, it was because i was using the wrong component, and the .text property was just remaining empty every time (hence the 0 result).

Now I've got it all working, but Anything other than Bristol in the first box seems to return a wrong value, i attached the exe for you ^_^

HyperSecret

2009-05-01 20:11:19

I am working on that problem right now. It is in the for loop, the test example I used only had 9 values so everything worked perfect but once you exceed the first 9 values shit gets messed up.

EDIT:There is an easy way out but that makes it so you have a 81 sized distances array, which would mean just put every value in order in the array and have the loops just set it to the corresponding slot.

I am trying to get it to some way mathematically do it so that the array isn't so big and makes you look more leet :).

Sp0rky

2009-05-01 20:21:39

From: HyperSecret
I am working on that problem right now. It is in the for loop, the test example I used only had 9 values so everything worked perfect but once you exceed the first 9 values shit gets messed up.

EDIT:There is an easy way out but that makes it so you have a 81 sized distances array, which would mean just put every value in order in the array and have the loops just set it to the corresponding slot.

I am trying to get it to some way mathematically do it so that the array isn't so big and makes you look more leet :).


Rofl =D

You're too epic. I really don't mind the larger array, a working solution will make me happy.

HyperSecret

2009-05-01 20:32:09


const int ROWS = 10;
const int COLUMNS = 10;

int distances[81]={0, 189, 75, 196, 114, 209, 70, 164, 217
189, 0, 203, 257, 74, 156, 129, 229, 264,
75, 203, 0, 271, 170, 282, 141, 240, 292,
196, 257, 271, 0, 191, 174, 164, 34, 25,
114, 74, 170, 191, 0, 112, 56, 160, 194,
209, 156, 282, 174, 112, 0, 141, 146, 181,
70, 129, 141, 164, 56, 141, 0, 130, 174,
164, 229, 240, 34, 160, 146, 130, 0, 53,
217, 264, 292, 25, 194, 181, 174, 53, 0};

int dCities[ROWS][COLUMNS]; //non dynamic array

for(int i = 1; i < ROWS; i++) {
for(int j = 1; j < COLUMNS; j++) {
dCities[i][j] = distances[i * j];
}
}

That should work then. No more if then statements in the for loops and no more tempSlot variable.

Since I am at work, I gotta leave the computer for a little while. I will keep working on the mathematics of it to see if anything is possible on paper. If I come up with a different solution, I will post it.


The REAL question is though. Do you understand how the multi-dimensional array is working now? How it is getting filled and how you are able to access each slot?

EDIT: IMPORTANT!!!

I also just realized that if you start the for loops at 0 you will get a 0 distance for the whole first column. So you need to change the constants of ROWS and COLUMNS to 10 and start your loops at i = 1 and j = 1.

Should work just fine then.

Sp0rky

2009-05-01 20:47:07

Ok, now, with all your fixes, I now get


for (int i = 1; i < ROWS; i++)
{
for (int j = 1; j < COLUMNS; j++)
{
dCities[i, j] = distances[i * j]; <--- Index out of bounds of the array ;(
}
}
}

HyperSecret

2009-05-01 21:11:03

(i * j) - 1


oops sorry :). Cmon sp0rky you should have caught that one, cmon haha.

Sp0rky

2009-05-01 21:21:43

From: HyperSecret
(i * j) - 1


oops sorry :). Cmon sp0rky you should have caught that one, cmon haha.


Haha, soz, really not at all in programming mood today, just wana get this over with.


Edit: Compiles with -1, just still doesnt give the correct values.

ShowerThoughts

2009-05-01 21:37:24

Hey, I code C# to and I think I can help you too!
Always a nice talk.
And I love a school or uni which actually does something with the language I code :).

Hermen Otter @ Live . nl
add meh.

ShowerThoughts

2009-05-01 22:28:33

From: HyperSecret

/**
* [0][0] = Bristol - Bristol (0)
* [0][1] = Bristol - Dover (189)
* [0][2] = Bristol - Exeter (75)
* [0][3] = Bristol - Leeds (196)
* [0][4] = Bristol - London (114)
* ....
*/

const int ROWS = 9;
const int COLUMNS = 9;
int *dCities = new int[ROWS][COLUMNS]; //dynamic array in c++
//Use one or the other
int dCities[ROWS][COLUMNS]; //non dynamic array

for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLUMNS; j++) {
dCities[i][j] = (some distance);
}
}

So what this is doing I will explain. As you can see up top I started to show you some of the possible routes that you are wanting to determine. In the multi-dimensional array location [0][0] is going to hold the distance of Bristol to Bristol, location [0][1] is going to hold distance of Bristol to Dover, location [0][2] location of Bristol to Exeter, etc… after you go through the first 9 columns of the first row (for loop with variable j) then variable i in the first for loop will increment to start filling in row to columns 1 through 9. It will continue to do that until you have looped through all 9 rows (for loop variable i) and all 9 columns (for loop variable j).

All that is doing it storing a distance at a certain location in the 2dimensional array. For getting to them it would just be if then statements or a couple of case loops whichever you feel more comfortable with I guess.

Now as for getting the distance in the (some distance) part will be up to you how you do that. If I were doing it in c++ I would store those distances into a txt file and have a function read that text file and grab the appropriate distance that I wanted. I am not familiar with c# so I wouldn't know how I would do it.


Won't work, since:

1 * 4 = 4, distance = 196 (distance = number, that is what it is supposed to be same for below.)
2 * 2 = 4, distance = 0
so formula is not working
but it will give the same if you use tah formula ;)
get my point?

I got a simple .Ini class :)

Sp0rky

2009-05-01 22:42:59

Had to go for a simple option where i populated the array myself manually ;( will get less marks but at least it works. thanks for the help guys.

HyperSecret

2009-05-01 22:56:38

From: Sp0rky
Haha, soz, really not at all in programming mood today, just wana get this over with.


Edit: Compiles with -1, just still doesnt give the correct values.


Sorry man. Like I said I don't have a compiler or code to actually execute so it is a little difficult for me to throw out stuff to test without being able to see it.

From: Hermpie
Won't work, since:

1 * 4 = 4, distance = 196 (distance = number, that is what it is supposed to be same for below.)
2 * 2 = 4, distance = 0
so formula is not working
but it will give the same if you use tah formula ;)
get my point?

I got a simple .Ini class :)


Yea, I see that now. I wasn't fully thinking I guess :rolleyes:.


Btw. Once I get done working out tonight, I will get this to work. Even though you don't need it, you got me wanting to figure it out, lol.

ShowerThoughts

2009-05-01 23:02:33

He already submitted it, and now he will not pass because of you! :p

R0b0t1

2009-05-02 19:41:02

If the origin is 0, then 1 on either axis would be "Bristol", 2 on either axis for "Dover", etc. Convert all of the names to numbers this way, and if you store the distances in a 2d array w/ the cell (0,0) blank, you should be able to look them up.

C# is bad for your health.

ShowerThoughts

2009-05-02 21:04:04

We were already doing that, like 1,1 bristol to bristol
Like I already said if you go from Bristol to the fourth(4)
and you do 4 * 1 - 1(because it's an index)
and if you do 2 * 2 from Dover to Dover(I guess)
from Dover to Dover = 0 distance
4 *1 > 0 distance :)
Fail.