定义函数reveal(int * array,int length),遍历数组[14,13,51,20,91,25]中第奇数个元素的值 C语言
时间: 2023-09-17 19:09:57 浏览: 29
以下是定义函数 `reveal` 的 C 语言代码,其功能是遍历数组中第奇数个元素的值:
```c
void reveal(int *array, int length) {
for (int i = 0; i < length; i++) {
if (i % 2 == 0) { // 判断下标是否为奇数
printf("%d ", array[i]);
}
}
}
```
调用这个函数,并传入数组 `[14,13,51,20,91,25]` 和其长度 `6`:
```c
int main() {
int array[] = {14, 13, 51, 20, 91, 25};
int length = 6;
reveal(array, length);
return 0;
}
```
输出结果为:
```
14 51 91
```
相关问题
定义一个函数void reveal(char * string),遍历字符数组char string[]="YouAreGreat!" C语言
下面是实现该函数的 C 语言代码:
```c
#include <stdio.h>
void reveal(char *string) {
int i = 0;
while (string[i] != '\0') {
printf("%c ", string[i]);
i++;
}
}
int main() {
char string[] = "YouAreGreat!";
reveal(string);
return 0;
}
```
函数 `reveal` 接收一个字符指针 `string`,该指针指向一个字符数组。函数使用 `while` 循环遍历数组,直到遇到字符串结尾的空字符 `'\0'` 为止,并使用 `printf` 函数输出每个字符。在 `main` 函数中,我们定义了一个字符数组 `string`,并将其传递给 `reveal` 函数进行遍历输出。
c++二维数组编写扫雷游戏
下面是一个简单的 C++ 二维数组实现扫雷游戏的例子:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ROWS = 10;
const int COLS = 10;
void initBoard(bool board[][COLS], int mines);
void printBoard(bool board[][COLS], bool reveal);
int main() {
srand(time(nullptr)); // 设置随机数种子
bool board[ROWS][COLS];
int mines = 10;
initBoard(board, mines);
printBoard(board, false); // 初始状态,不显示地雷
return 0;
}
void initBoard(bool board[][COLS], int mines) {
// 初始化棋盘
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
board[i][j] = false;
}
}
// 布置地雷
int count = 0;
while (count < mines) {
int row = rand() % ROWS;
int col = rand() % COLS;
if (!board[row][col]) {
board[row][col] = true;
count++;
}
}
}
void printBoard(bool board[][COLS], bool reveal) {
cout << " ";
for (int i = 0; i < COLS; ++i) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < ROWS; ++i) {
cout << i << " ";
for (int j = 0; j < COLS; ++j) {
if (board[i][j]) {
if (reveal) {
cout << "* ";
} else {
cout << ". ";
}
} else {
cout << "- ";
}
}
cout << endl;
}
}
```
在这个例子中,我们定义了一个 `bool` 类型的二维数组 `board`,表示扫雷游戏的棋盘。数组的每个元素代表一个方格,值为 `true` 表示这个方格上有地雷,否则没有地雷。
`initBoard()` 函数用来初始化棋盘,随机布置指定数量的地雷。
`printBoard()` 函数用来打印棋盘。如果 `reveal` 参数为 `true`,表示要显示所有的地雷,否则只显示未被翻开的方格。
在 `main()` 函数中,我们设置随机数种子,然后调用 `initBoard()` 函数初始化棋盘,并调用 `printBoard()` 函数打印初始状态的棋盘。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)