pex2_14.cpp

来自「数据结构C++代码,经典代码,受益多多,希望大家多多支持」· C++ 代码 · 共 43 行

CPP
43
字号
#include <iostream.h>

// find the trace of matrix M, which has a
// maximum of 20 columns
int Trace(int M[][20], int n)
{
	int traceValue = 1;
	
	// form the product of the diagonal elements
	for (int i = 0; i < n; i++)
		traceValue *= M[i][i];
	return traceValue;
}

void main(void)
{
	int mat[20][20];
	int matsize;
	int i,j;
	
	cout << "What is the size of your matrix? ";
	cin >> matsize;
	cout << "Enter the matrix:" << endl << endl;
	
	for (i = 0; i < matsize; i++)
		for (j = 0; j < matsize; j++)
			cin >> mat[i][j];
	cout << endl << "Trace of the matrix is " << Trace(mat,matsize) << endl;
}

/*
What is the size of your matrix? 5
Enter the matrix:

1 2 3 4 5
0 2 6 7 8
1 2 3 5 6
3 5 5 4 1
1 1 2 2 5

Trace of the matrix is 120
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?