菜学C++ Day36
- 随机数rand(),头文件
用法: int x=rand()%%a+b,表示x为[b,a-1+b],小数直接先得位数,再除10、100等就可以了#include<stblib.h>
一般要用到随机数种子srand(seed)
其中seed一般为time(0),随时间变化- #include<time.h>
- srand(time(0));
time(0)指函数返回当前时间,如果发生错误返回0
time(1)…………………………………………返回1
- setw()默认右对齐
-
P90 例4-14行指针传递二维数组 函数输入输出
- #include<iostream>
- #include<stdlib.h>
- #include<time.h>
- #include<iomanip>
- using namespace std;
- //行指针传递
- void input(int p[][4], int n) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < 4; j++)
- p[i][j] = rand() %% 100 + 1;
- }
- }
- void output(int (*p)[4], int n) {
- cout << "随机生成的二维数组为:" << endl;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < 4; j++)
- cout <<setw(4)<< p[i][j];
- cout << endl;
- }
- }
- int main() {
- int a[3][4];
- //定义行指针 数据类型 (*指针变量名)[二维数组列数]
- int(*p)[4]=a;
- //时间若返回错误,则返回0
- srand(time(0));
- input(a, 3);
- output(p, 3);
- return 0;
- }
推荐阅读