> There's another problem with the above C++ code: If ny and nz
> aren't constant, you can't write
>
> double s[ny][nz];
>
> Instead, either you allocate the array in two stages:
>
> double** s = new (double*)[ny];
> for (j=0; j < ny; ++j) s[j] = new double[nz];
Actually, the first version works -- and both C and C++ (tested here with
gcc -- not sure it it became a standard or not)) will dynamically allocate
memory for you. Try this:
#include <iostream>
using namespace std;
int main() {
int size1, size2, i, j;
cin >> size1;
cin >> size2;
double vec[size1][size2];
for (i=0; i<size1; i++)
for (j=0; j<size2; j++)
vec[i][j] = i+j;
cout << endl;
for (i=0; i<size1; i++) {
for (j=0; j<size2; j++)
cout << vec[i][j] << " ";
cout << endl;
}
}
And enter something like "1000" and "1000" when asked for the array dimensions.
It compiles, runs, and does not segfault! :-)
The C version:
#include<stdio.h>
int main() {
int size1, size2, i, j;
scanf("%d", &size1);
scanf("%d", &size2);
double vec[size1][size2];
for (i=0; i<size1; i++)
for (j=0; j<size2; j++)
vec[i][j] = i+j;
puts("");
for (i=0; i<size1; i++) {
for (j=0; j<size2; j++)
printf("%f ",vec[i][j]);
puts("");
}
}
J.
--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]