# arrays

Discussion in 'Java' started by iVan on CSC 101, Feb 16, 2007.

1. ### iVan on CSC 101Guest

hi to all!....I'm really having having problms with this array
thing...our instructor just gave us 15 pages copy all about Arrays and
of course problms about it...I have 5 problems and really don't know
how to solve these.....geezzz.....they suck!...=D...hhmmmm...anyway
i'm here for any help from you guys...

beginners like us really need ( I think) to have lectures by those who
should do it but I dunnot know why they're just giving us this stuff
without explanations...hhhhmmm....

nyways, here are the problems about Arrays....

1. Longest plateau. Given an array of integers, find the length and
location of the longest contiguous sequence of
equal values.

2. Bad shuffling. Suppose that you choose a random integer between 0
and N-1 in Shuffle.java instead of 0 and i.
Show that the resulting order is not equally likely to be one ot
the N! possibilities, even whenn N-3. Assuming
the elements are named A, B, and C, verify that all 6 outcomes are
possible, but that theu occur with the
following baised probabilities.

ABC ACB BAC BCA CAB CBA
4/27 5/27 6/27 4/27
5/27 3/27

schedule. Each task takes 1 unit of time and has
a deadling by which time it is expected to finish. If a task is
not completed by its deadline, you pay a \$1,000
fine. Find a schedule that minimizes the penalty. Hint: schedule

4. Magic squares. Write a program MagicSquare.java that reads in an
odd integer N form the command line and prints our an N-by-N magic
square. the square contains each of the integers between 1 and N^2
exactly once, such that all row sums, column sums, and diagonal sums
are equal.

4 9 2 11 18 25 2 9
3 5 7 10 12 19 21 3
8 1 6 4 6 13 20 22
12 5 7 14 16
17 24 1 8 15

lastly,
5. US postal barcodes. The POSTNET barcode is used by the US Postal
System to route mail. Each decimal digit in the zip code is encoded
using a sequence of 5 short lines for use by scanners as follows:

Value Encoding
0 I I i i i
1 i i i I I
2 i i I i I
3 i i I I i
4 i I i i I
5 i I i I i
6 i I I i i
7 I i i i I
8 I i i I i
9 I i I i i

i = are small bar lines
I = are the larger ones

response.....badly needing it for our midterms....

you could also email me at ....thanks a
lot!...... ^ ^

God bless dudes!!!!

iVan on CSC 101, Feb 16, 2007

2. ### Daniel PittsGuest

On Feb 15, 4:52 pm, "iVan on CSC 101" <>
wrote:
> hi to all!....I'm really having having problms with this array
> thing...our instructor just gave us 15 pages copy all about Arrays and
> of course problms about it...I have 5 problems and really don't know
> how to solve these.....geezzz.....they suck!...=D...hhmmmm...anyway
> i'm here for any help from you guys...
>
> beginners like us really need ( I think) to have lectures by those who
> should do it but I dunnot know why they're just giving us this stuff
> without explanations...hhhhmmm....
>
> nyways, here are the problems about Arrays....
>
> 1. Longest plateau. Given an array of integers, find the length and
> location of the longest contiguous sequence of
> equal values.
>
> 2. Bad shuffling. Suppose that you choose a random integer between 0
> and N-1 in Shuffle.java instead of 0 and i.
> Show that the resulting order is not equally likely to be one ot
> the N! possibilities, even whenn N-3. Assuming
> the elements are named A, B, and C, verify that all 6 outcomes are
> possible, but that theu occur with the
> following baised probabilities.
>
> ABC ACB BAC BCA CAB CBA
> 4/27 5/27 6/27 4/27
> 5/27 3/27
>
> 3. Scheduling with deadline. Suppose that you have N tasks to
> schedule. Each task takes 1 unit of time and has
> a deadling by which time it is expected to finish. If a task is
> not completed by its deadline, you pay a \$1,000
> fine. Find a schedule that minimizes the penalty. Hint: schedule
>
> 4. Magic squares. Write a program MagicSquare.java that reads in an
> odd integer N form the command line and prints our an N-by-N magic
> square. the square contains each of the integers between 1 and N^2
> exactly once, such that all row sums, column sums, and diagonal sums
> are equal.
>
> 4 9 2 11 18 25 2 9
> 3 5 7 10 12 19 21 3
> 8 1 6 4 6 13 20 22
> 12 5 7 14 16
> 17 24 1 8 15
>
> lastly,
> 5. US postal barcodes. The POSTNET barcode is used by the US Postal
> System to route mail. Each decimal digit in the zip code is encoded
> using a sequence of 5 short lines for use by scanners as follows:
>
> Value Encoding
> 0 I I i i i
> 1 i i i I I
> 2 i i I i I
> 3 i i I I i
> 4 i I i i I
> 5 i I i I i
> 6 i I I i i
> 7 I i i i I
> 8 I i i I i
> 9 I i I i i
>
> i = are small bar lines
> I = are the larger ones
>
> response.....badly needing it for our midterms....
>
> you could also email me at ....thanks a
> lot!...... ^ ^
>
> God bless dudes!!!!

Consider how much time you spent copying the description of your
homework assignment. Then consider how much you learned by copying it
over.
Next, consider how much time you would have saved by at least trying
some of the assignment yourself before posting. Also, consider how
much more you would have learned by trying.

After you consider that, make at least an ATTEMPT at it, tell us whats

understand it, feel free to pay someone, but don't bother applying for
a job as a programmer.

