Bookmark Topic Watch Topic
  • New Topic

Converting Multidimensional array into 1D array  RSS feed

 
Seth Lamb
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Report post to moderator


I am really close to finishing this program and my output is almost there, except it's only printing out the first half or so of the array. I have two for loops and two counters to determine the size of the array and then copy the multidimensional array's values into the 1D array, but it only prints out the first half of the array as such:

How many rows in the array? 4
How many columns in the array? 2

This 2D array contains:
35.23, 26.94,
99.48, 66.69,
7.31, 25.18,
64.53, 21.25,

Converted to a 1D array:
35.23, 26.94, 99.48, 66.69,

And yes, I realize that I should be formatting my Print statements with % but my instructor doesn't seem to care about it (he never taught it to us) so for the time being I am being stubborn and using \.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Report post to moderator
Lets assume it's correct up to line 56. Overcomplicated from line 59. Look for easier solution (I found it for myself).
Also, check Line 60 (your comment and actual taken action).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Seth Lamb wrote:
I am really close to finishing this program and my output is almost there, except it's only printing out the first half or so of the array. I have two for loops and two counters to determine the size of the array and then copy the multidimensional array's values into the 1D array, but it only prints out the first half of the array as such:


You iterate through each "row" of the "two dimensional" array and ...

1. Copy one row of the "2D" array into the "one dimensional" array ... looks good.
2. Calculate the new index of the "one dimensional" array ... looks good.

3. Print one value of the "one dimensional" array... huh?? You just copied a whole row, so how does printing one value represent the whole row the you just copied?
3b. To print the one value, you used the m index on the "one dimensional" array ... huh?? Isn't the m index used to dereference a row of the "2D" array? How can it now be applied to the "1D" array?

[EDIT] Oops. Didn't noticed that Liutauras had already answered with a hint (which is probably better as it makes the OP think it through). Sorry. Have a cow ...

Henry
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you, Henry

I still think, I didn't express myself clear enough after 3 times editing my post.

Seth Lamb, did you find where your mistake appeared?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't write such long lines; I have corrected that by moving the comments to lines by themselves. You don't need so many comments. We know that you are creating objects without having to be told that, so you can probably delete most of those comments.

The big problem that I can see is that you are trying to do everything in the main method. you should have one statement in the main method and everything else in different methods. You should have a method which creates your array, one to print it, etc., etc. Then you can test the methods separately and find which works and which doesn't.

And there is no need to be rude about your instructor.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!