displaying the max using a function
Jul 23, 2017 at 2:56pm UTC
Hello, I am creating a menu, and for option 1 I am trying to display the fastest skier, I am a biut lost on how to go about this, any suggestions?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
void displaymax(double ); //display max protoype
int main() {
//********************************************************************************
// Array Times
//*****************************************************************************
string names[] = { "Leela" , "Sarah" , "Anna" , "Keesha" , "Heidi" }; //
double time[] = { 2.03, 2.40, 1.85, 1.90, 2.50 };
for (int count = 0; count < 5; count++) //
{
cout << names[count] << fixed << setprecision(2) << time[count] << endl; //
}
//***************************************************************************
// MENU
//***************************************************************************
cout << "Enter 1 to determinet0 the fastest skier. " << endl;
cout << "Enter 2 to calculate the average time" << endl;
cout << "Enter 3 to find the time of a skier" << endl;
cout << "press any other key to exit" ;
//**********************************************************************************
cin >> choice;
void (double max)
{
if (choice = 1)
{
double max = times[0];
for (int i = 1; i < 5; i++)
{
if (times[i] > max)
{
max = times[i];
}
}
}
}
cout << max << endl;
return 0;}
Last edited on Jul 23, 2017 at 3:03pm UTC
Jul 23, 2017 at 3:59pm UTC
You need to declare
choice
.
The trick to displaying the fastest skier is to keep track of the index of the fastest skier, not the time. You can use the index to get the time and the name.
Your comparison at line 42 is wrong. You fastest skier has the smallest time, not the largest.
Lines 35,36 and 48 aren't needed.
Line 49 should go between lines 46 and 47.
Putting that all together, replace lines 33-49 with:
1 2 3 4 5 6 7 8 9 10 11 12
int choice;
cin >> choice;
if (choice == 1) {
size_t idx = 0;
for (int i = 1; i < 5; i++) {
if (time[i] < time[idx]) {
idx = i;
}
}
cout << "Fastest time is " << time[idx] << " by " <<names[idx] << '\n' ;
}
Jul 23, 2017 at 8:21pm UTC
Hello TheArk,
dhayden has a shorter idea than I had, but I think this is closer to what you are trying to do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
void displayMax(std::string names[], double times[]);
int main()
{
std::string names[] = { "Leela" , "Sarah" , "Anna" , "Keesha" , "Heidi" }; //
double time[] = { 2.03, 2.40, 1.85, 1.90, 2.50 };
int choice{ 0 };
for (int count = 0; count < 5; count++) //
{
// <--- Changed.
std::cout << std::left << std::setw(6) << names[count] << " " << std::right << std::fixed << std::setprecision(2) << time[count] << std::endl; //
}
std::cout << "\n\n" ;
//***************************************************************************
// MENU
//***************************************************************************
std::cout << "Enter 1 to determinet0 the fastest skier. " << std::endl;
std::cout << "Enter 2 to calculate the average time" << std::endl;
std::cout << "Enter 3 to find the time of a skier" << std::endl;
std::cout << "press any other key to exit" << std::endl;
//**********************************************************************************
std::cout << " Enter Choice: " ; // <---Added.
std::cin >> choice;
switch (choice)
{
case 1:
displayMax(names, time);
break ;
default :
break ;
}
std::cout << "\n\n\n\n Fin Press any key to continue -- > " ;
std::cin.get(); // <--- Gets the \n from the buffer.
// Waits for enter.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ); // Requires header file limits.
return 0;
}
void displayMax(std::string names[], double times[])
{
int idx{ 0 };
for (i = 1; i < 5; i++)
{
if (times[i] < times[idx])
idx = i;
}
std::cout << names[idx] << " has the fastest time of " << times[idx] << std::endl;
}
Notice the comments I put in the program and the changes I made.
Hope that helps,
Andy
Last edited on Jul 24, 2017 at 11:17am UTC
Topic archived. No new replies allowed.