C++ 如何使用数组解决这个问题?

C++ 如何使用数组解决这个问题?,c++,arrays,C++,Arrays,我是编程新手,有没有办法解决这个问题: 从用户处获取10个整数输入并打印以下内容 正数的数目 负数的数目 奇数的数目 偶数个数 #包括 使用名称空间std; int main() { 国际努马拉[10]; cout您可以在没有阵列的情况下执行此操作,但为了清晰起见,我将坚持您的原始意图 #include <iostream> #include <array> using namespace std; int main() { array<int, 10&g

我是编程新手,有没有办法解决这个问题:

从用户处获取10个整数输入并打印以下内容

  • 正数的数目
  • 负数的数目
  • 奇数的数目
  • 偶数个数
  • #包括
    使用名称空间std;
    int main()
    {
    国际努马拉[10];
    
    cout您可以在没有阵列的情况下执行此操作,但为了清晰起见,我将坚持您的原始意图

    #include <iostream>
    #include <array>
    using namespace std;
    
    int main()
    {
        array<int, 10> numArray; //an array of ints, size 10. this is CPP style array, it is 'safer' than C-style array. (someone correct me)
    
        //get your 10 inputs.
        cout << "Enter Number :";
        for(size_t i = 0; i < numArray.size(); i++)
        {
            cin >> numArray[i];
        }
    
        int positive_count = 0;
        int negative_count = 0;
        int even_count = 0;
        int odd_count = 0;
        
        //loop thru your 10 inputs, increment counters accordingly.
        for(size_t i = 0; i < numArray.size(); i++)
        {
            
            if (numArray[i] < 0)
            {
                negative_count += 1;
            }
            if (numArray[i] > 0)
            {
                positive_count += 1;
            }
    
            if (numArray[i] % 2 == 0)
            {
                even_count += 1;
            }
            else 
            {
                odd_count += 1;
            }
        
        }
        
        cout << "Positive Number " << positive_count << endl;
        cout << "Negative Number " << negative_count << endl;
        
        cout << "even number " << even_count << endl;  
        cout << "Odd number " << odd_count << endl;
        
        return 0;
       
    }
    
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    array numArray;//一个整数数组,大小为10。这是CPP样式的数组,比C样式的数组“安全”。(有人纠正我)
    //获取你的10个输入。
    库特>努马拉伊[i];
    }
    int正计数=0;
    int负计数=0;
    整数偶数=0;
    int奇数计数=0;
    //循环通过10个输入,相应地增加计数器。
    对于(size_t i=0;i0)
    {
    正计数+=1;
    }
    如果(努马拉伊[i]%2==0)
    {
    偶数计数+=1;
    }
    其他的
    {
    奇数计数+=1;
    }
    }
    
    cout您可以在输入循环中执行此操作:

    int positives = 0, zeros = 0, odds = 0;
    int negatives = 0, evens = 0;
    for(size_t i = 0; i < numArray.size(); i++)
    {
        cin >> numArray[i];
    
        // increment number of odds
        odds += numArray[i] & 0x1;
        // odds += numArray[i] % 2;
    
        // increment number of positives
        positives += (numArray[i] > 0);
        // positives += (numArray[i] > 0 ? 1 : 0);
    
        // increment zeros
        zeros += !(numArray[i] | 0);
    }
    
    evens = 10 - odds;
    negatives = 10 - zeros - positives;
    
    cout << "Positive count " << positives << endl;
    cout << "Negative Number " << negatives << endl;
    
    cout << "Even number " << evens << endl;  
    cout << "Odd number " << odds << endl;
    
    int正数=0,零=0,赔率=0;
    整数负=0,偶数=0;
    对于(size_t i=0;i>努马拉伊[i];
    //赔率增量
    赔率+=numArray[i]&0x1;
    //赔率+=numArray[i]%2;
    //增加正数
    正值+=(numArray[i]>0);
    //正数+=(努马拉伊[i]>0?1:0);
    //增量零
    零+=!(numArray[i]| 0);
    }
    evens=10-赔率;
    负=10-零-正;
    
    提示:创建整数计数器,每次看到满足条件的内容时,计数器都会递增。按照@Meowmere的建议,您会发现根本不需要数组。您认为这有什么用?
    i
    (int i=0;i是否要将正数、负数赔率和偶数存储在单独的数组中?上面的问题很好,为什么需要数组?是
    0
    positive numer吗?#include与仅在main方法中创建数组有什么区别?#include是一个预处理器指令。它告诉编译器“嘿,我希望e库名为array。如果您只是创建一个没有#include的数组,您将得到编译错误,因为计算机不知道这个名称“array”是什么,也不知道它在哪里。Alexis除了我上面的
    0
    点之外。我建议合并输入循环和计数循环。@KPCT 0既不是正的,也不是负的。如果您询问非负整数s、 包括0。如果需要非正整数,也包括0。但是,0既不是正整数,也不是负整数。
    int positives = 0, zeros = 0, odds = 0;
    int negatives = 0, evens = 0;
    for(size_t i = 0; i < numArray.size(); i++)
    {
        cin >> numArray[i];
    
        // increment number of odds
        odds += numArray[i] & 0x1;
        // odds += numArray[i] % 2;
    
        // increment number of positives
        positives += (numArray[i] > 0);
        // positives += (numArray[i] > 0 ? 1 : 0);
    
        // increment zeros
        zeros += !(numArray[i] | 0);
    }
    
    evens = 10 - odds;
    negatives = 10 - zeros - positives;
    
    cout << "Positive count " << positives << endl;
    cout << "Negative Number " << negatives << endl;
    
    cout << "Even number " << evens << endl;  
    cout << "Odd number " << odds << endl;
    
    int positives = 0, zeros = 0, odds = 0;
    int negatives = 0, evens = 0;
    for(size_t i = 0; i < numArray.size(); i++)
    {
        cin >> numArray[i];
    
        // increment number of odds
        if (numArray[i] % 2 == 1)
            odds++;
    
        if (numArra[i] == 0)
            zeros++;
        else if (numArray[i] > 0)
            positives++;
    }
    
    evens = 10 - odds;
    negatives = 10 - zeros - positives;
    
    cout << "Positives " << positives << '\n';
    cout << "Negatives " << negatives << '\n';
    
    cout << "Evens " << evens << '\n';  
    cout << "Odds " << odds << '\n';