If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Load user input into 10x4 rectangular array

Ok, I am working on a form that calculates the future value of an investment. I need to add a 10x4 rectangular array (required) and then display the results when the user clicks exit. I am having trouble figuring out how to load the user input into the array. Here's what I have so far:

int numberOfRows = calculations.GetLength(0); is also giving me an error "A field initializer cannot reference the non-static field, method, or property 'FutureValue.form1.calculations'
Do I need to assign values to the array first?

I already coded the message box at the end, I just mainly need to know how to enter the user input into the array. Thanks for your help.

I am not completely sure if that answered your question but if not I am sure one of the more knowledgeable members will come in and help you shortly. If not when I get a chance later I will try to help out with more detail.

Re: Load user input into 10x4 rectangular array

2 Dimensional arrays are not much more complicated than single element arrays. You populate them in a similar manner. In your code I do not see any attempt at populating the array though. I see in on exit you make an attempt to display it through a nested loop but there is no information saved into the array to be displayed.

2 Dimensional array

Code:

[2,2]
[0,0] [0,1]
[1,0] [1,1]

So you know you need to populate the array. If you are reading data from a file it is simple since you know the array will be fully populated and can use a nested loop to store the information into the array. Now from the looks of it if I understand your code correctly you are attempting to store the value calculated into the array each time something is calculated. Then at the end when you exit you want to display all the calculations.

Well to store the data you will need to create some counters and some checks to make sure you are not out of bounds of the array. Something such as the following would work.

Simply put what the code will do is once the data is validated it will store the information into the array. Once saved it will check that the current column is still within the array bounds. If it is then it will add to the counter so it can save into the next position in the array. If it is higher than the maximum column then it checks if the rows are still within range. If it is same thing it will add to the row counter to move to the next row and reset the column counter. It will keep doing this until the array is filled in which case it will only replace the last position of the array.

Try integrating code that populates your array and post what errors you get.

Re: Load user input into 10x4 rectangular array

It would help to know which line the error occurred on (and I don't mean for you to answer with "line 46" .. i mean "if(currentColumn < calculations column maximum)" )

That said, on that line there I pasted..

if(currentColumn < calculations column maximum)

You cannot put spaces in a variable name, so tell me, what do you mean with this line of code? what is it supposed to do?

The biggest problem with your program as it stands is that you're going straight to coding without thinking about it first. You should write your algorithm and logic in plain english (or your chosen first language) first and then code after. Here's an example, for iterating over a 2D array:

Code:

//first we declare a 2d array
//then we need a for loop which will iterate over one dimension
//inside this for loop, another for loop iterates over the other dimension
//print out what we find at this array location

Right now, without guidance (comments) you're just coding by hope, it's frustrating and unrewarding. Write first what you mean to do, check that it's correct, then code what you wrote, then test it. If it doesnt work out first ask "where is the problem?" then ask "does the code do what I commented that it should?" then ask "is the logic of the comment correct?"

Somewhere along the way you'll find the mistake

-
Tip: a 2D array is just an array of arrays. You understand that an array is a list of values, like pages in a book.. page[1], page[227] ?
Well now that your book, and look at the library.. the library is a list of books, an array of arrays. I could say library[13,227] and it would mean "go to the 13th book, and get the 227th page"
Wanna make it 3 dimensions? Lets get all the libraries in the country.. allofthem[72,13,227] .. go to the 72nd library, get the 13th book, get page 227
Wanna make it 4 dimensions? Let's get all the countries of the world, and all their libraries, and all their books, and all their pages..

And so on

Actually, we find in programming that 2D arrays declared like array[,] arent as useful because every book has the same number of pages, every library has the same number of books.. You'll move on to using arrays like int[][] books = new int[10][]; -- this means "I know I have 10 books in my library, but I don't know right now how many pages each book has"
For that you need to do more code:
books[0] = new int[235]; //the first book as 235 pages
books[1] = new int[421]; //the second book has 421 pages

All youre doing with int[][] books = new int[10][]; is saying "I want an array that is 10 long, and it will hold in each slot, another array of unknown length"

But I digress.. back to your problem.. take the code you wrote and save it, then trash it and start a new project. WRITE COMMENTS FIRST. Even pro programmers do this after being on the job 15 years. It helps them think, and helps people who come along after see what they were thinking.

Re: Load user input into 10x4 rectangular array

Ok, so I have it mostly figured out now, thanks to everyone's help. I declared the array and am using the variable "r" to designate the rows. You can see in the code where I am loading each field into the array using "r" as the row. Stuck on how to increment r after each instance of the calculations, should be easy for me by now but just not getting it.

Re: Load user input into 10x4 rectangular array

In your Calculate method there is not need to create and initialize the variable again. You simply have to do r++ which is the equivalent to doing r = r + 1 or r += 1 they all do the same thing. Also like mentioned in my previous post you will want to make sure that r does not go past the array's size in this case it is 10. What happens when the user enters 10 different calculations and they decide to enter an 11th one?