0

I am confused as to what to put as the parameter for "output();"

I need to have it display the content of the array that has been created but I am getting confused as its an array and not just a variable... I thought it should be output(value); but that and many other guesses you might say have not worked.

I was taught you put in there what variables you created in main but that did not work.

using System;


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

        int value;

        Console.Write("How big of an Array? ");
        int arraySize = int.Parse(Console.ReadLine());
        int[] arr = new int[arraySize];

        for (int i = 0; i <= arraySize - 1; i++)
        {
            Console.Write("First Value: ");
            value = int.Parse(Console.ReadLine());
            arr[i] = Convert.ToInt32(value);
        }

        output();
    }

    static void output(ref int value, ref Array arr, ref int arraySize)
    {

        foreach (int i in arr)
        {
            for (int v = 1; v <= arraySize; v++)
            {
                string number = "Number: ";

                Console.WriteLine("{0}{1} {2}", number, v, i);
                Console.ReadLine();
            }
        }
    }
}
}
6
  • You are calling ouput but not passing anything when it is expecting 3 parameters ? Commented Jul 3, 2012 at 15:46
  • Why do you pass the value argument? Just pass the array and loop thru it to print its content! Commented Jul 3, 2012 at 15:48
  • Also now that someone explained it. I have gone back and it the checkbox for the best answer on ALL previous questions. Commented Jul 3, 2012 at 15:50
  • @thatdude - apologies for not explaining - respect for taking action Commented Jul 3, 2012 at 15:52
  • @thatdude good ! That will certainly help when you want people to answer your questions. The best answer is actually a really important part of the Question/Answer model of StackExchange sites. Commented Jul 3, 2012 at 16:00

4 Answers 4

2

First, why all the refs? They seem to not serve any purpose, as well as the first parameter which is never used:

static void output(Array arr, int arraySize)
    {

        foreach (int i in arr)
        {
            for (int v = 1; v <= arraySize; v++)
            {
                string number = "Number: ";

                Console.WriteLine("{0}{1} {2}", number, v, i);
                Console.ReadLine();
            }
        }
    }

Now use

output(arr, arraySize);

The second parameter can also be stripped, since C# arrays hold their length in a field:

static void output(Array arr)
    {

        foreach (int i in arr)
        {
            for (int v = 1; v <= arr.Length; v++)
            {
                string number = "Number: ";

                Console.WriteLine("{0}{1} {2}", number, v, i);
                Console.ReadLine();
            }
        }
    }

and use:

 output(arr);
Sign up to request clarification or add additional context in comments.

3 Comments

is the arraySize parameter needed either? - oh and +1
@Andras Zoltan: Not really. I'll make a note of that.
I now understand the ACCEPT answer function. Sorry bout not using it prior to today. Thank you for this, I forgot about order. THat being arr, arraySize order when I passed these to the method.
2

You should send the array as a parameter to this method:

static void output(int[] arr)
{
    for (int i = 0; i< arr.Length; i++)
    {

        string number = "Number: ";
        Console.WriteLine("{0}{1} ", number, arr[i]);
        Console.ReadLine();
    }
}

Comments

2

Use this :

 static void output(int[] myArray)
 {
    for (int i = 0; i< myArray.Length; i++)
   {

    string number = "Number: ";
    Console.WriteLine("{0}{1} {2}", number, v, i);
    Console.ReadLine();
   }
 }

but dont forget to pass the parameters when you call the function :

output(myArray)

because output is expecting to receive an array. Drop all the ref too.

The myArray.Length already stores the size of the array.

Comments

0

I'm not sure what the value parameter is for, but arr would be your array, arr, and arraySize is your variable arraySize. So do output(value, arr, arraySize)

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.