# Problem with a for loop

This is a discussion on Problem with a for loop within the C++ Programming forums, part of the General Programming Boards category; Hi, I have to translate this line from Matlab to C/C++: x = (-cols/2 : (cols/2 - 1))/(cols/2);y = -(-rows/2 ...

1. ## Problem with a for loop

Hi,

I have to translate this line from Matlab to C/C++:

x = (-cols/2 : (cols/2 - 1))/(cols/2);y = -(-rows/2 : (rows/2 - 1))/(rows/2);[x,y] = meshgrid(x,y);
I try to explain better.

x is a monodimensional array of lenght=cols.
y x is a monodimensional array of lenght=rows.

Cols and rows depends from the picture I charge in the project. (EX cols=696,rows=534).

x[cols]= all the elements from -(cols/2)/(cols/2) to (cols/2)-1/(cols/2)
(example) -> x[0] = -(696/2)/(696/2)= -1;
x[1]= -(695/2)/(696/2)= .-0,99
....
x[695]= 0,99

For y it s the same. After done this I have to meshgrid. I mean:

transform the domain specified by vectors x and y into arrays X and Y , that are bidimensional arrays.
The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.

2. I tried with :

Code:
```
double x_a = -(double)cols/2;
double x_b = ((double)cols/2 - 1)/((double)cols/2);
double y_a = (double)rows/2;
double y_b = -((double)rows/2 - 1)/((double)rows/2);
double m_inc = (x_b-x_a) / m;
double n_inc = (y_b-y_a) / n;

double X[rows][cols];
double Y[rows][cols];

for(i = 0 ;  i<=n; i++){
for(j= 0;  j<=m; j++) {
X[i][j] = x_a + m_inc*j;
Y[i][j] = y_a + n_inc*i;
}
}```

3. can you post some code please

4. Originally Posted by Elvio Esposito
I tried with :

Code:
```
double x_a = -(double)cols/2;
double x_b = ((double)cols/2 - 1)/((double)cols/2);
double y_a = (double)rows/2;
double y_b = -((double)rows/2 - 1)/((double)rows/2);
double m_inc = (x_b-x_a) / m;
double n_inc = (y_b-y_a) / n;

double X[rows][cols];
double Y[rows][cols];

for(i = 0 ;  i<=n; i++){
for(j= 0;  j<=m; j++) {
X[i][j] = x_a + m_inc*j;
Y[i][j] = y_a + n_inc*i;
}
}```
what are you using to edit your code? microsoft word?

5. >>for(i = 0 ; i<=n; i++)
This is a buffer overrun. Indexes go from 0 to size - 1, so if n is size, then n - 1 is the last element.
Arrays in C++ are tricky, especially since there is no default bounds checking and everyone teaches people not to use practices to avoid those, so have a read.

6. Originally Posted by Elkvis
what are you using to edit your code? microsoft word?
I do this sometimes, it's really nice being able to see how it'll look on paper before i print it.

7. o_O
Why do you print code?

8. I solved with:

Code:
```double x[cols*2],y[rows*2];

for(int i=0;i<cols*2;i++){
x[i]=((double)(i-cols)/2)/((double)cols/2);
}```
The same thing for the y vector.
Now to do the meshgrid:

Code:
``` double X[cols*2][rows*2];
double Y[cols*2][rows*2];

for(int m=0;m<cols*2;m++){
for(int n=0;n<rows*2;n++{
X[m][n]=x[m];
Y[m][n]=y[n];
}
}
```

Is it right?

9. Originally Posted by Elysia
o_O
Why do you print code?
Physically annotating code with a red pencil is one of life's joys. Do it in moderation.

10. After changed the loop , print it (work good), after change the other loop, now I have a Segmentation fault a run time I see debugging. It must be about radius and theta. The code:

Code:
```
...
raidus=(double**)malloc(sizeof(double*)*rows;
for(int i=0;i<rows;i++);

theta=(double**)malloc(sizeof(double*)*rows;
for(int z=0;z<rows;z++);
theta[z]=(double*)malloc(sizeof(double)*cols;```
The function where they are used I modified. When I arrive to print function I have the segmentation fault debugging

Code:
```void pre_filter_Computations(double **radius,double **theta,int cols,int rows){

double x[cols],y[rows];
double X[cols][rows],
Y[cols][rows];
double epsilon=0.0001;

printf("Entering prefilter function\n");

for(int i=0;i<cols*2;i++){
x[i]=((double)(i-cols)/2/((double)cols/2;
}
...```

11. is there a really good reason why you're using pointer-to-pointer style arrays instead of std::vector<std::vector<double> >?

12. More cross-posting buffoonery.
Segmentation Fault At Run Time - Dev Shed

13. The first reason is that in my University we studied just C and not C++. But my work project is about image processing. So I have to use OpenCV library that works with OpenCV. So I don' t know well the type of variable. For what I just see I suppose it' very usefull in my case. What I have to do it s to build in this function the 2 matrix theta(angle) and radius to speed up filter construction. If I use <std::vector<double> I can go to every part of the vector as it be a bidimensional array? Because after I have to square all ellement of radius and to change the center value of matrix radius to 0 and do some more stuff. Thanks helping

14. A vector is an array with dynamic size. Therefore, yes, you can access any element.
You need to brush up on your C++. When you need to do something, look for the available C++ containers and algorithms.
And whatever you do - don't use malloc! It will cause your program to blow up in your inexperienced hands with C++. There are places where one might have to use it, but this is not one of them. Always use new/delete instead of malloc/free in C++ unless there is a special requirement.
But to go even further, don't use new at all if you can. There are lots of containers that all the hard work for you (eg vector). Even if there are no containers, you can still use smart pointers.
And if the standard library don't have what you're looking for - don't forget to try the Boost library!

And I did give you a link before about arrays, which I am pretty convinced you did not read. It is important that you do.
If you don't understand all of it, then that's fine. Ask a question or go research a little on the topic you don't understand, but be sure to understand what it says before dealing with arrays. Arrays are tricky things - especially to a C developer who has no knowledge of the alternatives to make them safe in C++.

15. I have a question: Can I assign a bidimension array of double to an array of double;

double X[cols][rows];