Thread: Counting Elements in an Array

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 07:53 PM.

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, 11:37 AM
  5. Counting # of elements in array or file.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 05:33 AM