Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Writing a Table

  1. Oct 27, 2006 #1
    i need to write a program that sets up a table displaying values of n, sqrt n and ln n from n = 0 - nmax. Ive prompted the user to enter a value for integer nmax. but then ive no idea how to make this apply for the value for nmax for my table.

    this is what i have so far

    Code (Text):

    //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? ive spent hours and hours, im sorry, im so bad at this...

    all i get out is 0x22cbd..... :frown:
  2. jcsd
  3. Oct 27, 2006 #2
    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...
  4. Oct 27, 2006 #3


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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).
  5. Oct 28, 2006 #4

    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?
  6. Oct 28, 2006 #5
    For what you are doing a spreadsheet program should suffice. OpenOffice.org Calc would do the trick or if you prefer M$ then Excel.
  7. Oct 28, 2006 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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)
  8. Oct 28, 2006 #7


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook