# Help with Java coding problem!

• June 4th, 2012, 07:14 AM
eyesackery
Help with Java coding problem!
This is a problem I have been struggling with, think I'm almost there with it just need a nudge in the right direction. I'm not looking for the answer, just some 'hints' :)

The Problem
////////////////////////////// PROBLEM STATEMENT //////////////////////////////
// Given an array of ints of any length, print an array with the elements //
// "rotated left" by the specified amount. Use methods for array input, //
// rotating the array and printing the array. //
// {1, 2, 3} 1 -> {2, 3, 1} //
// {5, 11, 9} 2 -> {9, 5, 11} //
// {7, 0, 0} 3 -> {7, 0, 0} //
///////////////////////////////////////////////////////////////////////////////

I have all my methods set up, just not sure how to write the code for the rotateArray(); method.

So far I've tried swapping each element in the array within a decreasing for loop, but I having trouble with the front element that has to be carried to the end of the array.

(In the problem statement - {1, 2, 3} is the actual given array and the number following is how many times it must be 'rotated left'. Then the second set of curly braces is the expected result.
• June 4th, 2012, 07:25 AM
KevinWorkman
Re: Help with Java coding problem!
How would you do this by hand, with a piece of paper and a pencil instead of a computer? Pretend you are given a deck of cards, and you're supposed to implement this algorithm on it. How do you do it?
• June 4th, 2012, 07:59 AM
eyesackery
Re: Help with Java coding problem!
Wow, great advice. The physical deck of cards made me visualize the problem a lot better.

This is how I plan to solve this problem.

I will create a loop that calls the rotateLeft(); method 'n' amount of times.

Within the rotateLeft() method I will store element 0 into a temp variable then swap each element going up the array until I reach array.length - 1, where I will place the int stored in my temp variable.

• June 4th, 2012, 08:11 AM
eyesackery
Re: Help with Java coding problem!
Quote:

Originally Posted by KevinWorkman
How would you do this by hand, with a piece of paper and a pencil instead of a computer? Pretend you are given a deck of cards, and you're supposed to implement this algorithm on it. How do you do it?

import java.util.*; public class H1D4 {public static void main (String[] args) {while (JPL.test()) {

////////////////////////////// PROBLEM STATEMENT //////////////////////////////
// Given an array of ints of any length, print an array with the elements //
// "rotated left" by the specified amount. Use methods for array input, //
// rotating the array and printing the array. //
// {1, 2, 3} 1 -> {2, 3, 1} //
// {5, 11, 9} 2 -> {9, 5, 11} //
// {7, 0, 0} 3 -> {7, 0, 0} //
///////////////////////////////////////////////////////////////////////////////

// >>>>>> Your Java Code Fragment starts here <<<<<<
Scanner keyboard = new Scanner(System.in);
int[] mainArray = getArray(keyboard);
int[] result = rotateArray(mainArray, keyboard);
printArray(result);
// >>>>>> Your Java Code Fragment ends here <<<<<<
}}

// >>>>>> Your Java Methods start here <<<<<<
static int[] getArray(Scanner kb)
{
int n = kb.nextInt();
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
a[i] = kb.nextInt();
return a;
}

static int[] rotateArray(int[] a, Scanner kb)
{
int n = kb.nextInt();
for (int k = n; k > 0; k--)
{
int lastChar = a[0];
for (int i = 0; i < a.length -1 ; i++)
{
a[i] = a[i+1];
}
a[a.length - 1] = lastChar;
}
return a;
}

static void printArray(int[] a)
{
for (int i = 0; i < a.length; i++)
System.out.print(a[i]);
}

// >>>>>> Your Java Methods end here <<<<<<
}

Thank you very much for your help, this algorithm solved the problem. Simple after I had visualized it with the deck of cards, what great advice! :)
• June 4th, 2012, 08:21 AM
KevinWorkman
Re: Help with Java coding problem!
Quote:

Originally Posted by eyesackery
Thank you very much for your help, this algorithm solved the problem. Simple after I had visualized it with the deck of cards, what great advice! :)

No problem. A big part of programming is actually taking a step back and thinking about the problem without a computer. It's hard to figure out how to write a program that solves the problem if you don't know how you would solve the problem in the first place. And a lot of problems have solutions that are like "I don't know, I just do it in my head!", so thinking about a concrete instance of the problem helps think about what your brain is actually doing to solve the problem.