0

I'm at a loss here, so I am looking for any hints to point me in the right direction. I can't figure out how to input the Celsius values that I converted from the Fahrenheit temperatures into the centigrade array. I tried to work in another for loop for that very purpose but it only outputs the last value for C after the calculation from the first for loop. Any ideas? Thanks in advance.

// Temperature Converter

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

int main()

double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
double centigrade[] = { 0 }, C(0);
int i(0);

cout << setw(13) << "Farenheit " << setw(9) << " Centigrade";
cout << endl;

    for (double t : temps)
    {
      C = (t - 32) * 5 / 9;
      cout << setw(10) << t << setw(12) << C;
      cout << endl;
    }
    for (i = 0; i <= 12; i++)
    {
      centigrade[i] = C;
      cout << centigrade[i] << endl;
    }
return 0;
}

3 Answers3

1

Here is a full working example based on the other answer.

#include <iostream>

using std::cout;
using std::endl;

int main() {

    double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
    const int count = sizeof(temps) / sizeof(temps[0]);

    double centigrade[count];

    for (int i = 0; i < count; i++) {
        centigrade[i] = (temps[i] - 32) * 5 / 9;
        cout << centigrade[i] << endl;
    }
    return 0;
}

If you want to work without an explicit indexing loop, then replace double centigrade[count]; with std::vector<double> centigrade, and replace the loop with:

for (double t : temps)
    centigrade.push_back((t - 32) * 5 / 9);

If you then wanted an array back for some reason, you could use this trick to get an array back:

 double* array_version = &centigrade[0];
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • @dyp, Thanks for suggestions and updated. `-pedantic-errors` is a useful flag to know about. – merlin2011 Apr 05 '14 at 23:28
  • is there anyway you could keep the range based loop in that set up? – user3465469 Apr 06 '14 at 01:22
  • If you are referring to the `for (double t : temps)`, then you either have to a) maintain an explicit index or b) use `std::vector` instead of array and just use `push_back()` to add to it. – merlin2011 Apr 06 '14 at 01:24
  • @user3465469 Libraries like `boost` provide a *zip iterator* which allows iterating simultaneously through multiple ranges (see http://stackoverflow.com/q/8511035/420683). But you can use `std::transform` here for the transformation Fahrenheit -> Celsius plus a second loop for the output. This isolates the different steps performed (which allows putting them into separate functions for a more complex program). – dyp Apr 06 '14 at 10:44
  • @merlin2011 and dyp I appreciate all the help, but as far as the using `std::vector` and `push_back()`, in regards to keeping a range-based for loop, that is out of the scope of how I wanted to do this program. I am gonna just set your initial answer as the correct one and go it alone till I find something suitable. Thanks. – user3465469 Apr 07 '14 at 14:20
  • @user3465469, Please see the new answer I posted and see if that is what you were looking for. – merlin2011 Apr 07 '14 at 18:02
0

Store values in the array in the first loop itself..

for (i=0;i<=12;i++)
{
     centigrade[i]= (temps[i] - 32) * 5 / 9;
     cout << setw(10) << temps[i] << setw(12) << centigrade[i];
     cout << endl;
}

U can generalize the for loop by finding the size of temps array dynamically..maybe

sizeof (temps) / sizeof (temps[0]);

Also allocate memory for centigrade array accordingly.

GoldRoger
  • 1,263
  • 7
  • 10
0

I am adding a new answer based on clarifications to the OP's question, rather than updating my existing answer, because I feel the context is more clear this way.

If you want to use a range-based loop, and avoid std::vector, there is a way to do it, but this solution is more in the spirit of C thanC++, because it uses pointer arithmetic.

#include <iostream>

using std::cout;
using std::endl;

int main() {

    double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
    const int count = sizeof(temps) / sizeof(temps[0]);

    double centigrade[count];
    double * walker = centigrade;

    for (double t : temps)
        *walker++ = (t - 32) * 5 / 9;

    // verify results by printing.    
    for (double t: centigrade)
        cout << t << endl;


    return 0;
}
merlin2011
  • 71,677
  • 44
  • 195
  • 329