Home > Enterprise >  How to count how many the non-zero value in a row in 2d array in C
How to count how many the non-zero value in a row in 2d array in C

Time:09-30

How can I calculate how many non-zero values include in a row

I try to calculate the average of the total value inside a row but somehow it is always dividend in 5 How can I create a counter to count how many non-zero value inside the array?

Sorry for my bad english

int main () 
{
    float Sum, average;

    float a[4][5] = { {2.30,4.00,5.00},
                      {3.00,2.10,1.00,4.00},
                      {1.00,2.00,3.00,4.00,5.00},
                      {4.00,2.50}};
    int rows, columns;
    int length;
 
 
    for(rows = 0; rows < 4; rows  )
    {
        Sum = 0;
        for(columns = 0; columns < 5; columns  )
        {
        
            Sum = Sum   a[rows][columns];
        }
        
        printf("The Sum of rows Elements in a Matrix =  %.2f \n", Sum );
        average = Sum / columns;
        printf("Product %d : %f\n",rows   1,average);
    }
    

      
      return 0;
   }

The output cames out is:

The Sum of rows Elements in a Matrix =  11.30
Product 1 : 2.260000
The Sum of rows Elements in a Matrix =  10.10
Product 2 : 2.020000
The Sum of rows Elements in a Matrix =  15.00
Product 3 : 3.000000
The Sum of rows Elements in a Matrix =  6.50
Product 4 : 1.300000

But I was aspect it will comes out

The Sum of rows Elements in a Matrix =  11.30
Product 1 : 3.77
The Sum of rows Elements in a Matrix =  10.10
Product 2 : 2.53
The Sum of rows Elements in a Matrix =  15.00
Product 3 : 3.00
The Sum of rows Elements in a Matrix =  6.50
Product 4 : 3.25

CodePudding user response:

Your example suggests that the non-zero values are first in the array. If that is true you can stop the loop at the first value being zero.

Change

columns < 5

to

columns < 5 && a[rows][columns] != 0

BTW... You should check that columns isn't zero before the division

CodePudding user response:

Try the following code:

int main () 
{
    float Sum, average;

    float a[4][5] = { {2.30,4.00,5.00},
                      {3.00,2.10,1.00,4.00},
                      {1.00,2.00,3.00,4.00,5.00},
                      {4.00,2.50}};
    int rows, columns, nonZeroCount;
    int length;
 
 
    for(rows = 0; rows < 4; rows  )
    {
        Sum = 0;
        nonZeroCount = 0;
        for(columns = 0; columns < 5; columns  )
        {
            if(a[rows][columns] != 0)
                nonZeroCount  ;
            Sum = Sum   a[rows][columns];
        }
        
        printf("The Sum of rows Elements in a Matrix =  %.2f \n", Sum );
        average = Sum / nonZeroCount;
        printf("Product %d : %f\n",rows   1,average);
    }
      return 0;
   }

CodePudding user response:

I hope that this is a solution that you are looking for:

int counter_row(float row[5], int n){
    int count=0;
//row vaiable is the row array
    for (auto i=0; i<n; i  ){
         // loop each element of row and check it's not equal to 0, if true then increase the count
        if (row[i]!=0)
        {
            count  ;
        }
    }    
    return count;
}

You can use it:


int main () 
{
    float Sum, average;

    float a[4][5] = { {2.30,4.00,5.00},
                      {3.00,2.10,1.00,4.00},
                      {1.00,2.00,3.00,4.00,5.00},
                      {4.00,2.50}};
    int rows, columns;
    int length;
 
 
    for(rows = 0; rows < 4; rows  )
    {
        Sum = 0;
        for(columns = 0; columns < 5; columns  )
        {
            Sum = Sum   a[rows][columns];
        }
        
        printf("The Sum of rows Elements in a Matrix =  %.2f \n", Sum );
        
        int amount = counter_row(a[rows], 5);
        average = Sum / amount;
        printf("Product %d : %f\n",rows   1,average);
  }
    

   return 0;
}

another solution

int main () 
{
    float Sum, average;

    float a[4][5] = { {2.30,4.00,5.00},
                      {3.00,2.10,1.00,4.00},
                      {1.00,2.00,3.00,4.00,5.00},
                      {4.00,2.50}};
    int rows, columns;
    int length;
 
 
    for(rows = 0; rows < 4; rows  )
    {
        Sum = 0;
        // init amount variable
        int amount=0;
        for(columns = 0; columns < 5; columns  )
        {
           // condition for increase amount variable is it's not equal to 0, because we counting non-zero element
          // a[rows][columns] being element of the row needed count
            if (a[rows][columns]!=0)
                amount  ;
            Sum = Sum   a[rows][columns];
        }
        
        printf("The Sum of rows Elements in a Matrix =  %.2f \n", Sum );
        // finally, amount variable is non-zero amount
        average = Sum / amount;
        printf("Product %d : %f\n",rows   1,average);
    }
    

      return 0;
   }

