if non zero elements in same column count only once
- by George
I want to check the elements above the main diagonal and if I found non zero values , count one.
If the non zero values are found in the same column ,then count just one ,not the number of the non zero values.
For example , it should be count = 2 and not 3 in this example because 12 and 6 are in the same column.
A=
1 11 12
4 5 6
0 7 0
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main( int argc, const char* argv[] ){
int Rows = 3 , Cols = 3;
float *A = (float *) malloc ( Rows * Cols * sizeof (float) );
A[0] = 1.0;
A[1] = 11.0;
A[2] = 12.0;
A[3] = 4.0;
A[4] = 5.0;
A[5] = 6.0;
A[6] = 0.0;
A[7] = 7.0;
A[8] = 0.0;
// print input matrix
printf("\n Input matrix \n\n");
for ( int i = 0; i < Rows; i++ )
for ( int j = 0; j < Cols; j++ ) {
printf("%f\t",A[ i * Cols + j ]);
if( j == Cols-1 )
printf("\n");
}
printf("\n");
int count = 0;
for ( int j = 0 ; j < Cols; j++ )
{
for ( int i = ( Rows - 1 ); i >= 0; i-- )
{
// check the diagonal elements above the main diagonal
if ( j > i )
{
if ( ( A[ i * Cols + j ] != 0 ) )
{
printf("\n Above nonzero Elmts = %f\n",( A[i * Cols + j] ) );
count++;
}
}
}
}
printf("\ncount = %d\n",count );
return 0;
}