C++ 光速入门指南day06

2021-06-13  本文已影响0人  __method__

二维数组

数组里面还是, 类似数学中的矩阵

#include <iostream>
#include<cmath>
using namespace std;
int main(){
    // int arr[] = {1,2, 3};// 一维数组
    int arr[3][4] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
    // 遍历
    for (int i = 0; i < 3 ; ++i) {
        for (int j = 0; j < 4; ++j) {
            int num = arr[i][j]; // 第i行 第j列
            cout<< num << " ";
        }
        cout<< endl;
    }
}

#include <iostream>
#include<cmath>
using namespace std;
int main() {
    //方式1
    //数组类型 数组名 [行数][列数]
    int arr[2][3];
    arr[0][0] = 1;
    arr[0][1] = 2;
    arr[0][2] = 3;
    arr[1][0] = 4;
    arr[1][1] = 5;
    arr[1][2] = 6;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    //方式2
    //数据类型 数组名[行数][列数] = { {数据1,数据2 } ,{数据3,数据4 } };
    int arr2[2][3] = {{1, 2, 3},
                      {4, 5, 6}};
    //方式3
    //数据类型 数组名[行数][列数] = { 数据1,数据2 ,数据3,数据4 };
    int arr3[2][3] = {1, 2, 3, 4, 5, 6};
    //方式4
    //数据类型 数组名[][列数] = { 数据1,数据2 ,数据3,数据4 };
    int arr4[][3] = {1, 2, 3, 4, 5, 6}; // 行可以省略 列不可以
}

⼆维数组数组名

#include <iostream>
#include<cmath>
using namespace std;
int main() {
    //二维数组数组名
    int arr[2][3] ={{1,2,3},{4,5,6}};
    cout << "size = " << sizeof(arr) << endl;
    cout << "size of row " << sizeof(arr[0]) << endl;
    cout << "size of element " << sizeof(arr[0][0]) << endl;
    cout << "row num" << sizeof(arr) / sizeof(arr[0]) << endl;
    cout << "col num" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
    //地址
    cout << "address" << arr << endl;
    cout << "drow address" << arr[0] << endl;
    cout << "first ele" << &arr[0][0] << endl;
    cout << "second ele" << &arr[0][1] << endl;
    cout << "second ele" << &arr[0][2] << endl;
    cout << "second ele" << &arr[0][3] << endl;
}
#include <iostream>
#include<cstring>
using namespace std;
int main() {
    int scores[3][3] ={{100,100,100},{90,50,100},{60,70,80}};
    string names[3] = {"zhangsan", "lisi", "wangwu"};

    for (int i = 0; i < 3 ; ++i) {
        int sum = 0;
        for (int j = 0; j < 3; ++j) {
            sum += scores[i][j];
        }
        cout<< names[i] << " total score = "<<sum <<endl;
    }
}
#include <iostream>
#include<cstring>
using namespace std;
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(a[i][j] == b[i][j])
               count++;
        }
    }
    cout<< "equals number = " << count<< endl;
}

编写程序实现矩阵的转置操作(行变列, 列变行)

#include <iostream>
#include<cstring>
using namespace std;
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;
    }
    cout<<"-------------------------------------------" << endl;
    // 进行转置操作
    for (int i = 0; i < 4 ; ++i) {
        for (int j = 0; j < 4 ; ++j) {
            // 行变列, 列变行
           b[j][i] = a[i][j];
        }
    }
    cout<<"-------------------------------------------" << endl;
    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>
#define M 4
#define N 3
#define P 5
// 这种方式叫做宏定义变量, 他会在main函数执行前完成初始化
using namespace std;
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];
    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<<"-------------------------------------------" << 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;
    }
    // 相乘
    for (int i = 0; i < M; ++i) {
        for(int j = 0; j < P ; ++j) {
            c[i][j] = 0;
            // 这里是N
            for (int k = 0; k < N; ++k) {
                c[i][j] += a[i][k]*b[k][j];
            }
        }
    }

//    int arr[] = {1, 2, 4, 5};
//    int sum = 0;
//    for (int i = 0; i < ; ++i) {
//        sum += arr[i]
//    }

    // 输出结果C
    cout<<"-------------------------------------------" << endl;
    cout<< "matrix C   = "<< endl;
    for (int i = 0; i < M; ++i) {
        for(int j = 0; j < P ; ++j) {
            cout<< c[i][j] << " ";
        }
        cout<< endl;
    }
}

函数

作⽤:将⼀段经常使⽤的代码封装起来,减少重复代码
⼀个较⼤的程序,⼀般分为若⼲个程序块,每个模块实现特定的功能。

函数的定义

1、返回值类型 2、函数名 3、参数列表 4、函数体语句 5、return 表达式
语法

返回值类型 函数名 (参数列表) {
       函数体语句
       return表达式 
}

演示函数的层层补充

#include <iostream>
using namespace std;

void add(){
    cout<< "add execute"<< endl;
}
int main() {
    // 编写函数实现两个数相加的和并返回
    // 调用函数  函数名();
    add();
}

无返回值版的相加

#include <iostream>
using namespace std;
// x, y 形参
void add(int x, int y){
    cout<< x + y<< endl;
}
int main() {
    // 编写函数实现两个数相加的和并返回
    // 调用函数  函数名();
    int a = 100, b = 20;
    add(a, b); // a, b 传入函数实际参数  实参
}

有返回值的

#include <iostream>
using namespace std;
// x, y 形参
int add(int x, int y){
    return x + y;
}
int main() {
    // 编写函数实现两个数相加的和并返回
    // 调用函数  函数名();
    int a = 100, b = 20, sum;
    sum = add(a, b); // a, b 传入函数实际参数  实参

    cout<<sum<< endl;
}


上一篇下一篇

猜你喜欢

热点阅读