简体   繁体   English

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

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

i've had a hard time thinking how to fix this out i need to split the negative values and the positive values but when i launched the program the results was wrong im just so new to programming especially in arrays so any help will appreciate. 我一直很难思考如何解决此问题,我需要将负值和正值分开,但是当我启动该程序时,结果是错误的,对编程而言是如此之新,尤其是在数组中,所以任何帮助将不胜感激。

here is the exercise problem: a)define an array with a maxinum of 20 integers values and fill the array with numbers of your own choosing as intializers then write, compile, and run a c++ program that reads the numbers in the array and places all zero and positve numbers in an array named positive and all negative numbers in an array named negative finally, have your program display the values in both the positive and negative arrays 这是练习的问题:a)定义一个最大为20个整数值的数组,并用您自己选择的数字填充该数组作为初始化器,然后编写,编译并运行一个c ++程序,该程序读取数组中的数字并将所有最后,在名为正数的数组中包含零和正数,最后在名为负数的数组中包含所有负数,让程序在正负数组中同时显示值

here is the code: 这是代码:

#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;

} }

the output was just like this: 输出是这样的:

enter a number : 1, -5 , 4, -55, 5 输入数字:1,-5,4,-55,5

the positive numbers is: 1 8 4 正数是:1 8 4

the negative numbers is: 4286352 -5 负数是:4286352 -5

First of all there is already standard function std::partition_copy declared in header <algorithm> that is able to do the job. 首先,已经在标头<algorithm>中声明了标准功能std::partition_copy ,该功能可以完成此工作。 Here is an example 这是一个例子

#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;
}

The program output is 程序输出为

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

If you want to write the program yourself without using standard algorithms then the approach can look like 如果您想在不使用标准算法的情况下自己编写程序,则该方法可能看起来像

#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;
}

The program output is the same as above. 程序输出与上述相同。

Of course you may change the program such a way that the user will enter values of the original array himself. 当然,您可以更改程序,使用户自己输入原始数组的值。

You're initailising uninitialised elements with themselves. 您正在自己初始化未初始化的元素。 This is your problem, initialise them with a sentinal value like 0 : 这是您的问题,请使用像0这样的参量值来初始化它们:

pos[k] = 0;

When looping to find positive and negative, you should use the loop index for comparison and then store in the second index like so: 循环查找正负时,应使用循环索引进行比较,然后将其存储在第二个索引中,如下所示:

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

which compares the ith element to -1 and then store it at kth index in pos array and 它将ith元素与-1比较,然后将其存储在pos数组中的kth索引处,并且

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

You also need to remove the else clause otherwise you might write after max index c and c2 and also because pos[k] = pos[k] is wrong. 您还需要删除else子句,否则您可能会在最大索引cc2之后写入,并且还因为pos[k] = pos[k]错误。

if this is c++, please don't use c-style arrays. 如果这是c ++,请不要使用c样式的数组。
a solution with vector might look something like this: 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;
}

a few notes on how vector works 关于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