Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I write a function that takes a dynamic array from the main and creates 2 arrays, 1 for positive values and other for negative and return the two arrays to the main?

this what i've tried:

What I have tried:

C++
#include <iostream>
using namespace std;
void PosNegArr(int* arr, int s, int* p, int* n, int cp, int cn) {

    for (int i = 0; i < s; i++) {
        if (arr[i] < 0) {
            arr[i] = n[i];
        }
        else {
            arr[i] = p[i];
        }
    }
}

int main() {
    int s, c1, c2;

    cout << "Enter number of elements:";
    cin >> s;
    int* A = new int[s];
    cout << "Enter the elements:";
    for (int i = 0; i < s; i++) {
        cin >> A[i];
    }
    for (int i = 0; i < s; i++) {
        if (A[i] < 0) {
            c2++;
        }
        else {
            c1++;
        }
    }
   int* p = new int[c1];
    int* n = new int[c2];
   
    PosNegArr(A, s, p, n, c1, c2);
    cout << "positive elements are :" << endl;
    for (int i = 0; i < c1; i++) {
        cout << p[i] << ' ' << endl;
    }
    cout << "Negative elements are:" << endl;
    for (int i = 0; i < c2; i++) {
        cout << n[i] << ' ' << endl;
    }
}
Posted
Updated 12-Mar-20 9:22am
v2

The instructions say that the function should create the new arrays and resturn them to the caller. So you need something like:

C++
void PosNegArr(int* source, int size, int** newpos, int** newneg)
{
    int* positives = new int[size];
    int* negatives = new int[size];
    memset(positives, 0, size);
    memset(negatives, 0, size);

    for (int i = 0; i < size; i++)
    {
        if (source[i] < 0)
        {
            negatives[i] = source[i];
        }
        else
        {
            positives[i] = source[i];
        }
    }
    *newpos = positives;
    *newneg = negatives;
}


You can then call it like:
C++
int* positiveList;
int* negativeList;

// collect input values ...

PosNegArr(array, countize, &positiveList, &negativeList)

// on return the two arrays will be populated with the numbers
 
Share this answer
 
Comments
Member 14771149 12-Mar-20 15:31pm    
I don't understand how you gave the 2 new arrays the same size of the main array.
also on printing the 2 arrays in the main we use a for loop, what is the size of each of them.
and thank you so much for your time.
Richard MacCutchan 13-Mar-20 5:11am    
The new arrays need to be large enough to hold all the existing numbers, because they could all be positive or all negative. After creating them they are set to zero so only non-zero numbers will be added (although that check needs to be added on the input loop). To print the array you just need to use the original item count and loop until that count is reached, or a zero value is found.
Richard already gave you the solution. Anyway a bit of modern C++ could be worth to see.
C++
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>

using namespace std;

tuple < vector <int>, vector <int> > pos_neg(const vector<int> & v );

int main()
{
  size_t count;
  cout << "Enter number of elements: ";
  cin >> count;
  vector <int > v(count);
  for (size_t n=0; n<count; ++n)
  {
    cin >> v[n];
  }

  auto  [vp, vn] = pos_neg(v);

  cout << "non-negative: ";
  for (const auto & x : vp)
    cout << x << " ";
  cout << "\n";

  cout << "negative: ";
  for (const auto & x : vn)
    cout << x << " ";
  cout << "\n";
}


tuple < vector <int>, vector <int> >  pos_neg(const vector<int> & v )
{
  vector <int> vpos, vneg;
  for_each( v.begin(), v.end(), [&vpos, &vneg](int i){ i < 0 ? vneg.push_back(i) : vpos.push_back(i); } );
  return make_tuple(vpos, vneg);
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 13-Mar-20 5:13am    
Cool!
Trial and error is a learning method where you learn from your errors.
The debugger is a tool that allow you to see step by step what your code is doing.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
[Update]
Quote:
How do I write a function that takes a dynamic array from the main and creates 2 arrays, 1 for positive values and other for negative and return the two arrays to the main?

Just a little writing should have gave you clues to solution:
a[0]=1  => p[0]=1
a[1]=0  => p[1]=0
a[2]=-1 => n[0]=-1
a[3]=3  => p[2]=3
a[4]=-3 => n[1]=-3
a[5]=1  => p[3]=1

it look obvious that you need 3 indexes for a, p and n.
 
Share this answer
 
v2
Given that he/she does provide enough information to solve the problem I'll leave it there.

Stop and think about the problem. The arguments to "PosNetarr" are not arbitrary, they are met to be used. What are you doing with the argument/parameters? Nothing as far as I see. You are suppose to be copying them to 'p' and 'n', not to the original 'Arr'. The limits on 'p' is 'cp' (count p length) and 'cn' (count n length). You are supposed to be copying elements from 'Arr' to 'p' and 'n', not the other way around.

I was tempted to provide a solution, but I will leave it up to you.

Hint:
C++
if (arr[i] < 0 && n_index < cn)
{
    n[n_index++] = arr[i];
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900