How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the C programming language
How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the C programming language
Table of Contents
Problem Statement
Find and display the sum of elements that are below the main diagonal of a matrix. The matrix should be a square matrix.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the C programming language
The code snippet you provided is a C program that reads a matrix from a file, prints it, and finds the sum of the elements below the main diagonal.
Here's a breakdown of the code:
-
The
matrix
structure is defined to represent a matrix. It contains three members:rows
andcols
to store the number of rows and columns in the matrix, respectively, anddataSet
to store the matrix data as a 2D array. -
The
readMatrix()
function takes the name of a file containing the matrix data as input and returns amatrix
structure filled with the data. It opens the file, reads the number of rows and columns, allocates memory for the matrix data, and reads the data from the file. -
The
printMatrix()
function takes amatrix
structure as input and prints the matrix data to the console. -
The
findSum()
function takes amatrix
structure as input and returns the sum of the elements below the main diagonal. It iterates through the matrix and adds the elements below the main diagonal to the sum. -
The
main()
function takes the name of the file containing the matrix data as a command-line argument. It calls thereadMatrix()
function to read the matrix data into amatrix
structure, calls theprintMatrix()
function to print the matrix data, and calls thefindSum()
function to find the sum of the elements below the main diagonal.
Here's an example of how the program can be used:
$ ./matrix_sum input.txt
Matrix is :
1 2 3
4 5 6
7 8 9
Sum below main diagonal : 20
In this example, the input file input.txt
contains the following data:
3 3
1 2 3
4 5 6
7 8 9
The program reads the matrix data from the file and prints it to the console. It then finds the sum of the elements below the main diagonal, which is 20.
Source code in the c programming language
#include<stdlib.h>
#include<stdio.h>
typedef struct{
int rows,cols;
int** dataSet;
}matrix;
matrix readMatrix(char* dataFile){
FILE* fp = fopen(dataFile,"r");
matrix rosetta;
int i,j;
fscanf(fp,"%d%d",&rosetta.rows,&rosetta.cols);
rosetta.dataSet = (int**)malloc(rosetta.rows*sizeof(int*));
for(i=0;i<rosetta.rows;i++){
rosetta.dataSet[i] = (int*)malloc(rosetta.cols*sizeof(int));
for(j=0;j<rosetta.cols;j++)
fscanf(fp,"%d",&rosetta.dataSet[i][j]);
}
fclose(fp);
return rosetta;
}
void printMatrix(matrix rosetta){
int i,j;
for(i=0;i<rosetta.rows;i++){
printf("\n");
for(j=0;j<rosetta.cols;j++)
printf("%3d",rosetta.dataSet[i][j]);
}
}
int findSum(matrix rosetta){
int i,j,sum = 0;
for(i=1;i<rosetta.rows;i++){
for(j=0;j<i;j++){
sum += rosetta.dataSet[i][j];
}
}
return sum;
}
int main(int argC,char* argV[])
{
if(argC!=2)
return printf("Usage : %s <filename>",argV[0]);
matrix data = readMatrix(argV[1]);
printf("\n\nMatrix is : \n\n");
printMatrix(data);
printf("\n\nSum below main diagonal : %d",findSum(data));
return 0;
}
You may also check:How to resolve the algorithm Fibonacci word step by step in the Racket programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Processing programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the Oz programming language
You may also check:How to resolve the algorithm Character codes step by step in the FutureBasic programming language