Answer to Question #257210 in C++ for george

Question #257210

Write a program to create a dynamic array of user defined size. Size should be in the range of 0 to 15. Write a function FindLarge that should ask user to enter a non-negative number. Function should find the next largest number than the input number in the list.


1
Expert's answer
2021-10-28T01:23:58-0400
#include <iostream>
using namespace std;
int FindLarge(int array[], int size){
    int x; 
    do{
        cout<<"Enter a non-negative number: ";
        cin>>x;
    }while(x < 0);
    int largest = x;
    for(int i = 0; i < size; i++){
        if(array[i] > x){
            largest = array[i];
            return largest;
        }
    }
    return largest + 1;
}
int main(){
    int size;
    do{
    cout<<"Enter size of array (between 0 and 15): ";
    cin>>size;
    }while(size > 15 || size < 0);
    int *array;
    array = new int[size];
    cout<<"Input items in the array: \n";
    for(int i = 0; i < size; i++){
        cin>>array[i];
    }
    int large = FindLarge(array, size);
    cout<<"Next largest number than the input number is: "<<large<<endl;
    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