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.
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 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...
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. 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...
Write an interactive C program that asks a user to enter a sentence from the keyboard,...
Write an interactive C program that asks a user to enter a sentence from the keyboard, and prints the sentence with all the words spelled backwards. Note: you may assume that each sentence will have at most 50 characters and each word is separated by a space. Sample code execution: bold information entered by user enter a sentence: hello ece 175 class olleh ece 571 ssalc continue (q to quit)? y enter a sentence: May the force be with you...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT