# Writing an Array

Page 1 of 1

## 4 Replies - 1406 Views - Last Post: 28 October 2006 - 08:24 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=20173&amp;s=d28592a71708365bf297f6e14e315892&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 stevenviney

Reputation: 0
• Posts: 2
• Joined: 27-October 06

# Writing an Array

Posted 27 October 2006 - 02:52 PM

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

```#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.....

Is This A Good Question/Topic? 0

## Replies To: Writing an Array

### #2 jsenseman

Reputation: 1
• Posts: 28
• Joined: 15-July 06

## Re: Writing an Array

Posted 27 October 2006 - 09:13 PM

stevenviney, on 27 Oct, 2006 - 02:52 PM, said:

i need to write a program that sets up a table displaying values of n, sqrt n and ln n from n = 0 - nmax.

Do you need to store these values in an array or just display them ?

If you only need to display them you can use a for loop

i.e.
```#include <stdio.h>
#include <cmath>
using namespace std;

int main()
{  int max;
float n=0,sqroot,ln;
printf("Enter Maximum  :");
scanf("%d",&max);

for(int i=0;i<max;i++)//table loop
{   sqroot=sqrt(n); //sqrt
ln=log(n); // ln
printf("\n%2.3f	 %2.3f	  %2.3f   \n",   n,sqroot,ln); // here is  an aligned report(table)
n++;
}

return 0;
}

```

If you need the calculated results stored in a table then you need to use an array of struct, and the new operator to dynamcally allocate the table.

Hope this helps

This post has been edited by jsenseman: 27 October 2006 - 09:14 PM

### #3 dorknexus

Reputation: 1272
• Posts: 4,625
• Joined: 02-May 04

## Re: Writing an Array

Posted 27 October 2006 - 11:59 PM

in c++, arrays exist on the stack. and the program must tell the kernel ahead of time how much memory to allocate on the stack. in this case, you do not know how much memory you will need ahead of time, instead you will only know at run time. in this case you need to use dynamic memory allocated which can be easily accomplished by use of pointers.

for instance, take your same code, and implement pointers in place of the static array. it would look like so

```#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
const int numRows = 3;

//Get input
cout <<"This program will setup a table with values up to an integer n. Please enter your desired maximum integer n:"<< endl;
cin >> n;

//Make array
double *table[numRows];

for (int y = 0;y < numRows;++y)
{
table[y] = new int[n];
}

//Display table
for (int y = 0; y < n;++y)
{
for (int x = 0;x < numRows;++x)
{
cout << table[y][x] << " ";
}
cout << endl;
}

for (int y = 0;y < numRows;++y)
{
delete []table[y];
}

return 0;
}

```

the first modification i made, was i removed the following statement:

const int numRows = n;

i am assuming that you are trying to get numRows to change with n. However, this will only assign n as the value of numRows at that very moment. if n changes later, numRows will not. I simply removed the variable.

next, input was requested from the user and that value was assigned to n. then dynamic memory allocated happens by use of the new keyword.

double *table[numColumns] makes an array of integer pointers, 'numColumns' long, this sets up the first dimension of the table. then, by use of a for loop, 'n' integers are allocated and tied to each pointer in the list.

```for (int y = 0;y < numColumns;++y)
{
table[y] = new int[n];
}

```

we then display this table by use of a double for-loop. the outer for loop traverses the rows of the table, and the inner for loop traverses the columns of the table. for each row of the table, 'n' columns will be displayed, and then the loop will move to the next row until no more rows are left to be displayed.

```for (int y = 0; y < n;++y)
{
for (int x = 0;x < numColumns;++x)
{
cout << table[y][x] << " ";
}
cout << endl;
}

```

and finally, the memory allocated to each integer pointer is then released

```for (int y = 0;y < numRows;++y)
{
delete []table[y];
}

```

no data is assigned to the allocated table though so it would be pretty useless to display anything.

for further reference on pointers, you can take a look at this tutorial:

http://forums.dreami...wtopic10826.htm

### #4 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: Writing an Array

Posted 28 October 2006 - 03:18 AM

if you don't know the size of your data structure before you run the program you can use a vector (an expandable array), e.g.

```#include <iostream>
#include <cmath>
#include <vector>

using namespace std;
int main()
{
int n;
const int numRows = n;
const int numColumns = 3;
vector < vector < double > > table(3, vector <double>(0,0));
cout <<"This program will setup a table with values up to an integer n. Please enter your desired maximum integer n:"<< endl;
cin >> n;
for (int i=0; i<n+1; i++)
{ table[0].push_back(i);  table[1].push_back(sqrt(i)); table[2].push_back(log(i)); }
for (int i=0; i<n+1; i++)
cout << table[0][i]<< ' ' <<  table[1][i] << ' ' <<   table[2][i] << endl;

return 0;
}

```

### #5 stevenviney

Reputation: 0
• Posts: 2
• Joined: 27-October 06

## Re: Writing an Array

Posted 28 October 2006 - 08:24 AM

wow thanks so much, that really got me going... very very much appreciated... .