Write the complete C++ program that implements the following program, as if you
ID: 3914947 • Letter: W
Question
Write the complete C++ program that implements the following program, as if you were turning this in for homework. Your program should have a commented header (your name, etc), comments within the program and a well-built display for the output. Don’t forget the pre-processor commands. Define an array called nums with a maximum of 20 integers, and fill the array with numbers, received from the keyboard (prompt for the numbers.) In this program, exactly 20 integers will be input (no need to use vectors). Then write a function named split() that accepts the array, along with two other arrays called positive and negative. This function places all zeros and positive numbers into positive and all negative numbers into negative. Finally, back in the main, have your program only display the values in the array positive and negative. Do not use vectors in any of your code. The output should look like the following example. You will need to account for in your code that the arrays positive and negative will not necessarily contain 20 numbers. The positive numbers are: 0 4 6 2 100 The negative numbers are: -1 -5 -99 -100 -2
How do you do this using arrays only?
Explanation / Answer
If the function could receive 2 reference parameters like sizePos and sizeNeg, it could return both the sizes
//sizePos and sizeNeg are values updated by function
void split(int numbers[], int positive[], int negative[], int &sizePos, int &sizeNeg){
sizePos = 0;
sizeNeg = 0;
for(int i = 0; i < 20; i++)
{
if(numbers[i] < 0)
negative[sizeNeg++] = numbers[i];
else
positive[sizePos++] = numbers[i];
}
}
But since the question says only 3 parameters.... Giving you the following code which uses -1 as a sentinel value in positive array and 0 as sentinel in negative array. Let me know if you have any doubts, I'll help.
Please do rate the answer if it helped. thank you
#include <iostream>
#include <string>
using namespace std;
void split(int numbers[], int positive[], int negative[]);
int main(){
int numbers[20];
int positive[20], negative[20];
for(int i = 0; i < 20; i++){
cout << "Enter number " << (i+1) << ": ";
cin >> numbers[i];
}
split(numbers, positive, negative);
//print +ve numbers
cout << "The positive numbers are:";
for(int i = 0; i < 20 && positive[i] >= 0; i++)
cout << " " << positive[i];
//print -ve numbers
cout << endl << endl << "The negative numbers are:";
for(int i = 0; i < 20 && negative[i] < 0; i++)
cout << " " << negative[i];
cout << endl;
}
//function to separate positive and negative numbers into 2 different arrays
//the positive array will hold a sentinel -1 to indicate end of all +ve values
//the negative array will hold a sentinel 0 to indicate end of all -ve values
void split(int numbers[], int positive[], int negative[]){
int pIndex = 0, nIndex = 0;
for(int i = 0; i < 20; i++)
{
if(numbers[i] < 0)
negative[nIndex++] = numbers[i];
else
positive[pIndex++] = numbers[i];
}
if(pIndex < 20)
positive[pIndex] = -1; //sentinel value to indicate no more +ve numbers in the array
if(nIndex < 20)
negative[nIndex] = 0; //sentinel value to indicate no more -ve numbers in the array
}
output
-----
Enter number 1: 5
Enter number 2: 9
Enter number 3: -4
Enter number 4: 8
Enter number 5: 0
Enter number 6: 200
Enter number 7: -3
Enter number 8: -1
Enter number 9: 9
Enter number 10: 33
Enter number 11: 55
Enter number 12: 24
Enter number 13: -55
Enter number 14: -2
Enter number 15: 1
Enter number 16: 0
Enter number 17: -3
Enter number 18: -10
Enter number 19: 25
Enter number 20: 67
The positive numbers are: 5 9 8 0 200 9 33 55 24 1 0 25 67
The negative numbers are: -4 -3 -1 -55 -2 -3 -10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.