Counting Elements in an Array

This is a discussion on Counting Elements in an Array within the C Programming forums, part of the General Programming Boards category; Hey guys, I have an array or [21]rows and [19]cols. Each row and column have the numbers -1,0,1 scattered randomly ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    13

    Counting Elements in an Array

    Hey guys, I have an array or [21]rows and [19]cols. Each row and column have the numbers -1,0,1 scattered randomly throughout the array from what was a medianmap. Now i want to calculate the counts for each respective column and row for 1,0,-1. I have written the code although it wont output correctly, below is the code followed by the print statement.

    your help would be appreciated.

    Code:
    /* Declare any constants */
    /* Input file name */
    #define FILE_NAME "data-5.csv"
    /* Data size */
    #define MAX_COLS 19
    #define MAX_ROWS 21
    /* Map symbols */
    #define MIN_SYM  -1
    #define MAX_SYM  1
    #define MIDDLE_SYM  0
    /* Use to reference the counts */
    #define MIN_SYM_INDEX  0
    #define MAX_SYM_INDEX  1
    #define MIDDLE_SYM_INDEX  2
    #define MAX_COUNTS  3
    
    /* Declare any variables */
    /* Used to hold the index of the row/col that is the middle value */
        int MedianIndex1 = 0;
        /* Used to hold the index of the row/col when there is an even number of rows/cols */
        int MedianIndex2 = 0;
    
    /* Initialise the variable arrays */
        for (row=0; row < MAX_ROWS; row++) ;
        {
            RowMedian[row] = 0;
            RowCounts[row][0] = 0;
            RowCounts[row][1] = 0;
            RowCounts[row][2] = 0;
        }
        for (col=0; col < MAX_COLS; col++) ;
        {
            ColMedian[col] = 0;
            ColCounts[col][0] = 0;
            ColCounts[col][1] = 0;
            ColCounts[col][2] = 0;
        }

    Code:
    /* Get Counts */
        /* Determine Counts for the rows */
        for(row = 0; row < MAX_ROWS; row++)
        {
            for(col = 0; col < MAX_COLS; col++)
            {
                if(MedianMap[row][col] == MIN_SYM)
                {
                    RowCounts[row][MIN_SYM_INDEX] = (RowCounts[row][MIN_SYM_INDEX] + 1);
                }
                else if (MedianMap[row][col] == MAX_SYM)
                {
                    RowCounts[row][1] = (RowCounts[row][1] + 1);
                }
                else
                {
                    RowCounts[row][2] = (RowCounts[row][2] + 1);
                }
            }
        }
        
        /* determine counts for the cols */
        for (col = 0; col < MAX_COLS; col++)
        {
        for (row = 0; row < MAX_ROWS; row++)
            {
                if (MedianMap[row][col] == MIN_SYM)
                {
                    ColCounts[col][MIN_SYM_INDEX] = (ColCounts[col][MIN_SYM_INDEX] + 1);
                }
                else if (MedianMap[row][col] == MAX_SYM)
                {
                    ColCounts[col][MAX_SYM_INDEX] = (ColCounts[col][MAX_SYM_INDEX] + 1);
                }
                else 
                {
                    ColCounts[col][MIDDLE_SYM_INDEX] = (ColCounts[col][MIDDLE_SYM_INDEX] + 1);
                }
            }
        }
    print statement.

    Code:
    printf("RowCounts\n");
        for (row = 0; row < MAX_ROWS; row++)
        {
            printf("%f", RowCounts[col][MIN_SYM_INDEX]);
            printf("%f", RowCounts[col][MAX_SYM_INDEX]);
            printf("%f", RowCounts[col][MIDDLE_SYM_INDEX]);
        } 
    
        printf("ColCounts\n");
        for (col = 0; col < MAX_COLS; col++)
        {
            printf("%f", ColCounts[col][MIN_SYM_INDEX]);
            printf("%f", ColCounts[col][MAX_SYM_INDEX]);
            printf("%f", ColCounts[col][MIDDLE_SYM_INDEX]);
        }
    Last edited by sammells; 09-05-2012 at 08:53 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Without code that we can compile, this will not be easy to assess. You also failed to include your variable declarations, so we can only guess where the problem might lie. It may be possible to discern the issue you're having with only the given information, but it's much more of a challenge for us, and therefore probably not worth the effort.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    would you like me to include the entire code, and the excel file with the raw data in it?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Personally speaking, no. But if you could provide a terse program with just enough code to illustrate the problem at hand, then I would be glad to take a look.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    ok so initially we were given a data file with an array of a known size. for me [21][19]. from there we had to determine the median value for each row and column. to do so we had to sort the data from highest to lowest and select the middle column/row which contained the median values. from here we created a median map where numbers 1,0,-1 were assigned to the array.
    if the number in a cell was greater then both the median for the row and column a 1 was assigned.
    if the number in a cell was less then both the median for the row and column a -1 was assigned.
    if neither occured a 0 was assigned.
    my output for the median map was perfect.
    now for the median map i want to calculate the counts of each row and column respectively.
    this code determined the median map, assigning 1,0,-1 respectively.

    Code:
    /* Create Map */
        for (row = 0; row < MAX_ROWS; row++)
        {
        for (col = 0; col < MAX_COLS; col++)
            {
                /* if < than both row and col medians */
                if (Data[row][col] < RowMedian[row] && Data[row][col] < ColMedian[col])
                {
                MedianMap[row][col] = MIN_SYM;
                }
                else
                /* if > than both row and col medians */
                if (Data[row][col] > RowMedian[row] && Data[row][col] > ColMedian[col])
                {
                MedianMap[row][col] = MAX_SYM;
                }
                else
                {
                MedianMap[row][col] = MIDDLE_SYM;
                }
            }
        }
        /* for(row = 0; row < MAX_ROWS; row++)
        {
            for(col = 0; col < MAX_COLS; col++)
            {
                printf("%3d ", MedianMap[row][col]);
            }
            printf("\n"); 
        } */
    once the median map is sorted its output looks something like this:
    (this is a sample)
    1 0 -1 -1 1 1 1 0
    0 0 -1 0 -1 1 0 0
    -1 -1 0 0 1 1 1 0

    Now i want to determine the counts for each column and row. code is as follows


    Code:
    /* Initialise the variable arrays */
        for (row=0; row < MAX_ROWS; row++) ;
        {
            RowMedian[row] = 0;
            RowCounts[row][0] = 0;
            RowCounts[row][1] = 0;
            RowCounts[row][2] = 0;
        }
        for (col=0; col < MAX_COLS; col++) ;
        {
            ColMedian[col] = 0;
            ColCounts[col][0] = 0;
            ColCounts[col][1] = 0;
            ColCounts[col][2] = 0;
        }
    
    /* Get Counts */
        /* Determine Counts for the rows */
        for(row = 0; row < MAX_ROWS; row++)
        {
            for(col = 0; col < MAX_COLS; col++)
            {
                if(MedianMap[row][col] == MIN_SYM)
                {
                    RowCounts[row][MIN_SYM_INDEX] = (RowCounts[row][MIN_SYM_INDEX] + 1);
                }
                else if (MedianMap[row][col] == MAX_SYM)
                {
                    RowCounts[row][1] = (RowCounts[row][1] + 1);
                }
                else
                {
                    RowCounts[row][2] = (RowCounts[row][2] + 1);
                }
            }
        }
        
        /* determine counts for the cols */
        for (col = 0; col < MAX_COLS; col++)
        {
        for (row = 0; row < MAX_ROWS; row++)
            {
                if (MedianMap[row][col] == MIN_SYM)
                {
                    ColCounts[col][MIN_SYM_INDEX] = (ColCounts[col][MIN_SYM_INDEX] + 1);
                }
                else if (MedianMap[row][col] == MAX_SYM)
                {
                    ColCounts[col][MAX_SYM_INDEX] = (ColCounts[col][MAX_SYM_INDEX] + 1);
                }
                else 
                {
                    ColCounts[col][MIDDLE_SYM_INDEX] = (ColCounts[col][MIDDLE_SYM_INDEX] + 1);
                }
            }
        }
    Now i want to output the results and show the counts

    Code:
    printf("RowCounts\n");
        for (row = 0; row < MAX_ROWS; row++)
        {
            printf("%f", RowCounts[col][MIN_SYM_INDEX]);
            printf("%f", RowCounts[col][MAX_SYM_INDEX]);
            printf("%f", RowCounts[col][MIDDLE_SYM_INDEX]);
        } 
    
        printf("ColCounts\n");
        for (col = 0; col < MAX_COLS; col++)
        {
            printf("%f", ColCounts[col][MIN_SYM_INDEX]);
            printf("%f", ColCounts[col][MAX_SYM_INDEX]);
            printf("%f", ColCounts[col][MIDDLE_SYM_INDEX]);
        }

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Instead of this
    Code:
    RowCounts[row][1] = (RowCounts[row][1] + 1);
    you probably want this
    Code:
    RowCounts[row][MAX_SYM_INDEX] = (RowCounts[row][MAX_SYM_INDEX] + 1);
    The next one is wrong too.

    Actually, you should be using the increment operator for all these:
    Code:
    ++RowCounts[row][MAX_SYM_INDEX];
    You could probably compress the whole thing, with a slight reordering of your count indices, like this:
    Code:
    int RowCounts[MAX_ROWS][MAX_COUNTS] = {{0}};
    int ColCounts[MAX_COLS][MAX_COUNTS] = {{0}};
    
    for (row = 0; row < MAX_ROWS; row++)
    for (col = 0; col < MAX_COLS; col++)
    {
        int i = MedianMap[row][col] + 1;
        ++RowCounts[row][i];
        ++ColCounts[col][i];
    }
    Last edited by oogabooga; 09-05-2012 at 09:50 PM. Reason: changed MAX_ROWS to MAX_COLS
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You are using the wrong index for printing your row counts:
    Code:
    printf("RowCounts\n");
        for (row = 0; row < MAX_ROWS; row++)
        {
            printf("%f", RowCounts[col][MIN_SYM_INDEX]);
            printf("%f", RowCounts[col][MAX_SYM_INDEX]);
            printf("%f", RowCounts[col][MIDDLE_SYM_INDEX]);
        }
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting array elements
    By katipo in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 07:10 PM
  2. Counting elements in array
    By axe in forum C Programming
    Replies: 11
    Last Post: 11-14-2007, 10:17 PM
  3. counting certain elements from a file
    By greenstock in forum C++ Programming
    Replies: 17
    Last Post: 01-01-2006, 08:37 PM
  4. counting depth and elements of a tree
    By lime in forum C Programming
    Replies: 2
    Last Post: 08-04-2003, 12:37 PM
  5. Counting # of elements in array or file.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 05:33 AM

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