Your friend is working on a Lab experiment to decide which insect is the fastest. You will help your friend and write an algorithm and a C++ program that allows your friend to enter the names of the insects and the measured speed of each insect. The names of the insects and the measured speeds should be stored in two arrays. The program should then sort and print the insects in descending order based on their speed. Your program should also output the name of the fastest insect.

Respuesta :

Answer:

#include <iostream>

#include <string>

using namespace std;

int main()

{

    int size=100;

   float insect_speed [size];

   string insect_name [100]={""};

int max=0;

int max_index=0;

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

   

   cout<<"insect speed"<<endl;

cin>>insect_speed[i];

cin.ignore();

   cout<<"insect name"<<endl;

   getline(cin,insect_name[i]);

if(insect_speed[i]>max){

   max=insect_speed[i];

   max_index=i;

}

   cout<<"Fastest insect is "<<insect_name[max_index]<<" with speed "<<insect_speed[max_index]<<endl;

}

   return 0;

}

Explanation:

Initially took max size of 100. Using string define an string array of size for insect name and float array of size for speeds.

Define a variable max and set to 0 and a max_index and set to 0. max index will store the index of maximum value in array of speed.

For every input of speed check if it greater than max set max to current input vale of speed and max_index to current index of array.

print speed and insect name at max_index for fastest insect.

System will calculate fastest insect at run time an update user after every input.

ACCESS MORE