Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

max value in array columns 1

Status
Not open for further replies.

kjb

Technical User
Oct 24, 2000
11
0
0
CA
I could not figure out how to write a function that counts elements greater than given max value in columns of a matrix separately.

Can somone help me, pls!
I tried:
Code:
int count = 0;
float max = 1.5;
for ( i = 0; i < rows; i++)
for ( j = 0; j < cols; j++)
{
if ( matrix[i][j] > max)
count += 1;
}
[\code]
Above count all the values greater than max but how to report the separately?

Tips/advice would be highly appreciated.

kjb
 
Hi Kjb,

What exactly you want to be reported ?
Do you want the array element to be printed ?
If so try the below code.

int count = 0;
float max = 1.5;
for ( i = 0; i < rows; i++)
for ( j = 0; j < cols; j++)
{
if ( matrix[j] > max)
{
count += 1;
printf(&quot; The maxtrix[%d,%d] value %d is greater than %f\n And count is %d\n, i,j,max,count);
}
}

printf(&quot;No of Elements greater than %f is %d \n&quot;, max, count);

Cheers,
Baskar.


Baskar
baskar52@hotmail.com
 
Thanks Basking

Actually I need to find the count of elements greater than
a given value in 'each' column. In case of an array of 3 columns the return should be like:
2 6 4

Regards

kjb

 
I hope the follwoing code helps you.

int count;
float max = 1.5;
for ( j = 0; j < cols; j++)
{
count = 0;
for ( i = 0; i < rows; i++)
{
if ( matrix[j] > max)
{
count += 1;
printf(&quot; The maxtrix[%d,%d] value %d is greater than %f\n And count is %d\n, i,j,max,count);
}
}
printf(&quot;No of Elements greater than %f in column %d is %d \n&quot;, max, j+1, count);
}
}

cheers,
Baskar

Baskar
baskar52@hotmail.com
 
Many thanks Basking

The function you wrote worked like magic!

Have a good day!

kjb
 
Hello all,

It seems to me you need a count for each column if I have followed this LONG thread correctly. So something like this...

int count[cols];
memset(count, 0, sizeof(int) * cols);

float max = 1.5;
for ( r = 0; r < rows; r++)
for ( j = 0; j < cols; j++)
{
if ( matrix[r][j] > max)
count[j]++;
}

Good luck
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top