⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex2_14.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -