Place a series of doubles into an array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Place a series of doubles into an array

  1. #1
    Join Date
    Mar 2011
    Posts
    1

    Question Place a series of doubles into an array

    Forgive me if this is a newbie question but I've spent several hours working on trying to figure out alternatives to this before I came here....I'm having some trouble in figuring out what's the best way to achieve what I'm trying to. I'd like to place a series of double values into an array without setting each value like this

    double spread = CSCO.Bar.Close / DELL.Bar.Close;
    spread_series.Add(CSCO.Bar.DateTime, spread);



    if (spread_series.Count > 6)
    {
    double[] dataArray = new double[6];


    dataArray[0] = spread_series.Ago(0);
    dataArray[1] = spread_series.Ago(1) ;
    dataArray[2] = spread_series.Ago(2) ;
    dataArray[3] = spread_series.Ago(3);
    dataArray[4] = spread_series.Ago(4);
    dataArray[5] = spread_series.Ago(5);
    dataArray[6] = spread_series.Ago(6);
    Array.Reverse(dataArray);
    }

    So in other words I'd like to to cycle though all values of spread_series.Ago(0-infinity) and place them into dataArray[i] something like this.

    if (spread_series.Count > 7)
    {
    double[] dataArray = new double[spread_series.Count];
    for (int i = 0; i < dataArray.Length; i++)
    {
    dataArray[i] = spread_series.Ago(i);
    Array.Reverse(dataArray);
    }

    }

    The problem obviously is that I can't implicitly convert a double into an array and the .Ago method requires an (int) in it's reference. I've tried casting but I think the underlying framework prevents it. So I'm left wondering if it's possible to make a simple class or some other method that would allow me to reference all values in the spread_series.Ago() method programmatically so that I wouldn't have to set each value individually or if I don't know how much data I'll be referencing?

    Any help is greatly appreciated!
    Thank You

  2. #2
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Place a series of doubles into an array

    Hi LC.

    Although you've gone to great effort to explain it, I'm still having difficulty understanding the problem, but I can't help but wonder if it's simply that you want to create an array of doubles programmatically thus relieving you of the burdensome task of entering each double explicitly.



    Perhaps it's as easy as usin' a list<double> as temporary programmatic storage for doubles from your source(s) - No array needed. Then convert the list<double> to a double[] when you've gathered all the doubles you wish.




    perhaps sumthin' sorta like the following ? ....
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    
    
    using extensions;
    
    namespace logParser
    {
        public partial class sumthin {
    
    
            // ***********************************************
            // Create a huge source of doubles to 
            // model his "spread_series" (whatever that is)
            // ***********************************************
            static double[] humongous = new double[1492];
            static public double[] hugeArray {get {return humongous;}}
        }
    
    
        public partial class Form1 : Form
        {
    
            // *****************************************************
            // call the function, providing the source of doubles 'spread_series'
            // *****************************************************
            private void testProgrammaticArrayCreation(){
                programmaticallyCreateDoubleArray(sumthin.hugeArray);
            }
    
            // ***********************************************************************
            // here's the function that allows one  to programmatically gather doubles from the source,
            // then converts them to a double[] for easy handling.
            // ***********************************************************************
            public double[] programmaticallyCreateDoubleArray(double[] spread_series) {
                List<double> aggregate = new List<double>();
                double[] result = new double[0];
    
                // **************************************************************************
                // He doesn't tell us what "Ago" is so I feel free to make it whatever I wish
                // which is a double extension function that returns 'i' as a double
                // (See namespace 'extensions', function "Ago" below).
                // **************************************************************************
                if (spread_series.Length > 7) {
                    for (int i = 0; i < spread_series.Length; i++) {
                        aggregate.Add((spread_series.Ago(i))[0]);
                    }
                    result = aggregate.ToArray();
                }
                return result;
            }// end function 'programmaticallyCreateDoubleArray'
            // ***********************************************************************
    
        }// end class
    }// end namespace
    namespace extensions {
    
        public static class ext {
            public static double[] Ago(this double[] a, int  i) {
                return new double[] { (double)i } ;
            }
        }
    
    }
    Last edited by ThermoSight; March 14th, 2011 at 11:10 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center