2 Replies - 1439 Views - Last Post: 06 November 2011 - 09:18 AM Rate Topic: -----

#1 agozzino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1