counter-controlled repetition / sentinel-controlled

Hello,this is my first time here and I am in need of some assistance I cannot figure out my two homework questions any help would be greatly appreciated thank you.

2. Write one program that uses counter-controlled repetition to process temperatures. The program has the following display:

(1st Example Display)
Enter the number of temperatures: 2
Enter a temperature: 77
Enter a temperature: 78
The average temperature is 77.5 degrees for the 2 days.
End of Program

(2nd Example Display)
Enter the number of temperatures: 3
Enter a temperature: 80
Enter a temperature: 84
Enter a temperature: 79
The average temperature is 81.0 degrees for the 3days.
End of Program

(3rd Example Display)
Enter the number of temperatures: 0

No temperatures were entered.
End of Program


3. Write one program that uses sentinel-controlled repetition to process the hours an employee works. The program has the following display:

(1st Example Display)
Enter the number of hours or -1 to quit: 9
Enter the number of hours or -1 to quit: 4
Enter the number of hours or -1 to quit: -1

The employee worked an average of 6.5 hours/day for 2 days.
End of Program

(2nd Example Display)
Enter the number of hours or -1 to quit: 6
Enter the number of hours or -1 to quit: 3
Enter the number of hours or -1 to quit: 8
Enter the number of hours or -1 to quit: -1

The employee worked an average of 5.6 hours/day for 3 days.
End of Program

(3rd Example Display)
Enter the number of hours or -1 to quit: -1

No hours were entered
End of Program
where's the problem?
I honestly do not know where to begin. I have never encountered these problems in class
If I know how many times I want a loop to repeat - I'd tend to use a for loop. The counter in the first case is the number of temperatures - this number you'll get from the user. Since the problem wants you to calculate an average, you'll need to keep a running total of temperatures. This only gets done if the number of temps to be entered is greater than 0.

for each temperature the user wants to enter
...input a temperature
...output the temp entered
...increment running total
once all temps have been entered, calculate the average


In the second case, you don't know how many times the loop will repeat, since the repetition is controlled by the user entering a specific value, or sentinel. In this case, I'd use a while or do while loop - while the sentinel has not been entered, repeat the actions in the loop.

Since this problem also wants you to calculate an average, you need to keep a running total of all hours entered, as well as a counter that keeps track of how many times hours are entered, so you know the number of days to divide by for the average hours.
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
This is what I have so far
#include <iostream>
using namespace std;

double countAverageTemperature(int temperatures[], int number)
{
	int total = 0;

	for (int index = 0; index < number; index++)
	{
		total += temperatures[index];
	}
	
	return (double)total / number;
}

int main()
{
	cout << "Enter the number of temperatures: ";
	int number = 0;
	cin >> number;

	if (number == 0)
	{
		cout << endl << "No temperatures were entered." << endl;
		cout << "End of Program" << endl;
	}
	else
	{
		int* temperatures = new int [number];
		for (int index = 0; index < number; index++)
		{
			cout << "Enter a temperature: ";
			cin >> temperatures[index];
		}

		double average_temperature = countAverageTemperature(temperatures, number);
		cout << endl << "The average temperature is " << average_temperature << " degrees for the " << number << " days." << endl;
		cout << "End of Program" << endl;
	}

	return 0;
}
Last edited on
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
This is what I also have for employee's thus far

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int hours = 0, total = 0, number = 0;
	while (hours != -1)
	{
		cout << "Enter the number of hours or -1 to quit: ";
		cin >> hours;
		if (hours != -1)
		{
			total += hours;
			number++;
		}
	}

	if (number == 0)
	{
		cout << endl << "No hours were entered" << endl;
		cout << "End of Program" << endl;
		return 0;
	}

	cout << endl << "The employee worked an average of " << setprecision(3) << (double)total / number << " hours/day for " << number << " days. " << endl;
	cout << "End of Program" << endl;
	
	return 0;
} 
Last edited on
If you edit your post, highlight your code, then click the <> button in the Format palette on the right, that'll format your code on the forum and make it easier to read.

If you're going to dynamically allocate an array with new[], you need to have a corresponding delete[] to avoid a memory leak.
Thank you wildblue I will try that shortly
Topic archived. No new replies allowed.