Well I want to find out how many miles per gallon my car is getting. But the restrictions are:
1. Cannot look at the odometer.
2. Cannot use division.
3. Cannot use addition.
4. Cannot use math.
5. Cannot be your own car.
Well I want to find out how many miles per gallon my car is getting. But the restrictions are:
1. Cannot look at the odometer.
2. Cannot use division.
3. Cannot use addition.
4. Cannot use math.
5. Cannot be your own car.
My program works with values greater than 20 but not an amount of numbers greater than 20 but you can put a do while loop around it and just go back and add in your previous average.
HMMMM I normally put in iostream only but my compiler put it in as .h
So sorry
Here is a fun program that produces an approximation of the average :-)
Code:#include <iostream> #include <vector> double approx_average(std::vector<double> vec, double accuracy); double range(std::vector<double>& vec); int main() { std::vector<double> x; double x_i; double accuracy = 0.001; while (std::cin >> x_i) { x.push_back(x_i); } if (x.size()) { double ave = approx_average(x, accuracy); std::cout << ave << std::endl; } return 0; } double approx_average(std::vector<double> vec, double accuracy) { while (range(vec) > accuracy) { for (int i = 0; i < vec.size() - 1; ++i) { double diff = (vec[i] - vec[i + 1]) * 0.5; vec[i] -= diff; vec[i + 1] += diff; } } return vec[0]; } double range(std::vector<double>& vec) { double min = vec[0]; double max = vec[0]; for (int i = 1; i < vec.size(); ++i) { double curr = vec[i]; if (curr < min) min = curr; if (curr > max) max = curr; } return max - min; }
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.
This is very simple!
Use exponents!
Code:x/9 = x * (9^-1)Code:long double sum, avg; unsigned int n; avg = sum * pow(n, -1);