-
Beginner Problem
Alright, I'm a mechanical engineering student taking a mandatory C++ course and I am absolutely terrible at it. I currently need to write a program that I've been trying to write for a week and it's already late. I need to write a function for computing a moving average that is callable from main. If anyone can help me I'd really appreciate it.
-
did you read the tutorial on functions?
-
I have a basic understanding, and I'm able to write the moving average function, I just don't understand at all how to make one that is callable from another function.
-
well, what do you have so far?
-
Can you show us the math you're using to compute the moving average then I'll try to show you how to make it into a program.
-
This is what I have so far
Code:
#include <iostream>
#include <fstream>
using namespace std;
const int N=1000;
int process_speed_data(const double x[N], int n);
int main()
{
ifstream in;
int count(0), data;
double data[N], average[N];
double sum_1(0), sum_2(0);
in.open("sensor.dat");
while (count < (N-1) && !in.eof())
{
in >> data[count];
count++;
}
cout << main(data, count)
//Set first 9 elements of averages array to 0.
for( int i = 0; i < 9 ; i++ )
{
sum_1 += data[i];
average[i] = sum_1 / (i+1) ;
}
//Record moving averages for the remaining elements.
for( int i = 9; i<=count ; i++ )
{
sum_2 = 0;
for( int j = i - 9; j <= i; j++)
{
sum_2 += data[j];
}
average[i] = sum_2 / 10;
}
for( int i = 0; i < count; i++ )
{
cout << average[i]
}
}
EDIT: This code is choppy, the exact assignment is to make the reading and averaging of the data a callable function (process_speed_data) from a primary main function.
-
Code:
cout << main(data, count)
What are you trying to do here? Is this where you want to call the function?
-
I need to make a main function and have the filtering of the data occur in a second function which is called from main.
-
This one actually works, sorry about that other one
Code:
#include <iostream>
#include <fstream>
using namespace std;
const int N=1000;
int main()
{
ifstream in;
int count(0), data;
double prices[N], averages[N];
double sum_1(0), sum_2(0);
in.open("sensor.dat");
while (count < (N-1) && !in.eof())
{
in >> prices[count];
count++;
}
//Set first 19 elements of averages array to 0.
for( int i = 0; i < 9 ; i++ )
{
sum_1 += prices[i];
averages[i] = sum_1 / (i+1) ;
}
//Record moving avergaes for the remaining elements.
for( int i = 9; i<=count ; i++ )
{
sum_2 = 0;
for( int j = i - 9; j <= i; j++ )
{
sum_2 += prices[j];
}
averages[i] = sum_2 / 10;
}
for( int i = 0; i < count; i++ ) {
cout << i << " average = " << averages[i] << endl;
}
}
-
Code:
// Stuff...
int Function( Parameters for calculating speed stuff ) // int Function( int stuff ) for example
{
// Calculate speed stuff
return speed stuff;
}
int main()
{
//Read in the data
cout << Function( Pass data needed to calculate speed stuff ); // Function( 32 ) for example
return 0;
}
That should give you the idea of using a function... :)
- SirCrono6
-
So basically, i should read the data out of the file in main into an array, then send that data to the function to be averaged.
-
Yes, I just skimmed your code to get the idea of what you were doing.
-
Thanks for the help, I'm going to try that, knowing my luck with this, I'll be back.
-
Haly crap, thanks for the help, I finally figured it out. You've managed to do in 5 seconds what my professor couldnt explain in the past 3 weeks. Thanks.