Temperature conversion

I am trying to write a program that will convert an array of temperatures to Celsius and display them with the Fahrenheit values. I have the program put together my problem comes when i try to convert the values to Celsius I was thinking the value would be something like (Fahrenheit -32) * (5/9) but i can never get it to work.

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
  // sample code




#include <iostream>
#include <string>

using namespace std;



int main(int argc, char** argv) {
	// variable declaration section
	const int ARRAY_SIZE = 7;
	string dayName[ARRAY_SIZE] = { "Mon", "Tue", "Wed", "Thur","Fri", "Sat", "Sun" };
	float dayTEMP[ARRAY_SIZE] = {65, 72, 44, 45, 66, 88, 99};
	
	//celsius array
	string dayNamei[ARRAY_SIZE] = { "Mon", "Tue", "Wed", "Thur","Fri", "Sat", "Sun" };
	float dayTEMPi[ARRAY_SIZE] = {65, 72, 44, 45, 66, 88, 99};

	
	
	// temp table
	for (int arrayIndex = 0; arrayIndex < ARRAY_SIZE; arrayIndex++)
	{
		cout << dayName[arrayIndex] << " Fahrenheit: "<< dayTEMP[arrayIndex] << endl;
		cout << dayNamei[arrayIndex] << " Celsisus: " << dayTEMPi[arrayIndex] << endl;
	}


	return 0;
}

float tempInCelsius = (tempInFahrenheit - 32) * (5.0/9.0);
Ok so how would I implement that into my current code. Do I add it as part of my array ?
I would make a helper function that takes Fahrenheit array, and returns new Celsius array.
(even better using std::array or std::vector)
the function should do computation for each array index and store the result into a new array.

edit:
for example:

1
2
3
4
5
6
7
8
void GetCelsius(const float original[], float output[], const int& size)
{

	for (size_t i = 0; i < size; i++)
	{
		output[i] = (original[i] - 32) * (5.0 / 9.0);
	}
}


In your main dayTEMPi[ARRAY_SIZE] should be passed as second paramenter to function to initialize it with celsius values.

for example:
GetCelsius(dayTEMP, dayTEMPi, ARRAY_SIZE);
Last edited on
my current code

First: dayNamei[] and dayName[] have the same content. Do you plan to change that one day in some future or would one array be enough?
Second: Did you read temperature in Farenheit? Then store only Farenheit values. To display °C compute it.

I suggest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// sample code
#include <iostream>
#include <string>
using namespace std;

int main()
{
    // variable declaration section
	const int ARRAY_SIZE = 7;
	string dayName[ARRAY_SIZE] = { "Mon", "Tue", "Wed", "Thur","Fri", "Sat", "Sun" };
	float dayTEMP[ARRAY_SIZE] = {65, 72, 44, 45, 66, 88, 99};
	
	// temp table
	for (int arrayIndex = 0; arrayIndex < ARRAY_SIZE; arrayIndex++)
	{
		cout << dayName[arrayIndex] << " Fahrenheit: "<< dayTEMP[arrayIndex]
		                            << ", Celsisus: " << (dayTEMP[arrayIndex] - 32) * 5 / 9.0 << endl;
	}
//	return 0;
}

Result:

Mon Fahrenheit: 65, Celsisus: 18.3333
Tue Fahrenheit: 72, Celsisus: 22.2222
Wed Fahrenheit: 44, Celsisus: 6.66667
Thur Fahrenheit: 45, Celsisus: 7.22222
Fri Fahrenheit: 66, Celsisus: 18.8889
Sat Fahrenheit: 88, Celsisus: 31.1111
Sun Fahrenheit: 99, Celsisus: 37.2222
Last edited on
thank you guys so much I learned a lot in this thread my problem was how i was trying to add the code to execute things I kept trying to add it in the variable decloration setting I had tried the way above but but with two arrays and it was all messed up.
Ok new problem .. this guy is just trying to be an ass on purpose his instructions said nothing about using functions but once I submitted it he decided to be more difficult. Ok so I have done a new 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
//Sample code

// second try yaaay



#include <iostream>
#include <string>

using namespace std;

//Call this function below to convert F to C.
float temperature_calc(float f)
{	
    float c;	
    c = (f-32) * 5/9;	
    
    return c;	
}
 


int main(int argc, char** argv) {
	// variable declaration section
	const int ARRAY_SIZE = 7;
	string dayName[ARRAY_SIZE] = { "Mon", "Tue", "Wed", "Thur","Fri", "Sat", "Sun" };
	float dayTEMP[ARRAY_SIZE] = {65, 72, 44, 45, 66, 88, 99};
	
	
	//temp table
	for (int arrayIndex = 0; arrayIndex < ARRAY_SIZE; arrayIndex++)
	{
		cout << dayName[arrayIndex] << " Fahrenheit: "<< dayTEMP[arrayIndex] << endl;
		                            //cant figure out what to call here to call function above
		
	}


	return 0;
}
In C++, 5/9 is 0, because an int divided by an int gives an int.

If you want 5/9 to be 0.555556 , you need to do a float divided by an int.

http://cpp.sh/2r3lp
Repeater's comment is very important. The issue frequently trips up beginners. But in your case, the code will work:
c = (f-32) * 5/9; actually evaluates as:
c = ((f-32) * 5) / 9; f is a float, so 32 gets promoted to float and f-32 is a float
Since f-32 is a float, 5 gets promoted and (f-32)*5 is a float
Since (f-32)*5 is a float, 9 gets promoted and ((f-32)*5)/9 is a float.

kingfnie wrote:
//cant figure out what to call here to call function above
cout << dayName[arrayIndex] << " Celsius: "<< temperature_calc(dayTEMP[arrayIndex]) << endl;

PS: You probably would have gotten a faster response if you stated the actual problem in your post instead of it being hidden in a comment. Just sayin'.
Last edited on
Topic archived. No new replies allowed.