Answer to Question #148620 in C++ for Rana Awais

Question #148620
If you have 128 oranges all the same size, color, and weight except one orange is heavier than the rest. Write down a C++ Code/Algorithm to search the heavy orange, in how many steps would you be able to find it out?
1
Expert's answer
2020-12-03T14:37:07-0500
#include <iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));
    // the number of oranges
    int n = 128;
    // filling the array with oranges corresponding with their order the system find randomly the order of heavy orange
    int heavy_orange_order;
    heavy_orange_order = rand() % 120;
    // the cycle of finding
    int abstract_orange_order;
    int steps_finding = 0;
    while (true) {
        cout << "Predict the heavy orange order: ";
        cin >> abstract_orange_order;
        if (abstract_orange_order != heavy_orange_order) {
            steps_finding++;
        }
        if (abstract_orange_order == heavy_orange_order) {
            cout << "Well!!! you find heavy orange." << endl;
            cout << "The number of steps is that " << steps_finding + 1;
            break;
        } 
    }
    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