Thread: Adding a new column in array

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    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.
    Attached Images Attached Images Adding a new column in array-untitled-jpg 
    Attached Files Attached Files

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think your problem is that you initially define your array like this after the user enters M and N:

    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:

    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.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Thumbs up

    Quote Originally Posted by c99tutorial View Post
    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.
    Thank you very much for your answer.It helped a lot .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-09-2010, 06:27 PM
  2. adding up array column total
    By dmckay in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 03:14 PM
  3. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-18-2005, 11:49 PM
  4. Adding data to random column in List View
    By Gravedigga in forum Windows Programming
    Replies: 6
    Last Post: 07-25-2004, 07:11 AM
  5. Replies: 10
    Last Post: 07-13-2003, 01:48 PM

Tags for this Thread