Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:49:11+00:00 2026-06-14T07:49:11+00:00

I have this function which is SMA (Simple Moving Average). The result in the

  • 0

I have this function which is SMA (Simple Moving Average). The result in the array I display as graph in ZedGraph and now it will start from 0 to 1956. I want the graph to start from frameSize / 2 in this case example it will be 300 / 2 so 150 so the graph should start from 150 to 2016.

I don’t want to make the graph to grow I mean the array should stay length as 1956 I just want it to be pushed by 150 indexes from the beginning so it will start from index 150 instead of 0.

So this is the SMA function:

private static double[] smaDoubles(int frameSize, int[] data)
{
    int padding = frameSize / 2;
    double sum = 0;
    double[] avgPoints = new double[(padding + data.Length) - frameSize + 1];

    for (int counter = padding; counter <= data.Length - frameSize; counter++)
    {
        int innerLoopCounter = 0;
        int index = counter;
        while (innerLoopCounter < frameSize)
        {
            sum = sum + data[index];

            innerLoopCounter += 1;

            index += 1;

        }

        avgPoints[counter] = sum / frameSize;

        sum = 0;

    }

    return avgPoints;

}

In the for loop counter = padding before it was counter = 0 so the result of that is in the image here.

The green one is the SMA from this function. And the green start from 150 but ends at 1956 and it should end at 2106. When I moved it to start from 150 I want the whole graph to move as one unit by 150 so it will start from 150 and end in 2106. The red graph should stay the same all

How can I do it?

Now as it is in the image the graph end by 300 from the right edge.

This is the function as it is now i changed the line: double[] avgPoints = new double[data.Length – frameSize + 1]; this is how it was original so i changed it to this one now.
And the function get frameSize as 3 and data as [10] and im getting the same exception:

private static double[] smaDoubles(int frameSize, int[] data)
        {
            int padding = frameSize / 2;
            double sum = 0;
            double[] avgPoints = new double[data.Length - frameSize + 1];

            for (int counter = padding; counter <= data.Length - padding; counter++)//for (int counter = padding; counter <= data.Length - frameSize; counter++)
            {
                int innerLoopCounter = 0;
                int index = counter;
                while (innerLoopCounter < frameSize)
                {
                   // if (index < data.Length)
                    sum = sum + data[index];

                    innerLoopCounter += 1;

                    index += 1;

                }

                avgPoints[counter] = sum / frameSize;

                sum = 0;

            }

            return avgPoints;

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:49:12+00:00Added an answer on June 14, 2026 at 7:49 am

    Are you sure:

    for (int counter = padding; counter <= data.Length - frameSize; counter++)

    shouldn’t be:

    for (int counter = padding; counter <= data.Length - padding; counter++)

    And then your function that computes the moving average should go from counter – padding to counter + padding instead of from counter to counter + frameSize.

    To debug this type of problem, it’s often helpful to try it which a much smaller data set where you can compute the expected result by hand and see if your algorithm matches your expectations. I don’t believe that your algorithm is necessarily calculating what you think it’s calculating here. Try it with 10 data elements and a window size of 3 to see if you’re getting the results that you expect.

    Note, the first code line actually contains 2 logic errors, one of which is not necessarily apparent until you try the code 2nd line. The error is preserved for illustrative purposes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple function which toggles a hidden element in the webpage. function
I have this function which, when I click a button, another div will be
I have this function which will validate an email address. jsLint gives an error
I have this function which takes the average user rating given to an item
I have this function which is supposed to set a certain time format to
I have this function which pings my server for a specific change in data:
I have this function which gives me ( for a current date ) -
I have this function which returns a datatype InetAddress[] public InetAddress [] lookupAllHostAddr(String host)
Hi have made this function which is made to replicate an error that I
I have a function which calculates distance from database records. This function is called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.