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:
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...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
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....
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character...
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character read in, your program will print a neat message that echoes the input value, and the ASCII character code of the input character in hexadecimal. It will run forever: no HALT or End of processing is required. For example: Please press a key: You pressed 'z' which is x7A Please press a key: You pressed '@' which is x40 Please press a key: You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT