Little Fox
Little Fox

Reputation: 1252

How to get values from variable in array?

I have created an array xr_arr that stores variable xr and xr stores 100 values. How can I output first 24 values from my xr_arr without editing x+= step?

static void xn()
{
    double r = 3.9;
    for (double x = 0; x <= 1; x+= 0.01)
    {
        double xr = r * x * (1 - x);
        double [] xr_arr= new double[]{xr};
        for (int y = 0; y <23; y++) { 
          //  Console.WriteLine(xr_arr[y]);
        }
    }

Upvotes: 1

Views: 134

Answers (5)

jdweng
jdweng

Reputation: 34433

You need to use a List<> object so you can add items. See code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            xn();
        }
        static void xn()
        {
            double r = 3.9;
            List<double> xr_arr = new List<double>();
            for (double x = 0; x <= 1; x += 0.01)
            {
                double xr = r * x * (1 - x);
                xr_arr.Add(xr);
            }
            Console.WriteLine(string.Join(",", xr_arr.Take(24).Select(a => a.ToString()).ToArray()));
        }
    }
}

Upvotes: 2

Alex
Alex

Reputation: 13244

You may want to

  1. Pre-allocate the array if you know it is going to contain exactly 100 elements, using var xr_arr = new double[100]; Alternatively, if you don't know the exact number of items upfront, you can use a List<double> to add the new number to.
  2. Wait with trying to print the values until after you are done adding them.

So do it like:

static void xn()
{
    double r = 3.9;
    var n = 0;
    // I think the number of elements could (theoretically) be different from 100
    // and you could get an IndexOutOufRangeException if it is 101.
    var increment = 0.01d;  // 1.0d/300.0d; // <== try this.
    var n_expected = 100;   // 300 // <= with this.
    var x_arr = new double[n_expected]; 

    // alternative: if you cannot be absolutely certain about the number of elements.
    //var x_list = new List<double>(); 

    for (double x = 0; x <= 1; x += increment)
    {
        double xr = r * x * (1 - x);
        x_arr[n++] = xr;
        // x_list.Add(xr); // alternative.
    }

    for (int y = 0; y <23; y++) 
    { 
        Console.WriteLine(xr_arr[y]);
        // Console.WriteLine(xr_list[y]); // alternative.
    }
}

Floating point precision problems

Note that, unlike the general expectation, floating point values are still stored in a limited number of bits, and therefore subject to encoding resolution and rounding effects, make you loose precision if you perform operations such as addition, in a loop. It is a common mistake to expect that real numbers work the same on a computer as in pure math. Please have a look at what every computer scientist should know about floating point arithmetic, and the article it refers to.

In this example, if you had incremented by 1.0/300, expecting to get 300 elements, you would have had a problem.

Upvotes: 2

adricadar
adricadar

Reputation: 10219

Your code need to be modified as below

  1. Move xr_arr outside of first for

  2. Move for that display array after the for that builds it.

New code

double r = 3.9;
double[] xr_arr = new double[100];
// build xr_arr
for (double x = 0; x <= 1; x += 0.01)
{
    double xr = r * x * (1 - x);
    int index = (int) (x * 100);
    xr_arr[index] = xr;
}

var length = 23;
// display xr_arr
for (int y = 0; y < length; y++)
{
    Console.WriteLine(xr_arr[y]);
}

I recommend you to build 2 methods one for building and one for displaying the array. You will have only to win from this, the code will be easier to maintain and reusable.

static void xn()
{
    string data = "   abc df fd";
    var xr_arr = Build(100);
    Display(xr_arr, 23);
}


public static double[] Build(int size)
{
    double r = 3.9;
    double[] xr_arr = new double[size];
    double step = 1.0 / size;
    for (int index = 0; index < size; index++)
    {
        var x = index * step;
        double xr = r * x * (1 - x);
        xr_arr[index] = xr;
    }
    return xr_arr;
}

public static void Display(double[] xr_arr, int size)
{
    var length = Math.Min(xr_arr.Count(), size);
    for (int y = 0; y < length; y++)
    {
        Console.WriteLine(xr_arr[y]);
    }
}

Upvotes: 3

Nipuna
Nipuna

Reputation: 7026

static void xn()
{
    double r = 3.9;
    int count = 100;
    double[] xr_arr = new double[count];

    for (int x = 0; x < count; x += 1)
    {
        var incrementValue = (double)x / (double)100;
        double xr = r * incrementValue * (1 - incrementValue);
        xr_arr[x] = xr;
    }

    for (int y = 0; y < 23; y++)
    {
        Console.WriteLine(xr_arr[y]);
    }
}

Changes done,

  • Variable count will hold total length of xr_arr therefore you can change it easily
  • Moved creation of xr_arr out of for loop so it will get created only once
  • Separated logic of assigning to the array and displaying into 2 for loops

Upvotes: 2

Vikrant
Vikrant

Reputation: 5046

In every for counter; new array is being created. Declare it beofre for loop and then proceed!
As follows:

static void xn()
{
    double r = 3.9;
    double [] xr_arr= new double[100];
    for (double x = 0; x <= 1; x+= 0.01)
    {
        double xr = r * x * (1 - x);
        xr_arr[x]= xr;
        for (int y = 0; y <23; y++) { 
            Console.WriteLine(xr_arr[y]);
         }
    }
}

Upvotes: 1

Related Questions