Hello!

I was given the following problem:

Define the function f(x; y) as:
f(x,y) = sqrt(x^2-y^2)
Write a program that produces a table of f(x, y) for integers x and y from 0 to n where
y <= x. The value of f(x,y) appears in row (x+1), column (y +1) of the table. The number
of rows in the table is determined by the user. Format the table so that the columns align
and decimal points align. (You may assume that the table has at most fifty rows.) For
instance, a table with fifteen rows looks like:
0.0
1.0 0.0
2.0 1.7 0.0
3.0 2.8 2.2 0.0
4.0 3.9 3.5 2.6 0.0
5.0 4.9 4.6 4.0 3.0 0.0
6.0 5.9 5.7 5.2 4.5 3.3 0.0
7.0 6.9 6.7 6.3 5.7 4.9 3.6 0.0
8.0 7.9 7.7 7.4 6.9 6.2 5.3 3.9 0.0
9.0 8.9 8.8 8.5 8.1 7.5 6.7 5.7 4.1 0.0
10.0 9.9 9.8 9.5 9.2 8.7 8.0 7.1 6.0 4.4 0.0
11.0 11.0 10.8 10.6 10.2 9.8 9.2 8.5 7.5 6.3 4.6 0.0
12.0 12.0 11.8 11.6 11.3 10.9 10.4 9.7 8.9 7.9 6.6 4.8 0.0
13.0 13.0 12.8 12.6 12.4 12.0 11.5 11.0 10.2 9.4 8.3 6.9 5.0 0.0
14.0 14.0 13.9 13.7 13.4 13.1 12.6 12.1 11.5 10.7 9.8 8.7 7.2 5.2 0.0

I know that I have to use functions, and I also know that I have to use nested for loops to get/print out the values of x and y. Could I get just a hint of what the next step is (in terms of setting up the loops)? Any input would be greatly appreciated!

Sit down at your desk with pencil and paper with the above explanation.

Start by figuring out how x and y work together to get the chart (forget the function for the moment). What happens to y as x changes and vice versa. How can that interaction be defined as a loop or loops?

Program that portion to get it working.

Then start adding the function.

hmmmm....okay. I think the for loops for x and y would start off like this:

for (x=0; ???; ???)
{...}
for (y=0;???; ???)
{...}

I guess the part I'm having trouble with is finding out how to relate the number of table rows the user wants (inputs), and the values of the x's and the y's. I know the number of rows would be equal to the input-1.

Can you do the problem without the computer? Just sitting at a table on paper?

The thing I'm stuck on is how they are getting these x and y values

The thing I'm stuck on is how they are getting these x and y values

Oh?

... for integers x and y from 0 to n where y <= x.

okay, I THINK I know how they are getting the values. I solved for y and got that y = ((user input)^2-(x)^2). Then you find each value for when x=0 up until x is equal to the user input. Does that seem right?

okay, I THINK I know how they are getting the values. I solved for y and got that y = ((user input)^2-(x)^2). Then you find each value for when x=0 up until x is equal to the user input. Does that seem right?

No. You are over-thinking it.
x goes from 0 to n.
For each x, y goes from 0 to x.

-x-  -y-
 0    0
 1    0
 1    1
 2    0
 2    1
 2    2
 3    0
  etc.

so the loops would look something similar to this:

for (x=0; x<=n; x++)
{....
    for (y=0; y<=x; y++)
    {  .....}
}

?

so the loops would look something similar to this:

for (x=0; x<=n; x++)
{....
    for (y=0; y<=x; y++)
    {  .....}
}

?

:) :cool:

:icon_smile: :icon_biggrin:

:icon_cheesygrin: :icon_mrgreen:

:icon_wink:

okay okay :cool:...so in the second loop I would call the function, no?

okay okay :cool:...so in the second loop I would call the function, no?

You really want us to write this for you, don't you.

The art of programming requires thought. I have not seen much thought on your part. It's just ask a question and hope you get the perfect answer.

The question you just asked is answered in the problem description you posted. Just as the previous question was. Now sit down with some paper and do the problem by hand so you understand how to solve the task. If you don't understand the problem, you can't program it. So work out how to solve the problem, then you have a better chance at programming it.

I didn't intend to make it seem like I wanted it to be written for me. In fact, I had those loops written out before I asked the question, but I couldn't get them to work. I'm not looking for any free handouts.

OK. As I said, understanding is the only path to programming (sounds like a Chinese proverb).

If you really need to ask, you don't understand the problem. I'm going to assume you really know the answer and I'm going to ask you this instead:

Why should the function be called in the second loop rather than somewhere else?
Where would that somewhere else be that makes sense and why?

so the loops would look something similar to this:

for (x=0; x<=n; x++)
{....
    for (y=0; y<=x; y++)
    {  .....}
}

?

This type of loop will give this triangular shape to the output but what about the content in the triangle

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.