Creating a Table for n, sqrt n and ln n from 0 to nmax

  • Thread starter Thread starter stevenviney
  • Start date Start date
  • Tags Tags
    Ln Table
AI Thread Summary
The discussion revolves around creating a program that generates a table displaying values of n, sqrt(n), and ln(n) for a user-defined maximum integer nmax. The user has initiated the program but is struggling with implementing the logic to fill the table and display the results correctly. Key points include the need to define the size of the array after receiving nmax input, as well as the importance of using a loop to calculate and store the values in the array. The suggestion is made that instead of directly printing the array, the program should iterate through its elements to display the values. Additionally, there is a recommendation to consider whether an array is necessary, as the calculations could be printed directly without storing them, although the user insists on needing an array for listing all values. A potential solution includes writing a function to handle the printing of the table, which could streamline the process for future use.
stevenviney
Messages
3
Reaction score
0
i need to write a program that sets up a table displaying values of n, sqrt n and ln n from n = 0 - nmax. I've prompted the user to enter a value for integer nmax. but then I've no idea how to make this apply for the value for nmax for my table.

this is what i have so far

Code:
//Problem Sheet 2 Table
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  int n;
  const int numRows = n;
  const int numColumns = 3;
  double table[numRows][numColumns];
 cout <<"This program will setup a table with values up to an integer n. Please enter your desired maximum integer n:"<< endl;
 cin >> n;

 cout <<table;

return 0;
}

how can i make nmax apply to the n i choose? and how can I set it up in a table which is displayed? I've spent hours and hours, I am sorry, I am so bad at this...

all i get out is 0x22cbd... :frown:
 
Technology news on Phys.org
Do you specifically need to store the values in an array?
If you only have to write them out onto the screen then there is no need for an array...
 
I don't think "cout <<table;" is what you want (after you've built your table). You would want to loop through all of the elements in the array and send them (as scalars) to the stream... rather than sending the pointer to the table.

To build your table, you'd probably set up a for-loop from 0 to n and then calculate your values and place them in the table. As jpr0 mentioned, you may be able to skip the array altogether by immediately printing out the calculations for a particular value of i (0<=i<=n).
 
hmm..

no i do need an array becaue i need to list all the values from 0 - n so i assumed it would needed to be displayed in table form?
 
For what you are doing a spreadsheet program should suffice. OpenOffice.org Calc would do the trick or if you prefer M$ then Excel.
 
Internally, an array is just a pointer. So when you say:

cout << table;

it prints the memory address where the array begins. As the others have said, you need to "manually" print each entry in the table.

Actually, if you're feeling up to it, you could write a function that takes a 2-dimensional array, as well as its dimensions, and does the printing... and then you could use that in the future when you want to print tables. (there will be some tricky language issues, though)
 
Back
Top