average time function and arrays

I am on the menu option 2, to calculate the average time,and need to know the best route to go about doing that.

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
 #include <iostream>
using namespace std;

#include <iomanip>

void menu();
void displaymax(double);    //display max protoype

int main() {

	//*****************************************************************************
	//									Array Times
	//*****************************************************************************
	string names[] = { "Leela ", "Sarah ", "Anna ", "Keesha ", "Heidi " };                        //
	double times[] = { 2.03,     2.40,   1.85,     1.90,     2.50 };

	for (int count = 0; count < 5; count++)                                                   //
	{
		cout << names[count] << fixed << setprecision(2) << times[count] << endl;              //
	}



	menu();											//menu function call

	int choice;
	cin >> choice;

	if (choice == 1) {
		size_t idx = 0;
		for (int i = 1; i < 5; i++) {
			if (times[i] < times[idx]) {
				idx = i;
			}
		}
		cout << "Fastest time is " << times[idx] << " by " << names[idx] << '\n';
	}


	return 0;
}


//***************************************************************************
//									 MENU FUNCTION    
//***************************************************************************
void menu()
{
	cout << "Enter 1 to determine 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";
}
//**************** 
Hello TheArk,

What do you need to pass to the function to calculate the average? Then add and divide. Set up your output as needed.

Andy

P.S. Best to stay in one topic.
Last edited on
Here is what I have so far, It will compile in an online compiler, however will not compile in visualstudio, any idea why that may be happening?
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

#include <iomanip>

void menu();
void displaymax(double);    //display max protoype

int main() {

	//*****************************************************************************
	//									Array Times
	//*****************************************************************************
	string names[] = { "Leela ", "Sarah ", "Anna ", "Keesha ", "Heidi " };                        //
	double times[] = { 2.03,     2.40,   1.85,     1.90,     2.50 };

	for (int count = 0; count < 5; count++)                                                   //
	{
		cout << names[count] << fixed << setprecision(2) << times[count] << endl;              //
	}



	menu();											//menu function call

	int choice;										//choice variable initialized
	cin >> choice;									// input choice

	
			//CHOICE 1 If
	if (choice == 1)			
	{								
		size_t idx = 0;
		for (int i = 1; i < 5; i++) 
		{									//for loop choice 1
			if (times[i] < times[idx]) 
			{									//finding max
				idx = i;
			}
		}
		cout << "Fastest time is " << times[idx] << " by " << names[idx] << '\n';		//output fastest time
	}

			//CHOICE 2 if
	 else if (choice == 2)
	{
		double sum = 0;							//initialize accumulator to 0
		
		for (int i = 0; i < 5; i++)				//find sum of numbers
			sum += times[i];
		cout << "The average time is " << sum/ 5 << endl;		//output average
		
}
	
		//CHOICE 3 if						??FIND THE TIME OF A SWIMMER??
	 else  if(choice == 3)								//??
	{

	}

		//Choice 4 exit
	else if(choice == 0)
	{
		exit;									//??Check exit code??
														//??
	}

	return 0;
}


//***************************************************************************
//									 MENU FUNCTION    
//***************************************************************************
void menu()
{
	cout << "Enter 1 to determine 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 << "0 to exit";
}
//**************** 
will not compile in visualstudio

If it doesn't compile, then VS will be reporting error messages to tell you exactly why it's not compiling, including line numbers to show you which lines the errors are on.
Last edited on
Hello TheArk,

Lines 6 and 7 are proto types for two functions, but you only define and use "menu". You are saying that you want a function "displaymax", but you never define this function.

If you have learned about "switch" it would work better than the if/else if statements. See http://www.cplusplus.com/forum/beginner/219248/#msg1010512 Sorry I just notice I left the proto type in my example. Fixed that.

The code you have from line 47 to line 51 is correct, but would work better in a function. Although there is nothing wrong with what you have done.

Choice 3 will have similar code that you used in choice 1 to find your match and then use "idx" in the "cout" to print the result.

Hope that helps,

Andy
Hello TheArk,

will not compile in visualstudio

You did not include the header file "string".

Andy
Hello TheArk,

just before the return 0;" in main put these two lines:

1
2
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // Include "limits" header file.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


The first line will clear the input buffer of the "\n". And the second will pause the screen waiting for enter to continue. Otherwise the program ends before you can read the screen output.

Hope that helps,

Andy
Topic archived. No new replies allowed.