2D array help

This is a discussion on 2D array help within the C++ Programming forums, part of the General Programming Boards category; Code: int **pixel; rows = numRows; columns = numCols; pixel = new int *[rows]; for(int i = 0; i < ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    2D array help

    Code:
    int **pixel;	
    rows = numRows;
    columns = numCols;
    
    	pixel = new int *[rows];
    	for(int i = 0; i < rows ; i++)
    	{
    		pixel[i] = new int [columns];
    		for(int j = 0; j < columns; j++)
    		
                    pixel[i][j] = 0;
    	}
    This bit is fine, and is in general when filling a 2d array from start through to finish.

    What if I want to then fill this 2d array with meaningful data within some statements:

    Code:
    int i=0;
    int j=0;
    
    for (int x=0;x<rows;x++){
        for (int y=0;y<columns;y++){
     
              pixelvariable++;
              if (somecondition_true){
                   pixel[i][j] = *pixelvariable;
                   if (j < (pixelColumnSize-1) j++ //THIS PART IS THE PROBLEM??
                   else j=0; i++;
         }
    }
    The code above is very rough as I do not have the problem at hand...I want to be able to fill that 2d array given a certain condition, therefore i need a way to control the i and j variables of the 2d array.

    Is there a better way? the above does not work at the moment.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    You should complete your code with appropriate braces. I do not know what you intended to do.

    Code:
    if (j < (pixelColumnSize-1) j++ //THIS PART IS THE PROBLEM??
    else j=0; i++;
    Did you mean:

    Code:
    if (j < (pixelColumnSize-1)) {
        j++;
    }
    else {
        j=0;
        i++;
    }
    ?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    Yes, that is how it is present in the actual code, thereabouts, with a few other checks...Sorry, I done it like that since I was just roughly formatting the code.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Be more specific.
    Why doesn't it work? Does it compile? Why not?
    You didn't provide the condition, I don't know what pixel_variable is. You want a better way while the current one does not even work.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    It does not work (compiles but throws memory exception), but I could get it to work eventually I think by just doing multiple checks. The variable pixel_variable is irrelevant in the context of this problem. It could just be a random Int.

    The second If statement attempts to navigate the 2d array correctly.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Code:
    for (int x=0;x<rows;x++){
        for (int y=0;y<columns;y++){
            if (somecondition_true){
                pixel[i][j] = *pixelvariable++;
                if (j < (pixelColumnSize-1)) {
                    j++;
                }
                //THIS PART IS THE PROBLEM??
                else {
                    j=0;
                    i++;
                }
            }
        }
    }
    I think your problem was related to how you chose to update pixelvariable. It doesn't have to be this, but it must be equivalent in effect to this, if it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate space for 2d array
    By jtay in forum C Programming
    Replies: 7
    Last Post: 04-25-2010, 10:34 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21