C++

Can someone help me with that question?
Write a C++ program that will read the content of the attached file titled “temperatures.txt” knowing only
the following format of the file: first column contains the Celsius temperature, and the second column
contains the equivalent Fahrenheit temperature.
Let your program display the following:
1. Maximum Celsius temperature and its equivalent Fahrenheit temperature.
2. Minimum Celsius temperature and its equivalent Fahrenheit temperature.
3. The average of the Fahrenheit temperatures given in the file.
Note: Assume that you DON’T know how many rows are saved in the file.

The .txt file contains:
5 41
10 50
100 212
20 68
25 77
30 86
35 95
45 113
50 122
60 140
65 149
70 158
75 167
15 59
80 176
85 185
0 32
90 194
95 203
40 104
55 131

The code that I wrote was
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string temp;
int max{}, min{}, avg, i, sum = 0;
ifstream in;
in.open("temp.txt");

for (i = 0;i < 20;i++)
{

if (max < temp[i])
{
max = temp[i];
temp[i] = max;
}
if (min > temp[i])
{
min = temp[i];
temp[i] = min;
}

sum = sum + temp[i];
avg = sum / 20;
}
in.close();

cout << "The maximum temprature is: " << max;
cout << "The minimum temprature is : " << min;
cout << "The average of the farenhiet temprature is: " << avg;

return 0;




}

Last edited on
First. please use code tags and indentation. See http://www.cplusplus.com/articles/jEywvCM9/
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std; // you might bring std::max and std::min to global namespace
int main()
{
  string temp;
  int max{}, min{}, avg, i, sum = 0;
  ifstream in;
  in.open("temp.txt"); // wrong filename

  for (i = 0;i < 20;i++) // Why 20? "Assume that you DON’T know how"
  {
    if ( max < temp[i] ) // temp is an empty string. temp[i] is a char
    {
      max = temp[i];
      temp[i] = max; // Why?
    }

    sum = sum + temp[i];
    avg = sum / 20; // Why not after the loop?
  }
  in.close();

  cout << "The maximum temprature is: " << max;
  cout << "The average of the farenhiet temprature is: " << avg;
  return 0;
}

You never read anything from the file.
Hello rahaf201,

Sorry I thought I sent this earlier, but something went wrong and it did not send.

Along wit what keskiverto has said.

This is a different approach to your code:
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string temp;
	int max{}, min{}, avg, i, sum = 0;

	ifstream in;

	in.open("temp.txt");
	
	// <--- How do you know the file is open?

	if (!in)
	{
		std::cout << "\n    File " << std::quoted("temp.txt") << " Did not open!\n";

		return 1;  // <--- No point in continuing until you fis the problem.
	}

	for (i = 0; i < 20; i++)
	{

		if (max < temp[i])
		{
			max = temp[i];
			temp[i] = max;
		}
		if (min > temp[i])
		{
			min = temp[i];
			temp[i] = min;
		}

		sum = sum + temp[i];

		avg = sum / 20;
	}

	in.close();

	cout << "The maximum temprature is: " << max;
	cout << "The minimum temprature is : " << min;
	cout << "The average of the farenhiet temprature is: " << avg;

	return 0;
}

Notice a few blank lines makes the code easier to read and that is the first goal.

Rearranging the header files is more a personal preference, but in the end the order makes no difference.

Lines 19 - 24 check if the file stream is open and ready to use. This is a must when dealing with an input stream. Not so much on an output stream as the output will create the file if it does not exist.

I was wondering int max{}, min{}, avg, i, sum = 0; why the difference when you define "sum"? What you did with "min" and "max" is good and can be used for "sum" Also "avg" and "i" should be initialized. Even if they do not need to be initialized it is still a good idea just to know that they do not contain a garbage value.

for (i = 0;i < 20;i++) // Why 20? "Assume that you DON’T know how" . DO NOT ASSUME anything. You were told that the file is of unknown length. If you want to use a for loop then do this:
for (i = 0; in >> celsius >> farenheiht; i++). When you read past end of file the condition will fail and so will the loop.

Now that I had had a chance to give your code a test this is what I found.

In the line string temp; replace it with int celsius{}, fahrenheit{};. As a "string" you would have to change the "string" into an "int" before you could work with it. By reading them as "int"s you do not have to worry about the conversion.

The average calculation should be done outside the for loop and you need to divide by "i" not 20. This way you are dividing by the number of temps read and not a magic number the is most likely wrong.

1
2
3
4
5
6
if (max < temp[i])
{
    max = temp[i];

    temp[i] = max;
}

First off "temp[i]" is a single character (char) that you are comparing to an "int". Not what you want.

Not sure what line 5 is for, but if you read the file into an "int" it is not needed.

You have completely missed point 3 of the instructions. The average that you have is on the Celsius temperatures when it needs to be on the Fahrenheit temperatures.

You have the line of code sum = sum + temp[i] it should be sum += fahrenheit;.

Andy
Topic archived. No new replies allowed.