User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,335 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,217 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 243 | Replies: 5 | Solved
Reply
Join Date: Feb 2008
Posts: 33
Reputation: esy928 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

My Big Problem

  #1  
Jul 4th, 2008
Greetings! I was given a task yesterday to constuct a table like this:

5 4 3 2 1
4 5 4 3 2
3 4 5 4 3
2 3 4 5 4
1 2 3 4 5

using loops and only loops. I've been trying to solve this for hours and still no luck in finding its solution. can anyone help me? pleaseeeeeeee
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,454
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 129
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: My Big Problem

  #2  
Jul 4th, 2008
Post your code and we will help you correct it, but no one is just going to give it to you.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 537
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 57
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: My Big Problem

  #3  
Jul 4th, 2008
As you can see for each line, you have a start, then increase each element by 1 and when you reach 5, if you didn't insert 5 elements in the line start reducing.

5 4 3 2 1(start)

4 5(reached 5, will decrease until the line is filled) 4 3 2(start)

3 4 5(reached 5, will decrease until the line is filled) 4 3(start)
I AM the 12th CYLON
Reply With Quote  
Join Date: Feb 2008
Posts: 33
Reputation: esy928 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

Re: My Big Problem

  #4  
Jul 4th, 2008
Here's my code..
it doesnt do much...yet
public class LogicPuzzle1 {

    public static void main (String [] args)
    {
    	final int num = 5;
    	int ctr;
    	int ctr2;
    	int temp=0;
    	for(ctr=num;ctr>0;ctr--)
    	{
    		System.out.print(ctr);
    		if(ctr!=num)
    		{
    			temp=ctr;
    			if(temp==num)
    			{
    				for(ctr2=1;ctr2<num;ctr2++)
    				{
    					temp--;
    					System.out.print(temp);
    				}
    			}
    			else
    			{
    				for(ctr2=1;ctr2<num;ctr2++)
    				{
    					temp++;
    					System.out.print(temp);
    				}
    			}
    		}
    		else
    		{	
    			for(ctr2=1;ctr2<num;ctr2++)
    				System.out.print(ctr-ctr2);
    		}
    		System.out.println();
    	}
    }
    
}
Reply With Quote  
Join Date: Feb 2008
Posts: 33
Reputation: esy928 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

Re: My Big Problem

  #5  
Jul 4th, 2008
THANK YOU javaAddict!!!!!!! Weeeee....
can anyone help me make my code more efficient? (remove uneeded stuff)

public class LogicPuzzle1 {

    public static void main (String [] args)
    {
    	final int num = 5;
    	int ctr;
    	int ctr2;
    	int temp=0;
    	boolean reachMax=false;
    	for(ctr=num;ctr>0;ctr--)
    	{
    		ctr2=num;
    		temp=ctr-1;
    		reachMax=false;
    		while(ctr2>0)
    		{
    			if(temp==5||reachMax==true)
    			{
    				reachMax=true;
    				temp--;
    			}
    			else
    			{
    				temp++;
    			}
    			System.out.print(temp);
    			ctr2--;
    		}
    		
    		System.out.println();
    	}
    }
    
}
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 537
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 57
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: My Big Problem

  #6  
Jul 4th, 2008
There are many ways to implement this, so if it works don't worry about it. In my idea I was thinking of creating an array 5x5, and put the values in it before printing them. I think that it would have been simpler because with the array I could start putting things at the end of the array:
arr[0][4] = 1
arr[0][3] = 2 . . .
So instead of while inside the first for-loop, I would have the outside loop counting the line of the array, and the inside loop counting backwards and start putting the values at the end of the array.
I AM the 12th CYLON
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC