This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Simple Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Problem" Watch "Simple Problem" New topic
Author

Simple Problem

Meena Singh
Greenhorn

Joined: Dec 30, 2009
Posts: 2

Hi friends
I am new to Java and I am stuck up with two basic logic.
Can you please help me in explaining the logic behind them.

Problem No 1)

Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of the non-zero numbers does not matter. So {1, 0, 0, 1} becomes {0 ,0, 1, 1}. You may modify and return the given array or make a new array.

zero Front({1, 0, 0, 1}) → {0, 0, 1, 1}
zero Front({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1}
zero Front({1, 0}) → {0, 1}

Problem2)
Write a program to print the following on screen
1
1 1
1 1 1
1 1 1 1
1 1 1 1


I don't want anybody to tell the code only beacause that I can find it on net. But somebody please explain the main logic behind it.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

I advise you to change your id to a real name.
Though you are not asking for code, it seems you want us to do your homework for you,

I promise to help you if you mention the though process in your mind (even mathematically) to solve this problem.

Welcome to code ranch.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Meena Singh
Greenhorn

Joined: Dec 30, 2009
Posts: 2
Hi Salvin
I am not asking you to do my homework but it is because I have never been able to think at this level that is why I wanted somebody to explain me. Though 2nd problem is the first level towards what I want to achieve which is
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

though I got this code through internet but could not figure out the logic behind it so I started with the basic one to develop 1 111...
I am stuck as to I could not figure out logic behind this also.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Sweet Sweetie wrote:

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917



Logic 1:
{1, 0, 0, 1}

look at first element,
its a 0 ? no, do nothing,
look at second element,
its a 0 ? yes, move it to start ....

generalised form:

look at nth element, if its a 0 move it to start

iterations:
1. {1, 0, 0, 1}
2. {0, 1, 0, 1}
3. {0, 0, 1, 1}
4. {0, 0, 1, 1}


Logic 2:



first time i print "1" one time
second time i print "1" two times...
nth time, i print "1" n times


Meena Singh wrote:Hi Salvin
I am not asking you to do my homework but it is because I have never been able to think at this level that is why I wanted somebody to explain me. Though 2nd problem is the first level towards what I want to achieve which is


though I got this code through internet but could not figure out the logic behind it so I started with the basic one to develop 1 111...
I am stuck as to I could not figure out logic behind this also.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

have you cracked it in code ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Thank you for correcting the naming error. Please have a look at the title of this thread, which doesn't inspire answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Problem
 
Similar Threads
Help me solve a puzzle on datastructures
"empty" array declarations
Sun Cirtification
Reading a String backward
Stacks