exercise_07_35.cpp 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <iomanip>
#include <unistd.h>

template <typename T>
void 
printArray(const T array[], const size_t sizeOfArray, const size_t firstIdx = 0) 
{
    std::cout << array[firstIdx];
    if (firstIdx == sizeOfArray - 1) {
        std::cout << std::endl;
        return;
    }
    if (sizeof(T) > 1) {
        std::cout << ", ";
    }
    return printArray(array, sizeOfArray, firstIdx + 1);
}

template <typename T>
void 
numericArrayInitialization(T array[], const size_t sizeOfArray) 
{
    for (size_t index = 0; index < sizeOfArray; ++index) {
        if (::isatty(STDIN_FILENO)) {
            std::cout << "element " << index + 1 << ": ";
        }
        std::cin >> array[index];
    }
}

void 
charsArrayInitialization(char array[], const size_t sizeOfArray) 
{
    if (::isatty(STDIN_FILENO)) {
        std::cout << "word: ";
    }
    std::cin >> std::setw(sizeOfArray) >> array;
}

int
main()
{
    if (::isatty(STDIN_FILENO)) {
        std::cout << "Insert integers array size and elements.\n" << "size: ";
    } 
    size_t integersArraySize;
    std::cin >> integersArraySize;
    int integersArray[integersArraySize];
    numericArrayInitialization(integersArray, integersArraySize);
    printArray(integersArray, integersArraySize);
    if (::isatty(STDIN_FILENO)) {
        std::cout << "Insert floating numbers array size and elements.\n" << "size: ";
    } 
    size_t floatingArraySize;
    std::cin >> floatingArraySize;
    double floatingArray[floatingArraySize];
    numericArrayInitialization(floatingArray, floatingArraySize);
    printArray(floatingArray, floatingArraySize); 
    if (::isatty(STDIN_FILENO)) {
        std::cout << "Insert characters array size and elements.\n" << "size: ";
    } 
    size_t charactersArraySize;
    std::cin >> charactersArraySize;
    char charactersArray[charactersArraySize];
    charsArrayInitialization(charactersArray, charactersArraySize);
    printArray(charactersArray, charactersArraySize);
    return 0;
}