Thread: Need some help initializing an array

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    53

    Need some help initializing an array

    Hi, Im trying to initialize the counter array to all zeros, but I keep getting this error when trying to compile:

    xray.cpp:30: error: variable-sized object ‘counter’ may not be initialized

    Code:
    #include <iostream>
    #include <stdio.h>
    #include "CImg.h"
    
    
    using namespace cimg_library;
    
    //main program
    int main()
    {
    CImg<double> img("xray.jpg");
    
    //retrieve image dimensions
    const unsigned int width = img.dimx();
    const unsigned int height = img.dimy();
    
    //converts the image to black and white
    img.RGBtoYCbCr().channel(0).resize(-100,-100,1,3).RGBtoLUT(CImg<>(2,1,1,3).fill(0.0f,255.0f),false); 
    
    //displays image
    CImgDisplay display(img,"Black and White");
    while (!display.is_closed)
    display.wait();
    
    //display dimensions
    printf("The height of the image is %d\n",height);
    printf("The width of the image is %d\n",width);
    
    //initialize counter array of size height
    int counter[height]={0};
    
    //go through image one row at a time from left to right
    for (int i=0;i<width;i++)
    {
        for (int j=0;j<height;j++)
        {
    	 //if pixel equals white
    	 //add total number of white pixels to counter  
                
        }
    }
    
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The error message seems pretty clear: you cannot initialize a variable-length object. (I.e., initialization is a compile-time activity, and the compiler has no idea what length is going to be in the future.) You'll have to use a loop.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    The error message seems pretty clear: you cannot initialize a variable-length object. (I.e., initialization is a compile-time activity, and the compiler has no idea what length is going to be in the future.) You'll have to use a loop.
    thats tabstop, that makes sense. I have one last question Im stumped on. Im trying to calculate the number of white pixels per row, but so far I can only get it calculate the total number of white pixels in the image. How do I make it calculate the total number of white pixels per row? Here is the code I have so far:

    Code:
    #include <iostream>
    #include <stdio.h>
    #include "CImg.h"
    #include <vector>
    
    using namespace cimg_library;
    
    
    //main program
    int main()
    {
    CImg<float> img("xray.jpg");
    
    const unsigned int width = img.dimx();
    const unsigned int height = img.dimy();
    
    //converts the image to black and white
    img.RGBtoYCbCr().channel(0).resize(-100,-100,1,3).RGBtoLUT(CImg<>(2,1,1,3).fill(0.0f,255.0f),false); 
    
    //displays image
    CImgDisplay display(img,"Black and White");
    while (!display.is_closed)
    display.wait();
    
    
    printf("The height of the image is %d\n",height);
    printf("The width of the image is %d\n",width);
    
    //counter array
    int counter[height];
    int number = 0;
    
    //go through image left to right, top to bottom
    for (int i=0;i<width;i++)
    {
        for (int j=0;j<height;j++)
        {
    	 //if pixel equals white, add to number
    	 if (img(i,j,0,0) == 255 && img(i,j,0,1) == 255 && img(i,j,0,2) == 255)
    	 {
    		 number=number + 1;
    	 }
    
        }
    }
    printf("The number of white pixels is %d\n", number);
    
    int x;
    
    //for each row, print the total number of white pixels for that row
    for (x =0; x < height; x++)
    {
    	printf("%d %d\n", x, number);
    }
    
    return 0;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, you're looping over the entire image. Don't do that, if you only want to look at a single row.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    You'll have to keep track of how many white pixels you get per row, just like you want.

    Code:
    // this is kinda pseudo-code
    
    int height = 100; // get the actual height, i just use 100 for example
    int width = 100; // ditto
    
    int numWhites[height][1];
    
    // init all to zero
    for(int h = 0; h < height; h++){
        numWhites[h][0] = 0;
    }
    
    // find all the values == 255
    for(int y = 0; y < height; y++){
        for(int x = 0; x < width; x++){
            if(image[x][y] == 255){
                numWhites[y][0]++;
            }
        }
    }
    
    // now count how many per row
    for(int y = 0; y < height; y++){
        printf("number of whites for line %i: %i\n", y, numWhites[y][0]);
    }
    I typed that in the reply window so if there's any syntax errors have mercy.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    BTW, variable length arrays like the one you're using aren't legal in standard C++, they are just provided as an extension on some the gcc compiler (because they are in the latest C standard).

    Normally, in standard C++ you'd use a vector for a dynamically sized array. The vector is automatically initialized with the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM