LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2005, 10:05 AM   #1
overule
LQ Newbie
 
Registered: Aug 2005
Posts: 3

Rep: Reputation: 0
Question Programming C++ moving average


A moving average is the average of the last n data items. For example if you have the
numbers 1, 2, 3, 4, 5, 6, 7 then the moving average of size 4 is (1+2+3+4)/4 = 2.5, then
(2+3+4+5)/4 = 3.5, then (3+4+5+6)/4 = 4.5 etc.

Anyone knows how to write a for loop to find the average with window size of 4 ?

let say 1,2,3,4,5,6,7 are stored in the array

this is what i got but i think there are errors

Code:
for(counter1 = 0; counter1 < ARRAY_SIZE; counter1++)
	{

		total = 0;

   		for(counter2 = index; counter2 < (WINDOW_SIZE + index); counter2++)
		{
			total = total + storeNumber[counter2];
		}

		index++;

		avg = static_cast<float>(total) / WINDOW_SIZE;
		cout << "The average is: " << setprecision(2) << fixed << avg << " " << index << "\n"; 
			
	}
thanks
 
Old 08-09-2005, 10:34 AM   #2
overule
LQ Newbie
 
Registered: Aug 2005
Posts: 3

Original Poster
Rep: Reputation: 0
please help, I have been thinking this for the whole day
 
Old 08-09-2005, 10:42 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
There is at least 1 minor problem with your code. You are likely going beyond the bounds of your array. Because this looks like a homework question I'm going to let you think about the solution a bit on your own, but think about what happens in your inner loop when counter1 is greater than 3...

Last edited by deiussum; 08-09-2005 at 10:44 AM.
 
Old 08-09-2005, 10:46 AM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Code:
#include <cstdio>
#include <cassert>
#include <cstddef>

using namespace std;

static unsigned
data[] = {1, 2, 3, 4, 5, 6, 7};

static int
avg(unsigned* data, size_t len, size_t size) {
        assert (size > 0);
        assert (size <= len);
        unsigned sum = 0;
        size_t i = 0;
        for (; i < size; ++i)
                sum += *data++;
        printf ("sum = %u\n", sum);
        for (; i < len; ++i, ++data) {
                sum += *data - data[-size];
                printf ("sum = %u\n", sum);
        }
}

int main() {
        avg(data, sizeof data / sizeof (int), 4);
}
dtrt afaict.

hth --Jonas
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
load average? ampex189 Linux - Newbie 2 03-06-2005 07:17 PM
RH 8 load average is messed up Mike-BB Red Hat 1 07-30-2004 05:59 AM
copying/moving stalls when moving a lot of data to a usb stick =X¥®µ§= Linux - Hardware 10 07-30-2004 05:29 AM
Load average 1.0, 1.0, 1.0 ? belated Linux - Newbie 4 11-30-2003 03:49 PM
Average load Cyth Linux - General 1 01-22-2002 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration