1

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]);
        }
    }
2
  • Your xr_arr have only 1 value.. Commented Apr 18, 2015 at 11:05
  • Consider better naming of your methods an variables Commented Apr 18, 2015 at 11:13

5 Answers 5

3

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]);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Would it be worth a separate index variable in the building of the array? I know that the (int) cast truncates the decimal but the floating point ops might cause a roundoff problem where you hit 0 and 1 and then overwrite 1 or something
@BenKnoble If the number is small it's not a problem, but with high number as you say, might cause roundoff problems. I updated my code for a better solution.
2
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

2 Comments

There's a mistake in your sample: you don't increment x by 0.01 but by 1 which will yield other (incorrect) results.
Ah I see, you fixed that with your last edit by adding the line var incrementValue = x / 100;
2

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.

Comments

2

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()));
        }
    }
}

Comments

1

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]);
         }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.