Okay here is the problem I am having.. I am trying to make an array based on a user input.. That means the size of the array is unknown.. Here is what the program should look like.

How many expierments do you want to do? Insert user input here
expierment 1:
expierment 2:
expierment 3:

And so on.. I don't want you to give me the program solution I am just asking of how to create the dynamic array?

Thank you

That means the size of the array is unknown.

That means you shouldn't be using an array. Use a List<> instead, unless this is a homework assignment where the whole point is to learn how to work with arrays. Though such an assignment seems silly since the Array class has supported a Resize() method since .NET 3.5.

The instructor said we can use whatever we want.. But I chose and array to make it work.. I kinda made the program accept the multiple user inputs.. Here is how

namespace ActualLab1
{
    class Program
    {
        static void Main(string[] args)
        {

            int input;

            Console.WriteLine("Enter the number of expriments you want to make:");

            input = int.Parse(Console.ReadLine());

            int[] index = new int[input];

            if (input > 2 && input < 6)
            {
                for (int counter = 0; counter < index.Length; counter++)
                {

                    Console.WriteLine("Enter number" + counter + ":");

                    index[counter] = int.Parse(Console.ReadLine());

                }
            }

            else
            {
                Console.WriteLine("Your input is not valid.");
            }
        }
    }
}

That Should make it work but now I have to get the average of these inputs.. I hope i can figure it out.

if you use LINQ, index.Average() I think will work

Hmmmmm,If you want to get the avaerage of the inputs that you're getting from your users.
1- Loop through the inputs and store them in one variable by using += operator or variable = variable + array[index] / array.Length.

I reckon you should catch exceptions like FormatException and other ones. your program crashes I enter a string in there.

Try this.

        int input;
        Console.WriteLine("Enter the number of expriments you want to make:");
        input = int.Parse(Console.ReadLine());
        int[] index = new int[input];
        if (input > 2 && input < 6)
        {
            int temp = 0;
            int avg = 0;

            for (int counter = 0; counter < index.Length; counter++)
            {

                Console.WriteLine("Enter number" + counter + ":");
                index[counter] = int.Parse(Console.ReadLine());

                temp += index[counter];
            }

            avg = temp / index.Length;
            Console.WriteLine("Average:" + avg  + "\nwe have :" +temp + " experiments");
        }
        else
        {
            Console.WriteLine("Your input is not valid.");
        }

Oh thank you rotten69 I think this is the right solution.. I am going to try it, and I will let you know if it works.

You're welcome, a comment in there and pressing the up arrow will do. And if this solves your problem then mark this thread as solved so others can benefit from it.

Just to warn you, your program will crash if the user inputs any negative number or non-numeric.

To overcome the second part you can use int.TryParse which returns true/false if it could be parsed. The first is a simple if statement to check for negativity. :)

I used a try catch to solve this problem.. Yours could be more efficent but I am not going to make it complicated.. Although I am going to try your way and comment it out.. Thanks!

I Know you've got your answer but I wanted to add my 2 cents. As deceptikon points out you should look on using List<>

The reason I felt I wanted to point this out is because when I learned to program in Java I quickly fell in love with using arrays. That transfered across into new langauges that I learned.

I would use the Array.Resize()and everything, but then someone asked once, why not use a List? Since then I now rarely find myself using arrays, I mean pretty much I only use them in other languages.

So my advice, if you get the chance again, use Lists, they are very useful, again I don't even use arrays anymore.

Also I will say I still do the try/catch for my Covert.ToInt32(), just easier to me then the Int.TryParse (which that Convert library is awesome, it's spoiled me)

Whilst Convert can be handy, unless you know you can convert to the target, I wouldn't use it.

Using try/catch to control program flow is a bad idea and makes the code far more unreadable than it needs to be...

Yours:

do
{
    Int32 myNumber;
    Boolean isNumber;
    try
    {
        String numberInput = Console.ReadLine();
        myNumber = Convert.ToInt32(numberInput);
        isNumber = true;
    }
    catch(Exception exc)
    {
        // We can't be sure that the exception was caused by Convert.ToInt32
        // But this is the best guess. This will cause problems if it was something
        // more serious. To correct this, we'd need a nested try/catch
        isNumber = false;
    }

    if(!isNumber)
        Console.WriteLine("You need to input a number!");
}while(!isNumber);

Mine:

do
{
    Int32 myNumber;
    String userInput;
    Boolean isNumber;

    try
    {
        userInput = Console.ReadLine();

        isNumber = Int32.TryParse(userInput, out myNumber);

        if(!isNumber)
            Console.WriteLine("You must enter a number!);
    }
    catch(Exception exc)
    {
        Console.WriteLine("A serious error occurred: ", exc.Message);
        return;
    }

}while(!isNumber);

Easier to read, better control of circumstances and my boolean is driven by an output value, not by reaching a certain point in code.

Admittedly yes, you should also be using lists (They're easy, trust me!). Resizing an array has serious performance implications and in other languages, the array size must be a compile time constant. It's better not to get into bad habits, whether or not you'd use those other languages. :)

commented: True but I rather make it simple with a try catch.. The instructor is not easy, that's why I went with it. +0

I am facing another problem now.. That when the user enters an invalid input and let's say "g" the program keeps going on the loop without going back to it.. I can't recall how to fix that from my previous experience.. Does anyone know how?

The keyword you need is continue;

This will reset you to the beginning of the loop.

commented: Sorry didn't work.. I think I am close.. I am gonna mark this one solved because I think I can fix the problem soon. Thank you everyone for your help +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.