# Adding a new column in array

• 11-21-2012
Tomislav Pti
Adding a new column in array
Can you tell me what I did wrong ? The program is supposed to create a two-dimensional array of specific dimensions from interval M.N and then enter random numbers form interval X-Y. Then you have to enter a number A which indicates where am I supposed to add a new column to the array. For example A=3 new column is between previous 2nd column and 4th column. The new column should be all zeros. Use of additional arrays is not permitted.

The error I get is that zeros are written in not only the last column but on first too which is not supposed to happen.
Code and example are in attachments.

If anyone can answer, awesome.If not ,its cool.
• 11-21-2012
Nominal Animal
If you'll include the code in your post (not as an attachment; use the [CODE][/CODE] tags), and translate the variable names and comments to English, I'd be happy to take a look.
• 11-21-2012
c99tutorial
I think your problem is that you initially define your array like this after the user enters M and N:

Quote:

int Z[M][N];
So, for example if M=N=5 then we have created a 5x5 matrix (the arraz Z has 25 elements). Then later you try to do this:

Quote:

N=N+1;
It seems you are assuming that Z will resize itself which is not the case. Your choices are:

1. Make a new array on the stack with M rows and N+1 columns (not allowed according to your rule).
2. Preallocate an array with MAXROW rows and MAXCOL columns, where MAXROW and MAXCOL are "big enough".
3. Use dynamic allocation and use malloc(3), realloc(3) and free(3) as appropriate

Also it would help to break your program into functions and then just post the function which you are having trouble with. It helps for debugging and communication.
• 11-22-2012
Tomislav Pti
Quote:

Originally Posted by c99tutorial
I think your problem is that you initially define your array like this after the user enters M and N:

So, for example if M=N=5 then we have created a 5x5 matrix (the arraz Z has 25 elements). Then later you try to do this:

It seems you are assuming that Z will resize itself which is not the case. Your choices are:

1. Make a new array on the stack with M rows and N+1 columns (not allowed according to your rule).
2. Preallocate an array with MAXROW rows and MAXCOL columns, where MAXROW and MAXCOL are "big enough".
3. Use dynamic allocation and use malloc(3), realloc(3) and free(3) as appropriate

Also it would help to break your program into functions and then just post the function which you are having trouble with. It helps for debugging and communication.