have a nice day!!!

CodePudding user response:

For starters declare variables in minimal scopes where they are used. For example the variables Sum and average are used within the for loops. So they should be declared in the scope of the outer for loop.

The variable length is not used in the program. So remove its declaration.

Do not use magic numbers like 4 or 5. Instead use named constants.

As for your problem then just introduce one more variable within loops that will count the number of non-zero elements in a row.

Here is a demonstrative program.

#include <stdio.h>

int main () 
{
    enum { ROWS = 4, COLS = 5 };
    float a[ROWS][COLS] = 
    { 
        { 2.30, 4.00, 5.00 },
        { 3.00, 2.10, 1.00 ,4.00 },
        { 1.00, 2.00, 3.00, 4.00, 5.00 },
        { 4.00, 2.50 }
    };
        
    for ( size_t row = 0; row < ROWS; row   )
    {
        float Sum = 0.0f;
        size_t total = 0;
        
        for ( size_t col = 0; col < COLS; col  )
        {
            if ( a[row][col] != 0 )
            {   
                Sum  = a[row][col];
                  total;
            }
        }
        
        printf( "The Sum of rows Elements in a Matrix =  %.2f \n", Sum );
        float average =total == 0 ? 0.0f :  Sum / total;
        printf( "Product %zu : %.2f\n", row   1, average );
    }
    
    return 0;
}

The program output is

The Sum of rows Elements in a Matrix =  11.30 
Product 1 : 3.77
The Sum of rows Elements in a Matrix =  10.10 
Product 2 : 2.53
The Sum of rows Elements in a Matrix =  15.00 
Product 3 : 3.00
The Sum of rows Elements in a Matrix =  6.50 
Product 4 : 3.25

CodePudding user response:

I try to calculate the average of the total value inside a row

A 0.0 can be a valid value that can exists in an array whose elements average you are calculating. This won't affect the sum of values of that array elements but if you are calculating the average of elements, then they have their influence on it. [All the posted answers won't work if 0.0 will be a member of array. ]. For e.g.

The first row of your 2D array is

{2.30, 4.00, 5.00}

The average is

(2.30   4.00   5.00) / 3 = 3.77

But what happens when this row contain a member with value 0.0 :

{2.30, 4.00, 0.0, 5.00}  

[or may be like this - {2.30, 4.00, 5.00, 0.0}]

Now the average is

(2.30   4.00   0.0   5.00) / 4 = 2.83

Instead of having 2D array, you can have all individual 1D arrays which can be of different size and create a structure which will have a pointer to first element of array and, along with it, the information about size of respective array. Create an array of these structures and iterate through it and calculate the sum and average of array elements.

#include <stdio.h>

typedef struct s {
    float * ptr;
    size_t size;
} ST;

float summation (float * p, size_t s) {
    float sum = 0.0f;
    for (size_t i = 0; i < s;   i) {
        sum = sum   p[i];
    }

    return sum;
}

int main (void) {
    float a1[] = {2.30, 4.00, 5.00};
    float a2[] = {3.00, 2.10, 1.00, 4.00};
    float a3[] = {1.00, 2.00, 3.00, 4.00, 5.00};
    float a4[] = {4.00, 2.50};
    float a5[] = {2.30, 4.00, 0.0, 5.00};

    ST a[] = {{a1, sizeof (a1) / sizeof (a1[0])},
              {a2, sizeof (a2) / sizeof (a2[0])},
              {a3, sizeof (a3) / sizeof (a3[0])},
              {a4, sizeof (a4) / sizeof (a4[0])},
              {a5, sizeof (a5) / sizeof (a5[0])}};

    for (size_t rows = 0; rows < sizeof (a) / sizeof (a[0]);   rows) {
        float sum = summation (a[rows].ptr, a[rows].size);
        printf ("The Sum of row elements is =  %.2f \n", sum);
        printf ("Average %zu : %.2f\n", rows   1, sum/a[rows].size);
    }

    return 0;
}

Output:

% ./a.out
The Sum of row elements is =  11.30 
Average 1 : 3.77
The Sum of row elements is =  10.10 
Average 2 : 2.53
The Sum of row elements is =  15.00 
Average 3 : 3.00
The Sum of row elements is =  6.50 
Average 4 : 3.25
The Sum of row elements is =  11.30 
Average 5 : 2.83
  • Related