How can I make this code 2 dimentional array?

Hi all,
The code below calculates the dx/dt=u equation by euler method. This calculates the u for the position of each particles (np) and calculates the distance of particles (dist) (until now the code below which is a part of big code correct). My teacher told me by using for loop make this in 2 dimension? would you please tell me how?
I mean do the calculations and build a two dimentional array
such as : in line 22: double x[n][2], u[n][2];


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    #include <iostream>
#include <cmath>
using namespace std;


int np=100;
int dt=0.1;
double dist;



double euler(double u, double u_previous_time_step){
double a;
	a=(dt/2)*(u_previous_time_step + u);
	return a;
}

int main(){
	int n; int i;
	int tmax, t;
	int n1,n2;
	double x[n], u[n];
	double u_previous_time_step[n];
	
	while(t<tmax)
	{
		for(n=0; n<np; n++){
		x[n]+=euler(u[n], u_previous_time_step[n]);
		}
	t=t+dt;
	for(n1=0; n1<np; n1++)
		for(n2=0; n2<np; n2++)
	{
	dist= (x[n2])-(x[n1]);
	}
	}
	
	return 0;
}

Last edited on
Sorry I just thought it might be old and no one reply to it would yiu please help me correct this part of the code. But there is error

#include <iostream>
#include <cmath>
using namespace std;


int np=100;
int dt=0.1;
double dist;



double euler(double u, double u_previous_time_step){
double a;
a=(dt/2)*(u_previous_time_step + u);
return a;
}

int main(){
int n; int i;
int tmax, t;
int n1,n2;
double x[n][i], u[n][i];
double u_previous_time_step[n];

while(t<tmax)
{
for (n=0; n<np; n++)
{
for (i=0; i<2; i++)
{
x[n][i]+=euler(u[n][i], u_previous_time_step[n][i]);
}
t=t+dt;
Sorry I just thought it might be old and no one reply to it

It was not terribly old yet. More importantly, it was you who did not reply to it.

Yes, your code has blatant errors but I don't feel like pointing them out before you explain in detail what the program supposedly does. Assume that we have never heard about "euler".
x[n][m];
you can get them using x[n-1][m-1];
you can try using it in vectors too.
Hit the searchbox and search for std::vector.
hope this helps.
Topic archived. No new replies allowed.