Thread: Beginner Problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    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.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    did you read the tutorial on functions?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, what do you have so far?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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.
    Last edited by knoxmaddog; 11-21-2005 at 10:26 PM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    cout << main(data, count)
    What are you trying to do here? Is this where you want to call the function?
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    I need to make a main function and have the filtering of the data occur in a second function which is called from main.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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;
    	}
    }

  10. #10
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    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
    Last edited by SirCrono6; 11-21-2005 at 10:40 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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.

  12. #12
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Yes, I just skimmed your code to get the idea of what you were doing.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    Thanks for the help, I'm going to try that, knowing my luck with this, I'll be back.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-09-2006, 12:06 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. very basic beginner problem
    By sandingman1 in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 05:48 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. beginner problem
    By laasunde in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 08:24 AM