c++ Simple Program - How to return an array from a function -
this question has answer here:
- can local variable's memory accessed outside scope? 20 answers
- how return local array in c++? 12 answers
i learning c++ @ school , have simple home work do. got working in more simple way wanted try implement calculation through function. returning 3 different values thought of returning array , output values later on main.
i getting lot of strange errors like:
std::cin not element std , lib cmath not being incorporated. have been doing research did not find help.
#include "iostream" #include "cmath" double r; double* calcspheremesures(double r); double* calcspheremesures(double r) { const double pi = 3.1416; double d, area, circumference, volume; double results[3]; d = 2*r; circumference = pi * d; area = (4 * pi * pow(r,2)); volume = (4 * pi * pow(r,3))/3; results[0] = area; results[1] = circumference; results[2] = volume; return results;
}
int main() { std::cout << "please enter radius of circle: "; std::cin >> r; double* results = calcspheremesures(r); std::cout << "************************************" << "\n" << "*area , circumference of sphere*" << "\n" << "************************************" << "\n" << "area" << results[0]<< "\n" << "circumference" << results[1]<< "\n" << "volume" << results[2]; return 0; }
is syntax error or did wrong pointers? saw 1 of solutions problem returning array use pointers not return c type array in c++.
the code working , these strange errors coming visual studio.
#include "stdafx.h" #include "iostream" #include <cmath> double r; double *calcspheremesures(double r); double *calcspheremesures(double r) { const double pi = 3.1416; double d, area, circumference, volume; double results[4]; d = 2*r; circumference = pi * d; area = (4 * pi * pow(r,2)); volume = (4 * pi * pow(r,3))/3; results[0] = area; results[1] = circumference; results[2] = volume; results[3] = d; return results; } int main() { std::cout << "please enter radius of circle: "; std::cin >> r; double *results = calcspheremesures(r); std::cout << "\n**************************************" << "\n" << "* area , circumference of sphere *" << "\n" << "**************************************" << "\n" << "\nradius: " << r << "\n" << "diameter: " << results[3] << "\n" << "area: " << results[0] << "\n" << "circumference: " << results[1] << "\n" << "volume: " << results[2]; return 0; }
Comments
Post a Comment