Answer to Question #348554 in C++ for Hasnat

Question #348554

Create an array of size ten; get input from user as odd index has odd value [place a check that when user enters odd value then assign that value to the array element] and even indexes can have any value, show array element by element.

1
Expert's answer
2022-06-10T08:30:28-0400
#include <iostream>
#include <limits>

int check();

int main() {
    int array[10] = {};
    for (int i = 0; i < 10; i++) {
        if (i % 2) {
            array[i] = check();
        }
        else {
            std::cin >> array[i];
        }
    }
    for (auto const& x : array) {
        std::cout << x << std::endl;
    }
    return 0;
}



int check() {
    int s;
    std::cin >> s;
    while (~s % 2) {
        std::cout << "Enter odd value please!" << std::endl;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cin >> s;
    }
    return s;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS