John McNair - 1 year ago 64
C++ Question

# C++ taking an array of Fahrenheit temperatures, converting them to Celsius, then adding the Celsius temperatures to a new array

Hi so this is my first time on Stackoverflow so I apologize in advance if I'm not to clear with what I'm asking. I have an assignment for class to take an array of temperatures in Fahrenheit, convert them to Celsius, and then place those converted numbers into another array of the same size. My issue seems to be with memory locations and getting the actual value from the Fahrenheit array and performing the arithmetic to convert that value. I've searched for a while to try and find more information on this because it seems pretty basic but for some reason cannot find any help, thus my reasoning for joining Stackoverflow. Any help or suggestions (including those telling me to just blow my brains out) or links to tutorials would be greatly appreciated.

``````#include <string>
#include <iostream>
using namespace std;

void toCelsius(double fahrenheitTemps[],double *celsiusTemps,int n){

for (int i = 0; i < n; i++) {
*(celsiusTemps + i) = (((*(fahrenheitTemps + i)) - 32)*(5 / 9));
cout << ((*(fahrenheitTemps + i) - 32)*(1.8)) << endl;

}

}
void PrintResults(double tF[], double tC[], int n) {

cout << "Fahrenheit Temps: ";
for (int i = 0; i < n; i++) {
cout << tF[i] << " ";
}
cout << endl;

cout << "Celsius Temps:    ";
for (int i = 0; i < n; i++) {
cout << tF[i] << " ";   //This was my main issue and mistake (it should be tC) view update to see corrected code
}
cout << endl;
}

int main(){
double tempF[] = { 33.5,67.5,67.5,88.0,46.0,94.5,77.5,83.0,95.0,80.5 }, tempC[10];
double * ptrTempF, * ptrTempC;
*ptrTempF = &tempF;
ptrTempC = tempC;

for (int i = 0; i < 10; i++) {
*(tempC + i) = (*(tempF + i) - 32)*(5 / 9);
cout << tempC << " " << tempC[i] << endl;
}

toCelsius(tempF, tempC, 10);
PrintResults(tempF, tempC, 10);
}
``````

Update: I'm just a complete moron everything you guys suggested genuinely fixed some of the bug problems I was having. However the reason the array
`tempC`
looked like it wasn't changing in the output was because I put in the function
`printResults`
`cout << tF[i] << " ";`
where
`cout << tC[i] << " ";`
should have been. My output looked made it look like nothing was working since when it was.

This is the working form of the code just in case anyone googles this and wants to see the working code:

``````#include <string>
#include <iostream>
using namespace std;

void toCelsius(double fahrenheitTemps[],double * &celsiusTemps,int n){ //I think & made a difference

for (int i = 0; i < n; i++) {
celsiusTemps[i] = ((fahrenheitTemps[i] - 32.0)*(5.0 / 9.0));
}

}

void PrintResults(double tF[], double tC[], int n) {

cout << "Fahrenheit Temps: ";
for (int i = 0; i < n; i++) {
cout << tF[i] << " ";
}
cout << endl;

cout << "Celsius Temps:    ";
for (int i = 0; i < n; i++) {
cout << tC[i] << " ";
}
cout << endl;
}

int main(){
double tempF[] = { 33.5,67.5,67.5,88.0,46.0,94.5,77.5,83.0,95.0,80.5 }, tempC[10];
double * ptrTempC;
ptrTempC = tempC;

toCelsius(tempF, ptrTempC, 10);
PrintResults(tempF, tempC, 10);
}
``````

`````` double * ptrTempF, * ptrTempC;
*ptrTempF = &tempF;
``````

`ptrTempf` is a pointer to a double. Therefore `*ptrTempF` is a double.

Setting aside that this is undefined behavior, dereferencing an uninitalized pointer, this assigment attempts to assign a pointer to a double array to a double. That, of course, makes no sense. The assignment should really be:

``````ptrTempF = tempF;
``````

The second bug:

``````*(celsiusTemps + i) = (((*(fahrenheitTemps + i)) - 32)*(5 / 9));
``````

"5/9" is integer division, which calculates to 0. You need to make it a `double` division:

``````*(celsiusTemps + i) = (((*(fahrenheitTemps + i)) - 32)*(5.0 / 9));
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download