Algorithm and C++ program that separates odd and even integers into separate arrays using the concept of object-oriented programming (OOP):
Algorithm:
- Create a class called "Separator"
 - Define two private arrays, one for odd integers and one for even integers
 - Define a public function called "separate()" that takes in an array of integers as an input
 - Iterate through the input array and use an if statement to check if the current integer is odd or even
 - If the integer is odd, add it to the odd integer array
 - If the integer is even, add it to the even integer array
 - Define a public function called "print_arrays()" that prints out the contents of the odd and even integer arrays
 
C++ Program:
#include <iostream>
 
class Separator {
private:
    int odd_array[100];
    int even_array[100];
    int odd_count = 0;
    int even_count = 0;
 
public:
    void separate(int input_array[], int size) {
        for (int i = 0; i < size; i++) {
            if (input_array[i] % 2 == 0) {
                even_array[even_count] = input_array[i];
                even_count++;
            } else {
                odd_array[odd_count] = input_array[i];
                odd_count++;
            }
        }
    }
 
    void print_arrays() {
        std::cout << "Odd integers: ";
        for (int i = 0; i < odd_count; i++) {
            std::cout << odd_array[i] << " ";
        }
        std::cout << std::endl;
 
        std::cout << "Even integers: ";
        for (int i = 0; i < even_count; i++) {
            std::cout << even_array[i] << " ";
        }
        std::cout << std::endl;
    }
};
 
int main() {
    int input_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int size = sizeof(input_array) / sizeof(input_array[0]);
 
    Separator separator;
    separator.separate(input_array, size);
    separator.print_arrays();
 
    return 0;
}
 
/*
======Input/Output=====
int input_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Odd integers: 1 3 5 7 9 
Even integers: 2 4 6 8 10 
*/#OOP#HappyProgramming#HappyCoding
No comments:
Post a Comment