• Post Reply 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem on Arrays

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a method that creates and initializes a two-dimensional array of double. The size of the array is determined by the arguments of the method, and the initialization values are a range determined by beginning and ending values that are also arguments of the method. Create a second method that will print the array generated by the first method. In main() test the methods by creating and printing several different sizes of arrays.(This is the Exercise(no.20) of Chapter 4 of the book 'Thinking in Java' by Bruce Eckel)
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which part of the problem are you having trouble with?
 
Lalit Sha
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn,
I just haven't understood the problem. Kindly explain 'The size of the array is ........arguments of the method' from the problem.
Thanks,
Lalit.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to create a method
void doMethod(int sizeOfFirstDimension, int sizeOfSecondDimension, double beginningValueOfRange, double endValueOfRange){
//create the array and load it
}
It doesn't really say what pattern to use to fill the array, or what to do if the range provides to many or too few values for the array - so I guess you improvise.
Then you need to create the printArray method, and finally add a main to play with your methods.

[This message has been edited by Cindy Glass (edited January 20, 2001).]
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lalit,
I guess what it means is this:
The first method should create and initialize a 2d-Array. You probably know what this is. Further the method should take arguments to determine the size(s) of the array (that means both dimensions, so you need two ints for that), and also arguments to determine the values in the Array.
This perhaps sounds more tricky as you are free to choose in which way to determine the exact values, the exercise only states that they should be derived from 'beginning and ending values that are also arguments of the method'. But since the array probably isn't meant to contain values that should be useful for any purpose, I would have chosen the most straightforward and easiest way to do this:
The exercise states that your method should take parameters for:
1)The size of the Array.
As this is a 2d Array that means one int for each dimension, which inside your method is used when initializing the actual array, like: myArr=new double[x][y]; where x and y are the parameters for each of the dim's. (myArr is of course declared globally in your class as double[][] myArr; so that it still exists after your method is finished)
2)'beginning and ending values that are also arguments of the method'
Now you are supposed to fill your array with some crappy values, which values probably don't matter, but you should specify the range with beginning and ending values. Here you have an awful lot of choices, but let's keep it simple and stupid.
For instance you can decide that your array shold take the range arguments, calculate the actual range, then insert values spread evenly across the range into your array.
( Note: the Ex. does not say how many range arguments; theoretically, you can use from one (setting the other end of the range to some default value, for instance zero) to, say one million, arguments, employing some logic in your array to calculate the actual range. Practically, however, your method would probably work best with two (the simplest) or four (it's 2d, remember) range args.)
Example:
2 size args/2 range args:
Let your method take two size args and two range args
( myMethod(int width,int height,int sval, int eval) ), and let's say on execution the user enters 5,10,3,200.
That would give you an Array of 5 'cells' one direction and 10 cells in the other, that's altogether 50 cells. To fill these with (double) values, you could take the range args, 3 and 200,
giving an actual range of 197, divide the range 197 on the number of cells to fill, 50, that would give you a double value close to 4 (which we can call the increment value). Use a double value (a local variable) as a counter, starting on the beginning value, running in a loop an amount of times equal to the number of cells you will fill (50). On each iteration of the loop, add the increment value to the counter, and assign the value of the counter to the corresponding cell. In this way, your array will be filled with values evenly distributed along the range specified. With four range values, you could impose rules on how the values should be distributed i.e. per 'column' of your 2d Array.
The actual programming of this, I assume you want to do yourself...an exercise, right?
Good luck!
Marius
 
Lalit Sha
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy, Marius,
Thanks. I have understood the problem. Now I can do it.
Lalit.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic