Question

In: Computer Science

Write a C++ program to read in a list of 10 integers from the keyboard. Place...

Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as your driver function. Write main( ) so that it allows the user to run the program as many times as desired, as indicated by input from the user. b. Write appropriate functions that main( ) calls to accomplish the tasks of reading the numbers from the user and assigning the integer values to the appropriate array(s), and displaying the output as below

Solutions

Expert Solution


#include<iostream>
using namespace std;

int main() {
        
        int numElements;
        
        // reading number of elements
        cout << "Enter number of elements : " ;
        cin  >> numElements;
        
        int even[numElements];
        int odd[numElements];
        int negetive[numElements];
        
        int evenCount = 0;
        int oddCount = 0;
        int negetiveCount = 0;
        
        // reading elements
        for(int i=0 ; i<numElements ; i++) {
                int temp;
                cout << "Enter element " << i+1 << " : ";
                cin >> temp;
                
                // checking if element is even or odd or negetive number
                // and adding element to array
                if(temp%2 == 0){
                        even[evenCount++] = temp;
                }else{
                        odd[oddCount++] = temp;
                }
                if(temp < 0){
                        negetive[negetiveCount++] = temp;
                }
        }
        
        // printing even number array
        cout << "\n";
        cout << "Even Elements" << endl;
        for(int i=0 ; i<evenCount ; i++) {
                cout << even[i] << " ";
        }
        
        // printing odd number array
        cout << "\n\n";
        cout << "Odd Elements" << endl;
        for(int i=0 ; i<oddCount ; i++) {
                cout << odd[i] << " ";
        }
        
        // printing negetive number array
        cout << "\n\n";
        cout << "Negetive Elements" << endl;
        for(int i=0 ; i<negetiveCount ; i++) {
                cout << negetive[i] << " ";
        }
        
        return 1;
}

Code

Output


Related Solutions

(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
Write a program that uses a loop to read 10 integers from the user. Each integer...
Write a program that uses a loop to read 10 integers from the user. Each integer will be in the range from -100 to 100. After all 10 integers have been read, output the largest and smallest values that were entered, each on its own line in that order. Avoiding using the max or min functions from Python, and definitely use a loop to read the integers instead of 10 input statements.
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
C++. Write a program that will read in id numbers and place them in an array.The...
C++. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please enter an...
C++ Write a program that reads in a list of 10 names as input from a...
C++ Write a program that reads in a list of 10 names as input from a user and places them in an array. The program will prompt for a name and return the number of times that name was entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in. For this lab, use the string data type as opposed to char to...
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT