二维数组定义和声明
- 示例
#include <iostream>
using namespace std;
int main()
{
// 二维数组的定义和初始化
int a[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, { 9, 10, 11, 12}};
for (int i = 0; i < 3 ; i++) {
for (int j = 0; j < 4 ; j++) {
cout<< a[i][j]<< " ";
}
cout<<endl;
}
}
几种二维数组初始化的方式
- 分行赋初始值
int a[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, { 9, 10, 11, 12}};
- 写到一个花括号中
int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
上面要注意元素对应的关系和个数
- 部分赋值, 没赋值的是0
int a[3][4] = {{1, 4}, {3}, {5}};
- 指定列数(必须), 不指定行数
int a[][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
统计两个二维数组中元素相等的个数
#include <iostream>
using namespace std;
bool isEqual(int x, int y){
return x == y? true: false;
}
int main()
{
int a[3][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
b[3][4]={12, 2, 3, 4, 10, 6, 7, 11, 9, 5, 8, 1};
int count = 0 ;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4 ; ++j) {
if (isEqual(a[i][j], b[i][j])){
count++;
}
}
}
cout<< "equals numbers ="<< count<<endl;
}
示例2
//有一个3×4的二维数组,
// 要求编程序求出其中值最小的那个元素,以及其所在的行号和列号
#include <iostream>
using namespace std;
int min_value(int a[][4], int &row, int &col){
int min = a[0][0];
for (int i = 0; i <3 ; ++i) {
for (int j = 0; j < 4 ; ++j) {
if (a[i][j] < min){
min = a[i][j];
row = i;
col = j;
}
}
}
return min;
}
int main()
{
int row = 0, col = 0;
int a[3][4]= {{1, 2, 3, 4}, {-9, 8, 7, 6}, {10, 5, 2, -5}};
// 二维数组名作为函数的参数
cout<< "min_value = "<<min_value(a, row, col)<<endl;
cout<< "row = "<<row<<endl;
cout<< "col = "<<col<<endl;
}
实现矩阵的转置
// 矩阵转置
#include <iostream>
using namespace std;
void transpose(int a[][4], int b[][4]){
for (int i = 0; i < 4 ; ++i) {
for (int j = 0; j < 4 ; ++j) {
// 交换值
b[j][i] = a[i][j] ;
}
}
}
int main(){
// 将a 转置的结果 赋值给b
int a[4][4]={{1, 2, 3, 4}, { 5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 16}};
int b[4][4];
cout<< "matrix a"<< endl;
for (int i = 0; i < 4 ; ++i) {
for (int j = 0; j < 4 ; ++j) {
cout<< a[i][j]<<" ";
}
cout<< endl;
}
// 调用
transpose(a, b);
cout<< "matrix b"<< endl;
for (int i = 0; i < 4 ; ++i) {
for (int j = 0; j < 4 ; ++j) {
cout<< b[i][j]<<" ";
}
cout<< endl;
}
}
第二种方式
// 矩阵转置 原地操作不增加变量的定义
#include <iostream>
using namespace std;
void transpose(int a[][4]){
for (int i = 0; i < 4 ; ++i) {
for (int j = i + 1; j < 4 ; ++j) {
// 注意j 可以从 i开始; 如果从 i+1开始 省略计算了主对角线
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
int main(){
int a[4][4]={{1, 2, 3, 4}, { 5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 16}};
cout<< "matrix a before = "<< endl;
for (int i = 0; i < 4 ; ++i) {
for (int j = 0; j < 4 ; ++j) {
cout<< a[i][j]<<" ";
}
cout<< endl;
}
// 调用
transpose(a);
cout<< "matrix a transposed = "<< endl;
for (int i = 0; i < 4 ; ++i) {
for (int j = 0; j < 4 ; ++j) {
cout<< a[i][j]<<" ";
}
cout<< endl;
}
}
矩阵相乘
// 矩阵乘法运算
#include <iostream>
#define M 4
#define N 3
#define P 5
using namespace std;
// a 是左矩阵 b 是右矩阵, c是结果矩阵
void dot(int a[][3], int b[][5], int c[][5]){
// 因为我们 的结果矩阵c是 4行 5列的
// 我们要将每一个值都算出来, 所以才会有这个2层(i,j)循环
// a.shape = (4, 3) b.shape = (3, 5) c = (4, 5)
for (int i = 0; i < M ; ++i) {
for (int j = 0; j < P ; ++j) {
c[i][j] = 0 ; // 这代表结果矩阵c的第i行第j列的值
// N 是行乘以列 的时候每个行列元素的个数, 是 (m,k)(k,n)z中的k
for (int k = 0; k < N ; ++k) {
c[i][j] = c[i][j] + a[i][k]*b[k][j];
}
}
}
}
int main(){
int a[M][N]={{1, 3, 5}, {2, 4, 6}, {15, 7, 4}, {-2, 8, 9}},
b[N][P]={{3, 6, 2, 1, 7}, {9, 1, 3, -1, 5}, {2, 5, 8, 1, 9}}, c[M][P];
// a.dot(b) = c
// a.shape = (4, 3) b.shape = (3, 5) c = (4, 5)
cout<< "matrix a = "<< endl;
for (int i = 0; i < 4 ; ++i) {
for (int j = 0; j < 3 ; ++j) {
cout<< a[i][j]<<" ";
}
cout<< endl;
}
cout<< "matrix b = "<< endl;
for (int i = 0; i < 3 ; ++i) {
for (int j = 0; j < 5 ; ++j) {
cout<< b[i][j]<<" ";
}
cout<< endl;
}
// 调用相乘函数
dot(a, b, c);
cout<< "matrix c = "<< endl;
for (int i = 0; i < 4 ; ++i) {
for (int j = 0; j < 5 ; ++j) {
cout<< c[i][j]<<" ";
}
cout<< endl;
}
}
网友评论