In: Computer Science
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
#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