# Making a table and displaying it in table form [closed]

a[1] = 2;
a[2] = 4;
a[3] = 7;
E = 1.5;
l = 0.02;
Print["m                   n1              n2               n3"];
For[m = 1, m < 10, m++,
For[i = 1, x = 0, i <= 3, i++;
x = (a[i] + m + E)/l;
If[i == 1, n1 = x; i == 2, n2 = x; i == 3, n3 = x];];
Print[m "     ", n1 "        ", n2 "       ", n3];]


I want to get output in table format. If anyone can solve this their help will be most welcome.

Table format means I want my output to look like this:

m                  n1            n2              n3
1                  -             -               -
2                  -             -               -
3                  -             -               -


and so on up to 10 values of m.

• Please at least try to read the docs for correct syntax before asking. a[1] = 2; a[2] = 4; a[3] = 7; e = 1.5; l = 0.02; Print["m n1 n2 n3"]; For[m = 1, m < 10, m++, For[i = 1; x = 0, i <= 3, i++, x = (a[i] + m + e)/l; Which[i == 1, n1 = x, i == 2, n2 = x, i == 3, n3 = x];]; Print[m " ", n1 , " ", n2, " ", n3];] – vapor Apr 19 '19 at 7:23
• It may help you to consider the statement Grid[Table[ f[m,i] , {m, 1, 9}, {i, 1, 3}]] . If that gives you the right size grid, then you're halfway there. After you see what Grid and Table can do for you, the next step is to define a function f[m,i] or to replace f[m,i] with an appropriate expression that gives what you want in the grid. If you get stuck, please edit your question and add your new code to show us your changes and where you are stuck. – LouisB Apr 19 '19 at 8:58
• Thanks @happy fish it's effective. – vijay Apr 19 '19 at 18:29

Your input of code is appreciated, but the syntax seems to be everywhere, and nowhere that is Mathematica or WL. Please see this post, and search "for loop" at the search bar, to find out why for loops are best left out of your programming constructs:

https://mathematica.stackexchange.com/a/259/63039

When creating a set, such as you have attempted with a, you must declare it as a set, whereas you have been declaring the output of the function of "a" with the value applied to it within the brackets. This is like so:

a = {2, 4, 7}; Ε = 1.5; l = 0.02;


I hope this helps!

Edit: Don't use protected symbols like 'E', instead, if you wish to track 'E' throughout, use something like:

Ε

• I am not getting the proper output. If you can solve this, most welcome. – vijay Apr 19 '19 at 7:13
• @vijay can you please clarify what the size of your table will be? Possibly add some expected output, if your syntax is lacking in what is necessitated to do so. I am trying to parse your goal, but it is difficult. I will check back later and see what I can do :D – CA Trevillian Apr 19 '19 at 7:32
• Now see the question again, if you can solve now. – vijay Apr 19 '19 at 8:10

When you want to build a table in Mathematica, your 1st choice should be Table not For. Here is a simple way of making your table.

t1 =
Module[{a, e, l},
a = {2, 4, 7};
e = 1.5;
l = 0.02;
Table[{m, (a + m + e)/l} // Flatten, {m, 10}]];

TableForm[t1, TableHeadings -> {None, {m, n1, n2, n3}}]


### Update

The following is added to address concerns raised by the OP in a comment to this answer.

To make m run from -10 to 0, you have to write the index specification in a slightly different form. Like so.

Table[{m, (a + m + e)/l} // Flatten, {m, -10, 0}]


The index range specification must always be given in the direction toward positive infinity; lowest value 1st then highest value.

• This code is not working. In the output only showing t1. Don't know why. By the way i am using version 9.0 – vijay Apr 19 '19 at 18:32
• @vijay if I am correct, you would have t1:=Module[.....and so-on for m_goldberg's answer to evaluate – CA Trevillian Apr 19 '19 at 19:47
• @vijay. Sorry, I forgot the assignment to t1. I have fixed the code. – m_goldberg Apr 20 '19 at 0:33
• Now it's working. thanks a lot. for your time. – vijay Apr 20 '19 at 4:31
• @m_goldberg but this not showing anything if need values upto 0 to -10. negative values of m is not showing. if you can figure it out please take a look. – vijay Apr 20 '19 at 4:42