Answer to Question #280941 in C++ for Sardor

Question #280941

Use the start method to find an item in the list where some of the adjacent elements are less than 72. If there are many such elements, then find the largest of them; if such an element does not exist - output the corresponding information.

1
Expert's answer
2021-12-17T18:17:59-0500
#include <iostream>
#include <cstdlib>


using namespace std;


const int SPECIAL = 72;


void start(int* arr, int len) {
    int counter = 0;
    int max;


    for (int i = 0; i < len; i++) {
        if (
        (i == 0 && arr[i + 1] < SPECIAL) ||
        (i == len - 1 && arr[i - 1] < SPECIAL) ||
        (arr[i - 1] < SPECIAL || arr[i + 1] < SPECIAL)
        ) {
            if (counter == 0) max = arr[i];
            else if (arr[i] > max) max = arr[i];


            counter++;
        }
    }


    if (counter == 0) cout << "Element does not exist!" << endl;
    else cout << "Element - " << max << endl;
}


int main() {
    srand(time(NULL));


    int randomLength = rand() % 11 + 10; // 10-20
    int* arr = new int[randomLength];


    cout << "List:" << endl;
    for (int i = 0; i < randomLength; i++) {
        arr[i] = rand() % 100; // 0-99
        cout << arr[i] << " ";
    }
    cout << endl;


    start(arr, randomLength);


    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