Trey Balut
Trey Balut

Reputation: 1395

Array of doubles

How do I fix this code to fill an Array x with doubles from 1.0 to 15.0?

int temp = 15;

string[] titles = new string[] { "Alpha", "Beta", "Gamma", "Delta" };
List<double[]> x = new List<double[]>();
for (int i = 0; i < titles.Length; i++)
{
    for (int j = 0; j < temp; j++)
    {
        x.Add[i][j]((double) j);
    }       
}

Upvotes: 2

Views: 2011

Answers (2)

xanatos
xanatos

Reputation: 111890

int temp = 15;

string[] titles = new string[] { "Alpha", "Beta", "Gamma", "Delta" };
List<double[]> x = new List<double[]>();

for (int i = 0; i < titles.Length; i++)
{
    double[] y = new double[temp];

    for (int j = 0; j < temp; j++)
    {
        y[j] = j + 1;
    }

    x.Add(y);
}

As a note, x could be an Array.

Or perhaps you don't really need a List of Arrays of double. You could simply use a jagged array of double[].

int temp = 15;

string[] titles = new string[] { "Alpha", "Beta", "Gamma", "Delta" };
double[][] x = new double[titles.Length][];

for (int i = 0; i < titles.Length; i++)
{
    double[] y = new double[temp];

    for (int j = 0; j < temp; j++)
    {
        y[j] = j + 1;
    }

    x[i] = y;
}

Upvotes: 1

N_A
N_A

Reputation: 19897

If the answer to my question above is "yes" then here is an answer:

        int temp = 15;

        string[] titles = new string[] { "Alpha", "Beta", "Gamma", "Delta" };
        List<double[]> x = new List<double[]>();
        for (int i = 0; i < titles.Length; i++)
        {
            double[] test = new double[15];
            for (int j = 1; j <= temp; j++)
            {
                test[j-1] = j;
            }
            x.Add(test);
        }

Upvotes: 1

Related Questions