Problem with arrays

I made a mistake and I don´t know how to solve it... It´s strange because I don´t know what I did wrong.

This is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (i=0;i<=n;i++){
    nodx[i] = Lx/((n-1)*2)+Lx/(n-1)*(i-1);}
nodx[0]=0;
nodx[n]=Lx;

for(i=0;i<=n;i++){
     cout << nodx[i] << " " ;}

for (j=0;j<=m;j++){
      nody[j] = Ly/((m-1)*2)+Ly/(m-1)*(j-1);}
nody[0]=0;
nody[m]=Ly;

cout << "\n";                         
for(i=0;i<=n;i++){
     cout << nodx[i] << " " ;}   


and the output is strange because when I obtein nodx[0] with the first cout is 0 , meanwhile with the second one is another number... but in between I didn´t do anything with nodx[i]...
please post the entire file's text and what errors you are getting, this just looks like gibberish at the moment, we dont know what n's value is, or Lx's value, or Ly, etc
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
#include <fstream>
#include <iostream>
using namespace std;
#define n 4
#define m 4

float Lx= 0.9;
float Ly= 0.9;
int i;
int j;
float x[n];
float y[m];
 
main()
{
for (i=0;i<=n;i++){
    x[i] = Lx/((n-1)*2)+Lx/(n-1)*(i-1);}
x[0]=0;
x[n]=Lx;

for(i=0;i<=n;i++){
     cout << x[i] << " " ;}


for (j=1;j<=m;j++){
      y[j] = Ly/((m-1)*2)+Ly/(m-1)*(j-1);}

for(i=0;i<=n;i++){
     cout << x[i] << " " ;}
     
y[0]=0;
y[m]=Ly;
system ("pause");
return 0;

}


I have no problem with this code separately, When I put it in a bigger code (at the very beggining where anything can change the value of x(0) ... i don´t know why it changes)
It's these two lines of code (at line 19 and line 32):

1
2
x[n]=Lx;
y[m]=Ly;


You initially declared these arrays thus:

1
2
float x[4];
float y[4];


So when initialising or reading MEMBERS of an array, the maximum value you would use is 3. This is because when initialising or reading, the members start at 0, NOT at 1.

To fix lines 19 and 32, replace them with:

1
2
x[n - 1]=Lx;
y[m - 1]=Ly;
Topic archived. No new replies allowed.