繁体   English   中英

练习:将数组值拆分为正数组和负数组

[英]exercise: split the array values into positive and negative arrays

我一直很难思考如何解决此问题,我需要将负值和正值分开,但是当我启动该程序时,结果是错误的,对编程而言是如此之新,尤其是在数组中,所以任何帮助将不胜感激。

这是练习的问题:a)定义一个最大为20个整数值的数组,并用您自己选择的数字填充该数组作为初始化器,然后编写,编译并运行一个c ++程序,该程序读取数组中的数字并将所有最后,在名为正数的数组中包含零和正数,最后在名为负数的数组中包含所有负数,让程序在正负数组中同时显示值

这是代码:

#include <iostream>

using namespace std;

int main()
{
const int MAXVAL = 5; // created 5 values for num, pos, neg i set to 5 because for testing purposes

int num[MAXVAL]; 
int pos[MAXVAL];
int neg[MAXVAL];

int i, k, c, c2, j;

c = 0;
c2 = 0;

cout << "enter the numbers: " << endl;

for(i = 0; i < MAXVAL; i++) // inputs the users
{
    cin >> num[i];
}

for(i = 0, k = 0; i < MAXVAL; i++){ // this function finds the positivevalue
    if(num[k] > -1){
        pos[k] = num[k];
        k++;
        c = c + 1;
    }
    else{
        pos[k] = pos[k];
        k++;
    }
}

for(i = 0, j = 0; i < MAXVAL; i++){ // this function finds the negative        value
    if(num[j] < 0){
        neg[j] = num[j];
        j++;
        c2 = c2 + 1;
    }
    else{
        neg[j] = neg[j];
        j++;
    }
}

cout << "the positive numbers is: " << endl;////// displays the result

for(i = 0; i < c; i++){
    cout << " " << pos[i];
}

cout << "\nthe negative numbers is: " << endl;

for(i = 0; i < c2; i++){
    cout << " " << neg[i];
}

return 0;

}

输出是这样的:

输入数字:1,-5,4,-55,5

正数是:1 8 4

负数是:4286352 -5

首先,已经在标头<algorithm>中声明了标准功能std::partition_copy ,该功能可以完成此工作。 这是一个例子

#include <iostream>
#include <algorithm>
#include <iterator>

int main() 
{
    const size_t MAX_VAL = 5;
    int num[MAX_VAL] = { 1, -5 , 4, -55, 5 };
    int pos[MAX_VAL];
    int neg[MAX_VAL];

    for ( int x : num ) std::cout << x << ' ';
    std::cout << std::endl;

    auto p = std::partition_copy( std::begin( num ), std::end( num ),
                                  std::begin( pos ), std::begin( neg ),
                                  []( int x ) { return !( x < 0 ); } );

    for ( int *first = pos; first != p.first; ++first ) std::cout << *first << ' ';
    std::cout << std::endl;

    for ( int *first = neg; first != p.second; ++first ) std::cout << *first << ' ';
    std::cout << std::endl;

    return 0;
}

程序输出为

1 -5 4 -55 5 
1 4 5 
-5 -55 

如果您想在不使用标准算法的情况下自己编写程序,则该方法可能看起来像

#include <iostream>

int main() 
{
    const size_t MAX_VAL = 5;
    int num[MAX_VAL] = { 1, -5 , 4, -55, 5 };
    int pos[MAX_VAL];
    int neg[MAX_VAL];

    for ( int x : num ) std::cout << x << ' ';
    std::cout << std::endl;


    size_t n1 = 0, n2 = 0;

    for ( size_t i = 0; i < MAX_VAL; i++ )
    {
        if ( !( num[i] < 0 ) )
        {
            pos[n1++] = num[i];
        }
        else
        {
            neg[n2++] = num[i];
        }
    }

    for ( size_t i = 0; i < n1; i++ ) std::cout << pos[i] << ' ';
    std::cout << std::endl;

    for ( size_t i = 0; i < n2; i++ ) std::cout << neg[i] << ' ';
    std::cout << std::endl;

    return 0;
}

程序输出与上述相同。

当然,您可以更改程序,使用户自己输入原始数组的值。

您正在自己初始化未初始化的元素。 这是您的问题,请使用像0这样的参量值来初始化它们:

pos[k] = 0;

循环查找正负时,应使用循环索引进行比较,然后将其存储在第二个索引中,如下所示:

if(num[i] > -1){
    pos[k] = num[i];
    k++;
    c = c + 1;
}

它将ith元素与-1比较,然后将其存储在pos数组中的kth索引处,并且

if(num[i] < 0){
    neg[j] = num[i];
    j++;
    c2 = c2 + 1;
}

您还需要删除else子句,否则您可能会在最大索引cc2之后写入,并且还因为pos[k] = pos[k]错误。

如果这是c ++,请不要使用c样式的数组。
vector的解决方案可能看起来像这样:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    vector<int> num, pos, neg;

    cout << "enter count :";
    cin >> n;

    for(int i = 0; i < n; ++i)
    {
        cout << "enter number :" ;
        int val;
        cin >> val;
        num.push_back(val);
    }

    for( auto val : num )
    {
        if( val >= 0 )
            pos.push_back(val);
        else
            neg.push_back(val);
    }

    cout << "the positive numbers are: " << endl;
    for( auto val : pos )
        cout << " " << val;

    cout << "\nthe negative numbers are: " << endl;
    for( auto val : neg )
        cout << " " << val;
}

关于vector如何工作的一些注意事项

// this creates a empty vector of int's, named v;
vector<int> v;

// this adds one element (in this case 3) last to the vector 
v.push_back(3);

// this gets the current number of elements
int n = v.size();

// this retrieves the first element of the vector
int a = v[0]; // zero based index

// this is a range based for loop, that prints every element
for( auto elem : v )
    cout << elem << " " ;

// that could also be done with a regular for loop
for( int i=0; i<v.size(); ++i )
    cout << v[i] << " ";

// this makes the vector empty
v.clear();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM