Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Program to find the sum, average, minimum and maximum of all elements of

ID: 3598072 • Letter: W

Question

Write a Program to find the sum, average, minimum and maximum of all elements of each row of a matrix. The user first enters the number of rows and columns, then enters all of the data of the matrix. Then the program will print the matrix elements and the sum, the average, the minimum, and the maximum of each row. Please write this in language C, not C+ or C++.

Enter number of Rows2 Enter number of cols2 Enter matrix elements Enter element [1,1] 1 Enter element [1,2] 2 Enter element [2,1] 3 Enter element [2,2]: 1 SUM :3 SUM: 4 Average 1.50 maximum: 2, minimum: 1 Average 2.0 maximum:3, inimum 1 2

Explanation / Answer

Following code achieves the required functions:

#include<stdio.h>

int main()
{
    int r,c;
    printf("Enter the number of Rows : ");
    scanf("%d",&r);                         // Input the number of rows in matrix
    printf("Enter the number of Cols : ");
    scanf("%d",&c);                         // Input the number of cols in matrix
    int matrix[r][c];
    int i,j,temp;                           // temp is used to temporarily accept the element and store it in matrix
    printf(" Enter matrix elements : ");
    for(i=0; i<r; i++)
    {
        for(j=0; j<c; j++)
        {
            printf("Enter element [%d][%d] : ",i+1,j+1);
            scanf("%d",&temp);              // Input the value of temp from user
            matrix[i][j] = temp;            // Store temp as matrix element
        }
    }
    printf(" ");
    for(i=0; i<r; i++)                      // Iteration over each row of matrix
    {
        int rowSum = 0;                     // Declare sum of elements in row as 0
        float avg = 0;                      // Avg of elements in row (note: it is a float variable)
        int max = matrix[i][0];             // declare a max variable and initialize to 1st element of that row
        int min = matrix[i][0];             // declare a min variable and initialize to 1st element of that row
        for(j=0; j<c; j++)                  // Iterate over each element in row
        {
            rowSum+=matrix[i][j];           // Add element to sum of row elements
            if(max < matrix[i][j])          // If element is greater than max, change max to current element
                max = matrix[i][j];
            if(min > matrix[i][j])          // If element is less than min, change min to current element
                min = matrix[i][j];
        }
        avg = ((float)rowSum) / c;          // Calculate average of row as (Sum of elements)/(Total no. of elements in row)

        //Output the required entities as required
        for(j=0; j<c; j++)
        {
            printf("%d   ",matrix[i][j]);
        }
        printf("    -> ");
        printf("SUM : %d    ",rowSum);
        printf("Average : %0.2f   ",avg);
        printf("maximum : %d,    ",max);
        printf("minimum : %d,    ",min);
        printf(" ");
    }
    return 0;
}


Few important notes:

1. The code is commented at every point which might require explanantion to avoid doubts.

2. While calculating average, note that since the variable "avg" is of type 'float', we need to convert either the variable 'rowSum' or 'c' to float because without that 'rowSum/c' would also be an integer as both are integers. In C, when 2 integers are divided, the output is converted to largest integer smaller than the actual answer. So 3/2 is reported as 1 (<1.5) because both 3 and 2 are integers. This problem of precision is solved by casting (converting) one of them to float. Thus ((float)3)/2 tells compiler to first convert 3 to a floating type number (3.00 instead of just 3) and then divide by 2 which is then reported as a float answer which would be 1.50.

Hope this helps.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote