How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

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:

  1. The matrix structure is defined to represent a matrix. It contains three members: rows and cols to store the number of rows and columns in the matrix, respectively, and dataSet to store the matrix data as a 2D array.

  2. The readMatrix() function takes the name of a file containing the matrix data as input and returns a matrix 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.

  3. The printMatrix() function takes a matrix structure as input and prints the matrix data to the console.

  4. The findSum() function takes a matrix 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.

  5. The main() function takes the name of the file containing the matrix data as a command-line argument. It calls the readMatrix() function to read the matrix data into a matrix structure, calls the printMatrix() function to print the matrix data, and calls the findSum() 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