在新数组中获取随机值

Getting the random values in the new array

本文关键字:获取 随机 数组 新数组      更新时间:2023-10-16

我正在从数组中创建一个新数组。而且我得到了随机值。(类似-858993460)这是我的代码

#include<iostream>
using namespace std;
int main() {
    const int m = 8;
        int i, k;
        int A[m] = { 1,2,3,4,5,6,7,8 };
    int B[m];
    k = 0;
    for (i = 0; i < m; i++) {
        if (A[i] % 2 == 0) {
            A[i] = B[i];
            k = k + 1;
        }
    }
    for (i = 0; i < k; i++) {
        cout << B[k];
    }
    cout << endl;
    return 0;
}

可能是您想要的。

#include<iostream>
using namespace std;
int main() {
    const int m = 8;
    int i, k;
    int A[m] = { 1,2,3,4,5,6,7,8 };
    int B[m];
    k = 0;
    for (i = 0; i < m; i++) {
        if (A[i] % 2 == 0) {
            B[k] = A[i];
            k = k + 1;
        }
    }
    for (i = 0; i < k; i++) {
        cout << B[i];
    }
    cout << endl;
    return 0;
}