Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Converting Multidimensional array into 1D array  RSS feed

 
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 \.
 
Marshal
Posts: 6008
415
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).
 
author
Sheriff
Posts: 23586
138
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
Marshal
Posts: 6008
415
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?
 
Marshal
Posts: 60083
188
  • 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.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!