简体   繁体   中英

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

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

the positive numbers is: 1 8 4

the negative numbers is: 4286352 -5

First of all there is already standard function std::partition_copy declared in header <algorithm> that is able to do the job. 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 :

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

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.

if this is c++, please don't use c-style arrays.
a solution with vector might look something like this:

#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

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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