Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in a new file called split.cpp which reads a list of numbers int

ID: 3531804 • Letter: W

Question

Write a program in a new file called split.cpp which reads a list of numbers into an array, splits the list into a list of negative numbers and a list of non-negative numbers, each in their separate arrays, and then outputs the arrays of negative and non-negative numbers. A negative integer is one that is strictly less than 0. All other integers are non-negative integers. The program should use the command new to allocate arrays of exactly the correct size.


The program should have a function count() for counting the number of negative elements and the number of non-negative elements of an array, a function split() for splitting the list into negative and non-negative lists, and a function print_array() for printing an array of numbers.

Run split_solution.exe to see an example of the program.

1. All functions should be written AFTER the main procedure.

2. A function prototype should be written for each function and placed BEFORE the main procedure.

3. Each function should have a comment explaining what it does.

4. Each function parameter should have a comment explaining the parameter.

5. Prompt for the number of elements of the list.

6. Allocate an array whose size equals the number of elements specified by the user.

7. Read into the array the elements of the list.

8. Write a function count() which counts the number of negative elements and the number of non-negative elements of an array. The function should take four parameters, the array, the number of elements in the array, the number of negative elements and the number of non-negative elements.

The function sets the number of negative and non-negative elements but does not return any value.

9. In the main program, call the function count() to get the number of negative elements and the number of non-negative elements of the array.

10. Allocate an array whose size equals the number of negative elements.

11. Allocate an array whose size equals the number of non-negative elements.

12. Write a function split() which takes as input 3 arrays, A, B, and C, and stores the negative elements of A in B and the non-negative elements of A in C. (Use better array names than A, B, and C). The size of array B should be exactly the number of negative elements in array A. The size of array C should be exactly the number of non-negative elements in A.

As you copy elements from A to B or C, count the number of elements copied to each array. When the function completes, check that the number copied equals exactly the array size. If it does not, print an error message and exit.

The function should take six parameters, array A, the size of array A, array B, the size of array B, array C, and the size of array C.

The function modifies arrays B and C but does not return any value.


13. Write a function print_array() which prints the elements of an array.

The function should take two parameters, the array and the array length.

The function does not modify or return any values.

14. 14. In the main program, call the function split(), passing the input array and the arrays whose sizes equal the number of even and odd elements.

15. In the main program, print the phrase

Explanation / Answer

#include<iostream>


using namespace std;


// prototype :..............


void count(int a[],int size,int& noOfNegativeElements,int &noOfNonNegativeElements);

void split(int A[],int size,int B[],int noOfNegativeElements,int C[],int noOfNonNegativeElements );

void print_array(int a[],int size);


// main function..................


int main()

{

int n;

int *A;

int* B;

int *C;

int noOfNegativeElements=0;

int noOfNonNegativeElements=0;

cout<<"enter the number of element of list ";

do

{

cin>>n;

if(n<0)

cout<<"enter positive value ";

}while(n<0);


A=new int[n];


cout<<"enter the element of the list ";

for(int i=0;i<n;i++)

cin>>A[i];


count(A,n,noOfNegativeElements,noOfNonNegativeElements);

B=new int[noOfNegativeElements];

C=new int[noOfNonNegativeElements];


split(A,n,B,noOfNegativeElements,C,noOfNonNegativeElements);


cout<<" Negative elements: ";

print_array(B,noOfNegativeElements);


cout<<" Non-negative elements: ";

print_array(C,noOfNonNegativeElements);

delete[] A;

delete[] B;

delete[] C;

return 0;

}


// count function.................

// It will counts the number of negative elements and the number of non-negative elements of an array


void count(int a[],int size,int& noOfNegativeElements,int &noOfNonNegativeElements)

{

for(int i=0;i<size;i++)

if(a[i]>=0)

noOfNonNegativeElements++;

else

noOfNegativeElements++;


}

//split function

// it will splits the list into a list of negative numbers and a list of non-negative numbers, each in their separate arrays


void split(int A[],int size,int B[],int noOfNegativeElements,int C[],int noOfNonNegativeElements )

{

int j=0;

int k=0;

int m=0;

for(int i=0;i<size;i++)

{

if(A[i]>=0)

{

C[j]=A[i];

j++;

m++;

}

else

{

B[k]=A[i];

k++;

m++;

}

}

if(!(m==(noOfNegativeElements+noOfNonNegativeElements)))

cout<<"error while copying ";



}


// print array function


void print_array(int a[],int size)

{

for(int i=0;i<size;i++)

cout<<a[i]<<" ";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote