# C++ about function and array homework coding

 Jun Nishikawa · July 5, 2014 Hi I have been coding this homework for a while but Im quite new to it and not quite sure if this is correct or not. below is the question.A) Define an array with a maximum if 20 integer values, and fill the arrays with numbers input from the keyboard. Then write a function name split() that reads the array and places all zeroes or positive number in an array named "positive" and all negative number in an array named "negative" Finally have a program call a function that displays the values in both the positive and negative arrays.B) Extend the program written in part A to sort positive and negative arrays into ascending order before they're displayedMy Coding and Example of result below//  main.cpp//  ArrayPosNeg//  Created by DOLCE on 5/7/26 ????.//  Copyright (c) ???? 26 ????. All rights reserved.#include using namespace std;void split(int theArray[], int sizeOfArray);     //Function Phototype work for spliting Pos & Neg numberint main(){    int numberSet[20];        for (int i = 0 ; i < 20 ; i++)    {                cout << "Enter 20's value of integer (Except zero) : ";        cin >> numberSet;            }        split(numberSet, 20);            return 0;}void split(int theArray[], int sizeOfArray){    int pos[sizeOfArray];    int neg[sizeOfArray];        for (int x= 0 ; x < sizeOfArray ; x++)    {        if (theArray[x] >= 0)        {            pos[x] = theArray[x];            cout << "The group of Positive number is : " << pos[x] << endl;        }            }    cout << endl;        for (int y = 0 ; y < sizeOfArray ; y++)    {        if (theArray[y] < 0)        {            neg[y] = theArray[y];            cout << "The group of Negative number is : " << neg[y] << endl;        }            }}//Sorting Example (Beta)//Enter 20's value of integer (Except zero) : 2//Enter 20's value of integer (Except zero) : 424//Enter 20's value of integer (Except zero) : 52//Enter 20's value of integer (Except zero) : -12//Enter 20's value of integer (Except zero) : -41//Enter 20's value of integer (Except zero) : 0//Enter 20's value of integer (Except zero) : 1250//Enter 20's value of integer (Except zero) : 0//Enter 20's value of integer (Except zero) : 356//Enter 20's value of integer (Except zero) : 124//Enter 20's value of integer (Except zero) : -14//Enter 20's value of integer (Except zero) : 0//Enter 20's value of integer (Except zero) : -35//Enter 20's value of integer (Except zero) : -23//Enter 20's value of integer (Except zero) : -5//Enter 20's value of integer (Except zero) : 2//Enter 20's value of integer (Except zero) : 4//Enter 20's value of integer (Except zero) : 0//Enter 20's value of integer (Except zero) : 6//Enter 20's value of integer (Except zero) : 3//The group of Positive number is : 2//The group of Positive number is : 424//The group of Positive number is : 52//The group of Positive number is : 1250//The group of Positive number is : 356//The group of Positive number is : 124//The group of Positive number is : 2//The group of Positive number is : 4//The group of Positive number is : 6//The group of Positive number is : 3//The group of Negative number is : -12//The group of Negative number is : -41//The group of Negative number is : -14//The group of Negative number is : -35//The group of Negative number is : -23//The group of Negative number is : -5//Zero Value0//Zero Value0//Zero Value0//Zero Value0//program ended with exit code: 0PS. so what I am really confuse what to do is the part "B" and part "A" if anyone knows what I have done wrong here feel free to correct me, this is my first C++ class I have ever taken in my life and I am still new to this web board so nice to meet you guys ! ^^/

## Replies

 Adam Beck · July 6, 2014 A) Define an array with a maximum if 20 integer values, and fill the arrays with numbers input from the keyboard. Then write a function name split() that reads the array and places all zeroes or positive number in an array named "positive" and all negative number in an array named "negative" Finally have a program call a function that displays the values in both the positive and negative arrays.B) Extend the program written in part A to sort positive and negative arrays into ascending order before they're displayedI only know Java and am learning C++, so sorry if my code is wrong.So first define an array:int[] array = new int[20];then do the input.for (int i = 0; i < 20; i++)cin >> array;then write the split function. First keep track of your variables.int positives = 0;int negatives = 0;then loop through your array again, doing positives++ and negatives++ when necessary. Then create your 2 extra arrays.int[] positiveArray = new int[positives];int[] negativeArray = new int[negatives];fill them up somehow. Use the % operator to figure out of each number is positive or negative.Now create a function that outputs them somehow. that shouldn't be too hard. You can either pass in the length variable of your functions or pass in the function in a parameter to loop through it.Now in part b you have to sort the arrays. There is probably a built int array.sort() method in c++, but if there isn't, you have to create a recursive function to sort the array. You have to make the array into an arraylist so you can freely move values in and out of each other. I'm not good at this part.Hope this helps somewhat.
• 1

## C++

104,834 followers