This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 6362
437
BSD Linux Mac OS X VI Editor
  • 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
Posts: 23811
140
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: 6362
437
BSD Linux Mac OS X VI Editor
  • 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: 62223
193
  • 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.
 
Did you just should on me? You should read this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!