Answer to Question #187281 in C++ for Vijay Raj

Question #187281

Let A is an integer array of size 10 and array A can only accept 10 integers. Check any negative number is found and array out of bound if size of array exceeds. If found throw an exception.

Runtime Input :

10

25

-15

30

22

Output :

Negative Number Exception Caught


1
Expert's answer
2021-04-29T16:19:45-0400
#include <iostream>
using namespace std;
int main(){
    int input[10], n;
    try{
        cout<<"How many elements do you want to input? ";
        cin>>n;
        string s = "Array out of bounds Exception Caught";
        if(n > 10) throw(s);
        else{
            cout<<"Input elements\n";
            for(int i = 0; i < n; i++){
                cin>>input[i];
            }
            s = "Negative Number Exception Caught";
            for(int i = 0; i < n; i++){
                if(input[i] < 0) throw(s);
            }
        }
    }
    catch(string err){
        cerr<<err;
    }
    return 0;
}

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