user2514307
user2514307

Reputation: 59

How to declare an array in C#?

How can I declare array without setting initial set of elements? Or is it mandatory to input initial set of elements? Here are my codes:

class Program
{
    static void Main(string[] args)
    {
        string ans, uname, choice, input;
        int temp;

        Console.WriteLine("Hi! We're Adrianne and Marco, today is " + DateTime.Now + ", what's yours?");
        uname = Console.ReadLine();

        do
        {
            Console.WriteLine("Hello, " + uname + "! Please select a function:");
            Console.WriteLine("1: Palindrome");
            Console.WriteLine("2: Prime or Not Prime");
            Console.WriteLine("3: Bubble Sort");
            Console.WriteLine("4: Fibonacci");
            choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.WriteLine("Enter any word or string:");
                input = Console.ReadLine();
                temp = Palindrome(input);

                if (temp == 0)
                    Console.WriteLine(input + " is not a palindrome...");
                else
                    Console.WriteLine(input + " is a palindrome!");
            }

            else if (choice == "2")
            {
                Console.WriteLine("Enter a number:");
                input = Console.ReadLine();
                temp = Prime(input);

                if (temp == 0)
                    Console.WriteLine(input + " is prime");
                else if (temp == 1)
                    Console.WriteLine(input + " is not prime");
                else
                    Console.WriteLine(input + " is neither prime nor composite");
            }

            else if (choice == "3")
            {
                int h;
                string tempo;
                double[] inputs = { 0, 0, 0, 0, 0, 0, 0, 0 };

                for (int i = 0; i < 8; i++)
                {
                    h = i + 1;
                    if (i == 0)
                    {
                        Console.WriteLine("Enter 1st number:");
                        tempo = Console.ReadLine();
                    }

                    else if (i == 1)
                    {
                        Console.WriteLine("Enter 2nd number:");
                        tempo = Console.ReadLine();
                    }

                    else if (i == 2)
                    {
                        Console.WriteLine("Enter 3rd number:");
                        tempo = Console.ReadLine();
                    }

                    else
                    {
                        Console.WriteLine("Enter " + h + "th number:");
                        tempo = Console.ReadLine();
                    }

                    inputs[i] = Convert.ToDouble(tempo);
                }

                bubbleSort(inputs);
                Console.WriteLine(inputs[0] + " " + inputs[1] + " " + inputs[2] + " " + inputs[3] + " " + inputs[4] + " " + inputs[5] + " " + inputs[6] + " " + inputs[7]);
            }

            else if (choice == "4")
            {
                Console.WriteLine("Enter a whole number:");
                input = Console.ReadLine();

                int temp3 = Convert.ToInt16(input);

                int[] fibNums = {0, 0, 0, 0, 0};

                //for (int i = 0; i < fibNums.Length; i++)
                //{
                //    Console.WriteLine(fibNums[i]);
                //}
                int temp4 = 0;
                do
                {
                    temp = fibSequence(temp4);
                    fibNums[temp4] = temp;
                    temp4++;
                } while (temp <= temp3);

                for (int i = 0; i < fibNums.Length; i++)
                {
                    Console.WriteLine(fibNums[i]);
                }
            }

            Console.WriteLine("There, all finished! Try again? (Y/N)");
            ans = Console.ReadLine();

        } while (ans == "Y");

        Console.WriteLine("Thank you, come again!");
        Console.Read();

    }

error is on this part "int[] fibNums = {0, 0, 0, 0, 0};". In my code, the array could actually have infinite number of elements, depending on the input. But since, I don't know how to declare an array, the number of elements is only limited to the number of elements that I initialize it.

Upvotes: 0

Views: 1754

Answers (5)

Eric Lippert
Eric Lippert

Reputation: 660138

How can I declare a local variable of array type and initialize it without providing an array initializer?

You already know how to declare a local variable of array type and initialize it with an initializer:

double[] inputs = { 0, 0, 0, 0, 0, 0, 0, 0 };

This syntax -- the use of an array initializer with nothing else around it -- is legal only in a local variable or field declaration.

There are other syntaxes for using an initializer as well that you might not know about. These are legal anywhere an expression is legal, not just in an initializer:

double[] inputs = new double[] { 0, 0, 0, 0, 0, 0, 0, 0 };
double[] inputs = new double[8] { 0, 0, 0, 0, 0, 0, 0, 0 };

This syntax is useful when you can't say or do not wish to say the type. However, you need to make sure that you get the types of the elements right:

double[] inputs = new[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };

Notice that I used 0.0 there instead of 0. When you say new[] the compiler attempts to deduce the array element type from the initializers, and in the original case it would have deduced int[], not double[].

You asked how to avoid the initializer syntax though. You say:

double[] inputs = new double[8];

It will be automatically initialized to zeros.

As others have pointed out, you might be better off using List<double> or some other collection type.

Upvotes: 4

Ibrahim Attia
Ibrahim Attia

Reputation: 91

solution:

first : you need to declare an array

int[] fibNums = new int[temp3+1];

we use temp3+1 because we start from 0

then we change condition of loop to

} while (temp < temp3);

the final code is :

                int[] fibNums = new int[temp3+1];
                //for (int i = 0; i < fibNums.Length; i++)
                //{
                //    Console.WriteLine(fibNums[i]);
                //}
                int temp4 = 0;
                do
                {
                    temp = fibSequence(temp4);
                    fibNums[temp4] = temp;
                    temp4++;
                } while (temp < temp3);

Upvotes: 0

svick
svick

Reputation: 244837

If you want to create an array with a specific number of elements, use the new operator:

int[] fibNums = new int[temp3];

This will create an array with temp3 elements, each initialized to the default value, which is 0 for int and other numeric types.

BTW, calling your variables like temp3 is a bad practice, use descriptive names instead.

Upvotes: 0

Fabian Bigler
Fabian Bigler

Reputation: 10895

How can I declare array without setting initial set of elements?

Use System.Collections.Generic.List instead of arrays.


How to instintiate a List:

var fibNums = new List<int>();

Then you can simply add the numbers as needed:

int yourNumber = 1;
fibNums.Add(yourNumber);

And if you need an array of integer, you can still use the 'ToArray'-Function of your list:

fibNums.ToArray();

Upvotes: 3

Dennis Traub
Dennis Traub

Reputation: 51634

I think a List<int> would be better suited for your purpose. It will automatically extend itself as needed.

List<int> fibNums = new List<int> {0, 0, 0, 0, 0};

Upvotes: 6

Related Questions