Home > Blockchain >  How to increase the array by 1 with each input
How to increase the array by 1 with each input

Time:12-17

My goal is, at every new input to the question which i marked, the array gets a new input. For example:

double[] Test = new double[10];
"give input" | 
int input = int.Parse(Console.ReadLine()) |
Test[0] = input |

Again to "give input". just that circle and with every input the "Test [HERE]" gets there a new input. (Like if u would do that manually)

sry for my bad english. english insn't my native language.

static void Main(string[] args)
        {
            Mittelwert();
        }
        public static void Mittelwert()
        {
            double[] Test = new double[10];
            for (int i = 1; i < 11; i  )
            {
                Console.WriteLine("Geben Sie ihren "   i   " Wert ein");
                int input = int.Parse(Console.ReadLine());

                Test[ 1] = input;

            }
            var Average = Enumerable.Average(Test);
            Console.WriteLine("Der Durchschnitt ist "   Average);

        }
    ```

CodePudding user response:

You can use i as the index of the array, but note that C# arrays are zero-based (i.e., the first index is 0, the second is 1, etc):

for (int i = 0; i < 10; i  )
{
    Console.WriteLine("Geben Sie ihren "   (i  1)   " Wert ein");
    int input = int.Parse(Console.ReadLine());

    Test[i] = input;
}

CodePudding user response:

If I understand what you are trying to do, you want to iterate a number of times over a collection and once the iterator passes the length of the collection, you want to essentially append new items to the end.

For this Arrays are not the best option, you want to instead use List<T> which scales up as you add new items.

    static void Main(string[] args)
    {
        Mittelwert();
    }
    public static void Mittelwert()
    {
        List<double> Test = new List<double> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        for (int i = 1; i < 11; i  )
        {
            Console.WriteLine("Geben Sie ihren "   i   " Wert ein");
            int input = int.Parse(Console.ReadLine());

            if (Test.Count < i)
                Test[i] = input;
            else Test.Add(input);

        }
        var Average = Enumerable.Average(Test);
        Console.WriteLine("Der Durchschnitt ist "   Average);

    }
  • Related