Answer to Question #348558 in C++ for Hasnat

Question #348558

Create an array of size 30. Take 2 indexes from user as input and multiply the value on those indexes and display the answer on screen.

1
Expert's answer
2022-06-13T08:29:09-0400
#include <iostream>

using namespace std;

void getIndex(const char error_str[], int& x);

int main(void) {
    int array[30] = { 51,78,3,6,42,60,9,8,57,64,76,7,98,75,8,97,0,9,84,27,29,53,98,37,6,4,9,78,65,46 };
    int a, b;

    cout << "Enter first index: ";
    getIndex("Wrong index!\nRe-enter first index: ", a);
    cout << "Enter second index: ";
    getIndex("Wrong index!\nRe-enter second index: ", b);

    cout << array[a] * array[b];
    return 0;
}

void getIndex(const char error_str[], int& x) {
    cin >> x;
    while (!std::cin || x < 0 || x >= 30) {
        cout << error_str;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin >> x;
    }
}

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