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. 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.

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]);
}```

2. 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. would you like me to include the entire code, and the excel file with the raw data in it?

4. 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. 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]);
}```

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];
}```

7. 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