Thread: Averaging Algorithm

  1. #16
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.


  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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

  3. #18
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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.

  4. #19
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM