Question

29. (20 points) 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. Dont forget the pre-processor commands Define an array called nums with a maximum of 20 integers, and fill the array with numbers, recei the keyboard (prompt for the numbers.) In this program, exactly 20 integers will be input (no need vectors). Then write a function named split0 that accepts the arra positive and negative. This function places all zeros and positiv numbers into negative. Finally, back in the main, have your program only positive and negative. Do not use vectors in any of vour 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. y, along with two other arrays called e numbers into positive and all negative display the values in the array The positive numbers are: 0 4 6 2 100 The negative numbers are: 1 -5 -99-100 -2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

#include <cstdlib>
#include<iostream>

using namespace std;
//Function prototype for split
void split(int[],int[],int[]);

//global variable declaration
int ct1=0;
int ct2 =0;

//driver program
int main(int argc, char** argv) {
//declare an array variable to hold the 20 array values
int nums[20];
//array of positive and negative numbers
int positive[20],negative[20];
//Prompt the user to accept 20 values
cout<<"\nEnter 20 values :";
for(int i = 0; i<20; i++){
cin>>nums[i];
}
//calls the function to split the numbers as positive and negative
split(nums,positive,negative);
  
//displays the positive numbers
cout<<"\nPositive numbers :";
for(int i = 0; i<ct1; i++){
cout<<positive[i]<<"\t";
}
cout<<"\n NEgative Numbers :";
//displays the negative numbers
for(int i = 0; i<ct2; i++){
cout<<negative[i]<<"\t";
}
return 0;
}

//function to split positive and negative numbers
void split(int nums[],int positive[],int negative[]){
for(int i = 0; i<20; i++){
if (nums[i]<0 ){
negative[ct2]=nums[i];
ct2++;
}
else{
positive[ct1] = nums[i];
ct1++;
}
}
}

output:


Enter 20 values :2
3
12
100
25
67
0
-9
8
-7
-6
-5
-4
-3
-100
400
300
11
-34
-65

Positive numbers :2 3 12 100 25 67 0 8 400 300 11
NEgative Numbers :-9 -7 -6 -5 -4 -3 -100 -34 -65
RUN SUCCESSFUL (total time: 38s)

Add a comment
Know the answer?
Add Answer to:
29. (20 points) Write the complete C++ program that implements the following program, as if you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • <!DOCTYPE HTML> <html lang="en">    <title>JavaScript Array Lab</title>    <script src="script.js"></script>    <body>        <p>To...

    <!DOCTYPE HTML> <html lang="en">    <title>JavaScript Array Lab</title>    <script src="script.js"></script>    <body>        <p>To test your function, call divideArray() from the JavaScript console in the browser.</p>    </body> </html> Don't edit your code on the above code and do not copy from others', plz! // Put your solution here in script.js Write the function divideArray() in script.js that has a single numbers parameter containing an array of integers. The function should divide numbers into two arrays, evenNums for...

  • programing C,already write part of code (a) Write a function print.array that receives an array of...

    programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • read it carefully C++ Question: Write a program that dynamically allocates an array large enough to...

    read it carefully C++ Question: Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input Validation: Do not...

  • Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers....

    Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers. This program will have array that holds 10 random integers from 1 to 5. Output the 10 random numbers to the screen using an enhanced for loop. This program will also have a method called frequency that takes an integer array as the parameter and it will return an array that holds the frequency of numbers. Index 0 will hold the frequency of 1,...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that...

    C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that creates an array of 10 integers. The program should then use the following functions: getData() Used to ask the user for the numbers and store them into an array displayData() Used to display the data in the array displayLargest() Used to find and display the largest number in the array (prior to sort). displaySmallest() Used to find and display the smallest number in the...

  • Please write a C# program For this part of the lab, you'll be writing methods to...

    Please write a C# program For this part of the lab, you'll be writing methods to work with 1D arrays. We will expect you to know how to create an array, and store and retrieve information from them. Continue working in the same project and “Program.cs” file. You will be generating random numbers, but the class Random is part of the standard library (don’t need any additional using statements). You should consider developing the methods for this project incrementally. In...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT