Please Help!!!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 2
Reputation: nishad is an unknown quantity at this point 
Solved Threads: 0
nishad nishad is offline Offline
Newbie Poster

Please Help!!!

 
0
  #1
Aug 29th, 2005
Hello everybody, actually i'm having problems with java loops as it is so complicated, dont know where to start. I have 2 questions if u could please answer it.

1. Write a program that generates and ouputs the table below:

10 20 30 .... 100
110 120 130 ....200
210 220 230... 300
. . . .
910 920 930... 1000


2. The square root of a number is computed as follows:
guess = n/2.0

repeat
guess = (guess+ n/guess)/2.0
until guess comes to very close to answer.

Write a program to find the sqaure root of a number a positive.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 773
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Please Help!!!

 
0
  #2
Aug 29th, 2005
You have several different types of loops


while (condition is true)
{}

for(variable declaration;while condition is true;variable increment/decrement)
{
}

for(int i=0; i<something; i++)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Please Help!!!

 
0
  #3
Aug 30th, 2005
Complicated? If you think a basic loop is complicated you'd better give up now and look for employment as a bellboy in a hotel without an elevator.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

Re: Please Help!!!

 
0
  #4
Aug 30th, 2005
For the first problem, you must use two for loops. Lets call the horizontal direction i and the vertical direction j.

Notice how the #'s change 10 times from 10 to 100 in direction i
Notice how the #'s change 10 times from 10 to 210 in direction j

Therefore use a loop that looks like
for(int i =0; i < 10; i++)
{
	System.out.println("Visiting row " + i);

	for(int j=0; j < 10; j++)
	{
		System.out.println("\tVisiting col " + j + " in row " + i);				
	}
}

The outer loop runs once for each row (there are 10 rows).
For each row, the inner loop runs once for each col (there are 10 columns) in that row.
Therefore, it will "visit" the table from top to bottom and left to right.
It prints the following

  1. Visiting row 0
  2. Visiting col 0 in row 0
  3. Visiting col 1 in row 0
  4. Visiting col 2 in row 0
  5. Visiting col 3 in row 0
  6. Visiting col 4 in row 0
  7. .
  8. .
  9. .
  10. Visiting row 1
  11. Visiting col 0 in row 1
  12. Visiting col 1 in row 1
  13. Visiting col 2 in row 1
  14. Visiting col 3 in row 1
  15. Visiting col 4 in row 1
  16. .
  17. .
  18. .
  19. .
  20. ..
  21. etc



How do the number's change in the i direction?
They always increment by 10.

How do the number's change in the j direction?
They always increment by 100.


You must create some variables before the two loops called currentValueInIDirection and currentValueInJDirection. The variables will change value as you generate the table.
These variables you must increment according to the rules above.

For more help, www.NeedProgrammingHelp.com



For the second problem, ask the user for input by writing

  1. System.out.println("Enter a guess");
  2. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  3. int n = Integer.parseInt(in.readLine());

Use the while loop and the formula given.

The hardest part is to know when to stop. The variable guess is constantly being updated in the loop. When its changes by a small amount (say .01) then it is safe to stop the loop.
To do this, create a separate variable called guessPrevious. Then do the following in the loop

  1. //save the guess before changing it
  2. guessPrev = guess;
  3.  
  4. //update guess using formula
  5. guess = (guess+ n/guess)/2.0
  6.  
  7. //stop loop if small change
  8. if( Math.abs(guessPrev-guess) < .01)
  9. break;
For more help, www.NeedProgrammingHelp.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC