1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Kay [80]
3 years ago
7

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/A

lgorithm to search the heavy orange, in how many steps would you be able to find it out?
Computers and Technology
1 answer:
aalyn [17]3 years ago
7 0

Answer:

#include <iostream>

using namespace std;

void Search_heavy_orange(int arr[], int l, int r, int x)

{

int count = 0, m = 0;

while (l <= r) {

 m = l + (r - l) / 2;

 // Check if x is present at mid

 if (arr[m] == x) {

  count++;

 }

 // If x greater, ignore left half

 if (arr[m] < x) {

  l = m + 1;

  count++;

   

 }

 

 // If x is smaller, ignore right half

 else {

  r = m - 1;

  count++;

 }

}

cout << "............For Worst Case......." << endl;

cout << "Orange with heavy weight is present at index " << m << endl;

cout << "Total number of step performed : " << count << endl;

}

int main()

{

// Assuming each orange is 100 gm and the weight of heavy

// orange is 150 gm

int orange_weight[128];

for (int i = 0; i < 127; i++) {

 orange_weight[i] = 100;

}

// At worst case the heavy orange should be at last position

// inside the basket : 127

orange_weight[127] = 150;

// We will pass array , start index , last index and the search element

// as the parameters in the function Search_heavy_orange

Search_heavy_orange(orange_weight, 0, 127, 150);

return 0;

}

Explanation:

You might be interested in
¿Qué diferencia existe entre un virus biológico y virus informático?
ddd [48]

Answer:

i dont know spanish but i am gussing that you want to find differnce in virus information?

Explanation:

6 0
2 years ago
How does a router handle a packet destined for a network that is not listed in the routing table?
kogti [31]
The packet is dropped or discarded. By default, a router will send packets to a network that has been listed in the routing table. If it happens that the network is not listed, the packet will be discarded or dropped. Packets that do not have a default route or gateway of last resort are dropped.

 






5 0
3 years ago
1.Public policy begins with . 2.Then, government officials . 3.Finally, a government agency .
dexar [7]

Answer:

what was the wuestion

Explanation:

cause we dont know how to help you

4 0
3 years ago
If not cleared out, log files can eventually consume a large amount of data, sometimes filling a drive to its capacity. If the l
OLEGan [10]

Answer:

<u>/var</u>

Explanation:

The /var subdirectory contains files to which the system writes data during the course of its operation. Hence, since it serves as a system directory, it would prevent log files from consuming a large amount of data.

7 0
2 years ago
Read 2 more answers
The intention of this problem is to analyze a user input word, and display the letter that it starts with (book → B).
yulyashka [42]

Answer:

Here is the C++ program:

#include<iostream>  // to use input output functions

using namespace std;     //to identify objects like cin cout

void StartChar(string str)  {  // function that takes a word string as parameter and returns the first letter of that word in capital

   int i;  

   char c;

   do  //start of do while loop

   {

   for (int i = 0; i < str.length(); i++) {  //iterates through each character of the word str

       if(str.length()>25){  //checks if the length of input word is greater than 25

           cout<<"limit exceeded"; }  //displays this message if a word is more than 25 characters long

      c = str.at(i);   // returns the character at position i

       if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ) {  //checks if the input word is contains characters other than the alphabet

            cout<<str<<" is not a word!"<<endl; break;}  //displays this message if user enters characters other than the alphabet

        if (i == 0) {  //check the first character of the input word

           str[i]=toupper(str[i]);  //converts the first character of the input word to uppercase

           cout<<str<<" starts with letter "<<str[i]<<endl;  }  // prints the letter that the word starts with in capital letter

   }   cout<<"Enter a word: ";  //prompts user to enter a word

      cin>>str;   //reads input word from user

}while(str!="#");   }    //keeps prompting user to enter a word until the user enters #

int main()   //start of the main() function body

{ string str;  //declares a variable to hold a word

cout<<"Enter a word: "; //prompts user to enter a word

cin>>str; //reads input word from user

   StartChar(str); }  //calls function passing the word to it

     

Explanation:

The program prompts the user to enter a word and then calls StartChar(str) method by passing the word to the function.

The function StartChar() takes that word string as argument.

do while loop starts. The for loop inside do while loop iterates through each character of the word string.

First if condition checks if the length of the input word is greater than 25 using length() method which returns the length of the str. For example user inputs "aaaaaaaaaaaaaaaaaaaaaaaaaaa". Then the message limit exceeded is printed on the screen if this if condition evaluates to true.

second if condition checks if the user enters anything except the alphabet character to form a  word. For example user $500. If this condition evaluates to true then the message $500 is not a word! is printed on the screen.

Third if condition checks if the first character of input word, convert that character to capital using toupper() method and prints the first letter of the str word in capital. For example if input word is "Computer" then this prints the message: Computer starts with the letter C in the output screen.

The program keeps prompting the user to enter the word until the user enters a hash # to end the program.

The output of the program is attached.

3 0
3 years ago
Other questions:
  • Which of the following accurately completes this sentence? The Internet is ____.
    6·2 answers
  • Complete the recursive function raisetopower(). ex: if userbase is 2 and userexponent is 4, then raisedvalue is assigned with 16
    5·1 answer
  • Type the correct answer in the box. Spell all words correctly.
    9·1 answer
  • F a domain consists of dcs that are running verions of windows server earlier than windows server 2008, what replication method
    10·1 answer
  • Implementing information systems has economic, organizational, and behavioral effects on firms. Information technology, especial
    14·1 answer
  • Moving laterally within a network once an initial exploit is used to gain persistent access for the purpose of establishing furt
    10·1 answer
  • Protocols at which layer of the OSI model have the primary function of moving datagrams through an internetwork connected by rou
    7·1 answer
  • What your favorite video game? (Put your user if you wanna play!)
    9·2 answers
  • Use the drop-down menus to complete statements about the two formats for importing contacts.
    11·2 answers
  • ____must cooperate with each other to neutralize the global threat of information censorship. Select 3 options.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!