Daniel Pitts, Feb 16, 2007

3. ### Mark RafnGuest

iVan on CSC 101 <> wrote:
>hi to all!....I'm really having having problms with this array
>thing...our instructor just gave us 15 pages copy all about Arrays and
>of course problms about it...I have 5 problems and really don't know
>how to solve these.....geezzz.....they suck!...=D...hhmmmm...anyway
>i'm here for any help from you guys...

Thanks for honesty about asking homework questions. You may even get some
help here, _IF_ you show that you've thought about the problem and are stuck
on a specific thing, rather than not knowing where to start and being too lazy
to try.

>beginners like us really need ( I think) to have lectures by those who
>should do it but I dunnot know why they're just giving us this stuff
>without explanations...hhhhmmm....

There's LOTS of books, and usually instructors are available to answer
questions if you don't understand a problem.

>1. Longest plateau. Given an array of integers, find the length and
>location of the longest contiguous sequence of
> equal values.

I give this as an interview question sometimes. It's a good quickie to make
sure the candidate can think somewhat clearly about a problem and handle basic
coding.

I don't like this as an interview question (though something similar is not
uncommon, I don't think it's that diagnostic). It's a pretty decent homework
problem, though.

Interesting. Haven't seen this one, but I kind of like it. He shouldn't have
given the hint, though.

>4. Magic squares.

Boring!

> 5. US postal barcodes.

I like having some real-world reference in the problem. Bonus points if you
can figure out why there are 5 bars when 4 would do.

>thank you so much for reading dude!

Thanks you for posting! I look forward to hearing how you do!
--
Mark Rafn <http://www.dagon.net/>

Mark Rafn, Feb 16, 2007
4. ### LewGuest

Mark Rafn wrote:
iVan on CSC 101 <> wrote:
>> 1. Longest plateau. Given an array of integers, find the length and
>> location of the longest contiguous sequence of
>> equal values.

Have you learned loops, 'while', 'for' and 'do'?

Let's backtrack. I will conclude from your choice of newsgroup that this is a
Java assignment. You should know how to put together a Java class, for
example, suppose your command line is parked at your project source directory.

\$ cat homework/QuestionOne.java
package homework;
public class QuestionOne
{
}

\$

I like to show this as the file name above dashes, contents beneath:

homework/QuestionOne.java
--------------------------------
package homework;
/** Solution to Question One.
*/
public class QuestionOne
{
/** Find and print the longest plateau
* in an <code>int</code> array.
* "plateau" definition per Prof. Genius.
* @param ins <code>int []</code> to analyze.
*/
public void showLongestPlateau( int [] ins )
{
for ( int value : ins )
{
}
System.out.println( "And the answer is: " + 17 );
}

}

Outside the loop you should declare some variables to hold the length and
location of the plateau. Perhaps you should define a class to hold a pair of
values for the length and location. You would use another pair of values (or
an instance of your Pair class) to keep track of the current candidate for
longest Plateau. Each time the next value in the array is different from the
one you are tracking (is that a different variable?) you wrap up the length
calculation and compare to the current longest candidate. A longer result is
popped in to the "longest" variable(s), reinitialize your tracking
variable(s), and start processing the new element.

Thought.

Thought is the key - think about the process of going through the array: what
would you need to know to tell if you are in a plateau? Where would you store
informtion about its length? Are there sub-parts to each loop iteration? What
/must/ be true at the start and end of each loop iteration? What if there is
more than one plateau of the longest length? Less than one?

If you can write down a precise description of the process, which named items
you must keep track of, how to change each one, and precisely what happens to
them - then you can translate that into Java and Bob's your uncle.

- Lew

Lew, Feb 16, 2007
5. ### Patricia ShanahanGuest

iVan on CSC 101 wrote:
> hi to all!....I'm really having having problms with this array
> thing...our instructor just gave us 15 pages copy all about Arrays and
> of course problms about it...I have 5 problems and really don't know
> how to solve these.....geezzz.....they suck!...=D...hhmmmm...anyway
> i'm here for any help from you guys...

....

Have you done some easier array exercises? If not, I suggest doing so.

These look to me like very good exercises for people who basically know
how to use Java arrays, and need to merge arrays with algorithm
thinking. They seem a bit too complicated for first array programs.

Patricia

Patricia Shanahan, Feb 16, 2007
6. ### Alex HunsleyGuest

iVan on CSC 101 wrote:
> hi to all!....I'm really having having problms with this array
> thing...our instructor just gave us 15 pages copy all about Arrays and
> of course problms about it...I have 5 problems and really don't know
> how to solve these.....geezzz.....they suck!...=D...hhmmmm...anyway
> i'm here for any help from you guys...
>
> beginners like us really need ( I think) to have lectures by those who
> should do it but I dunnot know why they're just giving us this stuff
> without explanations...hhhhmmm....

Remember that 15 pages about arrays etc. you said you were given in the
first paragraph? I presume that's the 'explanations' that you're
complaining about not having. Can you read? It looks like you can...

>
> nyways, here are the problems about Arrays....

Have you even attempted the problems? Or did you expect to just be able
to pop into a homework help desk (which this place *isn't*) and just get

> response.....badly needing it for our midterms....

If you need it badly, then why not make a start on the problems, or ask
specific questions?

> you could also email me at ....thanks a
> lot!...... ^ ^

No, newsgroups work best when answers come here, not to personal emails.

Alex Hunsley, Feb 16, 2007