Thread: Help Counting Elements in an Array

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

    Help Counting Elements in an Array

    hey guys if i have a data file called "data" for instance. which had 15 cols and 15 rows. if each cell had a number assigned to it (1,0,-1) and i wanted to count how many 1's, 0's and -1's are in each respective column and row, how would i go about it?

    cheers

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    First you should try. If you find any difficulties, you need to ask here.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    Ok so this is what i have done so far. Please notify if you see any errors


    Code:
    /*Declare Variables*/
    /* Stores the counts FOR each row FOR 1, 0 and -1 */
    float RowCounts[MAX_ROWS][MAX_COUNTS];
    /* Stores the counts FOR each col FOR 1, 0 and -1 */
    float ColCounts[MAX_ROWS][MAX_COUNTS];
    
    /*Declare Constants*/
    /* Data 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
    
    for(row = 0; row < MAX_ROWS; row++)
        {
            for(col = 0; col < MAX_COLS; col++)
            {
                if(Data[row][col] == MIN_SYM)
                {
                    RowCounts[row][MIN_SYM_INDEX] = (RowCounts[row][MIN_SYM_INDEX] +1);
                }
                else if (Data[row][col] == MAX_SYM)
                {
                    RowCounts[row][MAX_SYM_INDEX] = (RowCounts[row][MAX_SYM_INDEX] +1);
                }
                else
                {
                    RowCounts[row][MIDDLE_SYM_INDEX] = (RowCounts[row][MIDDLE_SYM_INDEX] +1);
                }
            }
        }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This sounds similiar to a question that you already posed. Okay, so maybe you want to restart from scratch. My take:
    Code:
    Read from file into a 2D array of integers. Let's name this array input.
    Create number_of_rows arrays of 3 arrays of integers for the row_counts, zero initialising the elements.
    Create number_of_columns arrays of 3 arrays of integers for the column_counts, zero initialising the elements.
    For each row
        For each column in the current row
            Let x = 0, 1, or 2 if input[row][column] is 1, 0, -1, respectively.
            Increment row_counts[row][x]
            Increment column_counts[column][x]
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you have no idea how to go about this, then I would suggest breaking up this task into several smaller steps.

    It might be best to start with a variable in the program that contains the "data" and work on counting the occurrences. You can make a function to handle this, and test it with "main()".

    When you have that part working, write more code (preferably in its own function) to read values from a data file.

    When that works, just take the data you get reading the file and plug it into the first function you wrote.

    Break the task up into smaller pieces and just focus on one at a time. As you get each one fully working, you can start to string them together to make your final program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting Elements in an Array
    By sammells in forum C Programming
    Replies: 6
    Last Post: 09-06-2012, 02:36 AM
  2. Counting array elements
    By katipo in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 07:10 PM
  3. Counting elements in array
    By axe in forum C Programming
    Replies: 11
    Last Post: 11-14-2007, 10:17 PM
  4. counting certain elements from a file
    By greenstock in forum C++ Programming
    Replies: 17
    Last Post: 01-01-2006, 08: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