Home > Software engineering >  How to use data from array for math equations and terminal display? C
How to use data from array for math equations and terminal display? C

Time:01-18

Complete beginner to C

Having trouble using data from a primitive array in other places. Do i just call them as if they are integers in the math and display output lines? Making a simple Fahrenheit to Celsius converter program.

#include <iostream>
#include <array> 

int main() {
    //Display name of program 
    std::cout << "Farenheit to Celsius Converter"; 
    
    //Temperature array
    int temp[] = {51, 42, 46, 36, 46, 33, 47, 34, 48, 36, 46, 34, 45, 36};
    
    //Math stuff
    int temp1 = temp[1] - 32 * .5556; 
    int temp2 = temp[2] - 32 * .5556;
    int temp3 = temp[3] - 32 * .5556;
    int temp4 = temp[4] - 32 * .5556;
    int temp5 = temp[5] - 32 * .5556;
    int temp6 = temp[6] - 32 * .5556;
    int temp7 = temp[7] - 32 * .5556;
    int temp8 = temp[8] - 32 * .5556;
    int temp9 = temp[9] - 32 * .5556;
    int temp10 = temp[10] - 32 * .5556;
    int temp11 = temp[11] - 32 * .5556;
    int temp12 = temp[12] - 32 * .5556;
    int temp13 = temp[13] - 32 * .5556;
    int temp14 = temp[14] - 32 * .5556;
    
    //Display output
    std::cout << temp1"," << temp2"," << temp3"," << temp4"," << temp5"," << temp6"," << temp7"," << temp8"," << temp9"," << temp10"," << temp11"," << temp12"," << temp13"," << temp14","; 
    return 0;
}

CodePudding user response:

I suggest that you use the standard std::transform algorithm to transform the array. You can then create a function or functor (like a lambda) for converting from Fahrenheit to Celcius and supply that to std::transform.

Example:

#include <algorithm> // std::transform
#include <iostream>
#include <iterator>  // std::begin, std::end

int main() {
    int temp[] = {51, 42, 46, 36, 46, 33, 47, 34, 48, 36, 46, 34, 45, 36};

    // a lambda for the conversion:
    auto F2C = [](int F) { return (F - 32) * 5 / 9; };

    std::transform(std::begin(temp), std::end(temp), std::begin(temp), F2C);

    for (int v : temp) std::cout << v << '\n';
}

Demo

CodePudding user response:

In C use vector for an array with flexible size.

To get a good conversion use floating point numbers.

And repeated calculations should really be functions.

use range based for loops where you can, to avoid index problems

#include <iostream>
#include <array> 
#include <vector>
#include <algorithm>

double fahrenheid_to_celcius(const double t)
{
    return (t - 32.0) * .55556;
}

int main()
{
    //Display name of program 
    std::cout << "Farenheit to Celsius Converter\n";

    //Temperature array
    std::vector<double> temperatures{ 51.0, 42.0, 46.0, 36.0, 46.0, 33.0, 47.0, 34.0, 48.0, 36.0, 46.0, 34.0, 45.0, 36.0 };

    // convert temperatures one by one (using a range based for loop)
    // double& is a reference to one of the temperatures in the vector, so the value can be changed within the loop
    for (double& temperature : temperatures)
    {
        temperature = fahrenheid_to_celcius(temperature);
    }

    bool comma = false;
    for (const double& temperature : temperatures)
    {
        if (comma) std::cout << ", ";
        std::cout << temperature;
        comma = true;
    }

    return 0;
}

CodePudding user response:

First: I recommended using the std::array, instead of the C-array:

const size_t tempValues = 14;
std::array<int, tempValues> temp = {51, 42, 46, 36, 46, 33, 47,
                                    34, 48, 36, 46, 34, 45, 36};

Second: you can operate via a for loop directly on the values. You don't have to write 14 variables:

for (size_t i = 0; i < tempValues; i  ) {
  temp[i] = (temp[i]-32) * .5556;
}

Prining to the output goes similarly:

for (size_t i = 0; i < tempValues - 1; i  ) {
  std::cout << temp[i] << ", ";
}
std::cout << temp[tempValues - 1] << std::endl;

Note that the loop ends with the second last value, because we do not need for the last value a comma separator.

This isn't the most elegant way, but probably the most beginner-friendly.

  • Related