# Nested loop for function that outputs table

Page 1 of 1

## 2 Replies - 2075 Views - Last Post: 06 November 2011 - 09:18 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=254547&amp;s=ed002851502711c0aac2648480a3fe20&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 agozzino

Reputation: 0
• Posts: 10
• Joined: 18-October 11

# Nested loop for function that outputs table

Posted 06 November 2011 - 09:09 AM

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

Prompt and read in the number of table rows. (You do not need to check that this number is positive.) If the table has m rows, then the table will contain f(x,y) for x and y from 0 to m − 1.
Use two nested for loops to print your table, looping over the values of x and y. Note that x and y should be integers and should both start at 0. Note also that y should be less than or equal to x.
Print the output in fixed precision with one digit after the decimal point. Columns and decimal points should be aligned.
ihave:
```#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int function(int x, int y)
{
int f;
f=sqrt(pow(x,2)+pow(y,2));
return(f);
}

int main()
{
int x(0);     // x integers
int y(0);     // y integers
int m(0);     // number of integers
int f(0);     // function (x,y)

cout<<"Enter number of rows; ";
cin>>m;

for (x=0; x<=m-1; x++)
{
for (y=0; y<=x; y++)
{
f=function(x,y);
cout.precision(1);
cout<< setw(f)<<endl;
}
}
return 0;
}

```

I am having trouble making the table any help???

This post has been edited by jimblumberg: 06 November 2011 - 09:12 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Is This A Good Question/Topic? 0

## Replies To: Nested loop for function that outputs table

### #2 AMZDeCoder

Reputation: 25
• Posts: 102
• Joined: 04-November 11

## Re: Nested loop for function that outputs table

Posted 06 November 2011 - 09:16 AM

Your new line statement should be outside the inner for loop,
See the inner loop will execute once for every outer loop iteration, so new line statement should be in the outer loop.
Hope that helps.

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3746
• Posts: 13,621
• Joined: 08-August 08

## Re: Nested loop for function that outputs table

Posted 06 November 2011 - 09:18 AM

I'd use printf and tabs.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }