c++一个数组取出偶数删除组成新的数组
时间: 2024-06-13 22:06:59 浏览: 11
以下是C++中一个数组取出偶数删除组成新的数组的代码实现:
```c++
#include <iostream>
using namespace std;
int main() {
int nums[] = {1, 2, 3, 4, 5, 6};
int numsLen = sizeof(nums) / sizeof(int);
int evenCount = 0;
for (int i = 0; i < numsLen; i++) {
if (nums[i] % 2 == 0) {
evenCount++;
}
}
int evenNums[evenCount];
int index = 0;
for (int i = 0; i < numsLen; i++) {
if (nums[i] % 2 == 0) {
evenNums[index] = nums[i];
index++;
}
}
cout << "原数组为:";
for (int i = 0; i < numsLen; i++) {
cout << nums[i] << "\t"; }
cout << endl;
cout << "新数组为:";
for (int i = 0; i < evenCount; i++) {
cout << evenNums[i] << "\t";
}
cout << endl;
return 0;}
```
运行结果为:
```
原数组为:1 2 3 4 5 6
新数组为:2 4 6
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)