Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:02:22+00:00 2026-06-02T02:02:22+00:00

the program calculates the average geometric mean of negative elements in a 2×2 matrix,i

  • 0

the program calculates the average geometric mean of negative elements in a 2×2 matrix,i am trying to understand code in this program below and why the author wrote what he wrote,may someone explain to me what this code below does im finding it hard to understand
what the author wrote too many pointers in the code

#include <stdio.h>
#include <math.h>

#define ROWS 2
#define COLS 2
char * last_geom_err = NULL;

float geometricMean(float * arr, int rows, int cols){
    float neg_mul = 1;
    int neg_count = 0;
    float arr_elem;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            if((arr_elem = *(arr + i * cols + j)) < 0){
                neg_mul *= arr_elem;
                neg_count++;
            }
    if (neg_count == 0){
        last_geom_err = "no negative elements in array";
        return 0;
    }   
    if ((neg_count % 2 == 0) && (neg_mul < 0)){
        last_geom_err = "a negative number under the square root of even degree";
        return 0;
    }
    last_geom_err = NULL;
    return pow(neg_mul, (float)neg_count);
}

int main(){
    float arr[ROWS][COLS] = {
        1., -2.,
        -5., -6.
    };
    printf("Array:\n");
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++)
            printf("%5.2f ", arr[i][j]);
        putchar('\n');
    }
    float gm = geometricMean((float*)arr, ROWS, COLS);
    if (last_geom_err != NULL)
        printf("#Error of calculation: %s", last_geom_err);
    else
        printf("Geometric mean of negative elements of array = %5.2f", gm);
    return 0; 
}

why this?

char * last_geom_err = NULL;

    float geometricMean(float * arr, int rows, int cols){
        float neg_mul = 1;
        int neg_count = 0;
        float arr_elem;
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                if((arr_elem = *(arr + i * cols + j)) < 0){
                    neg_mul *= arr_elem;
                    neg_count++;
                }
        if (neg_count == 0){
            last_geom_err = "no negative elements in array";
            return 0;
        }   
        if ((neg_count % 2 == 0) && (neg_mul < 0)){
            last_geom_err = "a negative number under the square root of even degree";
            return 0;
        }
        last_geom_err = NULL;
        return pow(neg_mul, (float)neg_count);
    }

and this one

float gm = geometricMean((float*)arr, ROWS, COLS);
        if (last_geom_err != NULL)
            printf("#Error of calculation: %s", last_geom_err);
        else
            printf("Geometric mean of negative elements of array = %5.2f", gm);
        return 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:02:24+00:00Added an answer on June 2, 2026 at 2:02 am

    the program calculates the average geometric mean of negative elements in a 2×2 matrix

    It doesn’t. First, usually the geometric mean applies only to positive numbers. One can extend the definition in a somewhat meaningful way to negative numbers by saying the geometric mean of k negative numbers is the negative of the geometric mean of the absolute values, but what the geometric mean of a set containing both negative and positive numbers should be is unclear. Another meaningful way of extending the geometric mean would be an extension as a holomorphic function to its domain of holomorphy (which for k > 1 wouldn’t be a subset of ℂk). That would include the former extension as the value on one branch above ℝ< 0k.

    Anyway a calculation of a geometric mean would include a k-th root in some form, which the given programme doesn’t. Now let’s look at the code.

    float geometricMean(float * arr, int rows, int cols){
        float neg_mul = 1;
        int neg_count = 0;
    

    Initialisation of the product of negative array elements and their count.

        float arr_elem;
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
    

    The memory layout of the matrix is

            --------------------------------------------
    arr -> | row 0 | row 1 | row 2 | ... | row (rows-1) |
            --------------------------------------------
    

    So row 0 occupies slots 0 to cols - 1, row 1 occupies the slots cols to 2*cols - 1, generally, row k occupies the slots k*cols to (k+1)*cols - 1. Thus arr + i*cols + j points to col j in row i.

                if((arr_elem = *(arr + i * cols + j)) < 0){
                    neg_mul *= arr_elem;
                    neg_count++;
                }
    

    Read matrix element a[i][j] and if it is negative, multiply it to the product of all negative entries and count it. Note that due to the flat memory layout, one could simply loop for(k = 0; k < rows*cols; ++k) and access arr[k] here.

        if (neg_count == 0){
            last_geom_err = "no negative elements in array";
            return 0;
        }
    

    If the array contains no negative elements at all, set the error message and return. A (geometric) mean of no numbers isn’t meaningful at all.

        if ((neg_count % 2 == 0) && (neg_mul < 0)){
            last_geom_err = "a negative number under the square root of even degree";
            return 0;
        }
    

    If the number of negative entries is even and the product of negative entries is negative, set the error message and return.

    Note that this is dead code. The product of an even number of negative numbers is always positive and the only caveat in floating point arithmetic (IEEE 754 conforming or sufficiently close to that; if it’s totally broken, anything could happen) is underflow, the product may become 0 although mathematically it isn’t. (Overflow isn’t an issue here, the infinities compare to 0 and behave in multiplications as they should.)

        last_geom_err = NULL;
        return pow(neg_mul, (float)neg_count);
    }
    

    Finally, set the error message to NULL, since no exceptional situation occurred and return

    (product of negative entries)(number of negative entries).

    For the geometric mean, the last line should resemble

        return pow(neg_mul, 1.0/neg_count);
    

    however, that would return a NaN if neg_mul < 0 since pow handles negative bases only for integer exponents, so

    if (neg_mul < 0) {
        return -pow(-neg_mul, 1.0/neg_count);
    } else {
        return pow(neg_mul, 1.0/neg_count);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a program which calculates the mean and moment by
I'm trying to use the below code to calculate the average of a set
I am trying to write a python program that calculates a histogram, given a
I'm having some trouble returning multiple values in this program that calculates min, max,
I built a little program that calculates the average of 15 numbers or less.
This program will calculate the average grade for 4 exams using a for loop
i want to create a program which calculates the average of N random numbers
When i run the code as is my output is: This program will calculate
I am trying to understand a point, i know that in this case it
Write a program that calculates Euler’s number e. To do this, first write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.