-2

This is a function to get integer numbers from Console, this parte works right. The trouble is in the part of write the values from the array.

Console.WriteLine("Function get numbers...");

static int[] numbers(int[] values){
    for(int ind = 0; ind < 10; ind++){
        Console.WriteLine("Type a number: ");
        values[ind]= int.Parse(Console.ReadLine());
    }
    return values;
}
numbers(new int[10]);

for(int s= 1; s < 10; s++){
    Console.WriteLine(numbers(new int[s]));
}

I I looked in the documentation but I didn't find a solution.

2
  • 1
    from the array - there is no the array. There are two arrays, unrelated to each other, both of which get discarded after the call to numbers returns. Commented Oct 31, 2022 at 12:52
  • The biggest problem is getting the array values out of the function. Commented Oct 31, 2022 at 18:01

4 Answers 4

0
Console.WriteLine("Function get numbers...");

static int[] numbers(int[] values){
    for(int ind = 0; ind < 10; ind++){
        Console.WriteLine("Type a number: ");
        values[ind]= int.Parse(Console.ReadLine());
    }
    return values;
}
var values = numbers(new int[10]);

for(int s= 0; s < 10; s++){
    Console.WriteLine(values[s].ToString()));
}

I think this works? I didn't try to parse it, it seems

Sign up to request clarification or add additional context in comments.

4 Comments

If you store the returned values into a variable, and then iterate through them starting from 0, not 1, you should get all inserted values printed.
You could leave out the input argument and just create a local 'values' array that is then returned
.ToString() is not necessary since WriteLine does this for you. for(int s= 0; s < values.Length; s++) is better. than using a magic number.
I believe we can all agree that this code, should be burned with fire. He asked why it wasn't working, and I merely adjusted the provided code, so that it would function as expected. That is it. Cleaning it up, and making it efficient, SOLID and clean code, is an entirely different question, in my opinion, and likely the finer points wasted, on a beginner which is still struggling with the concept of return values...... I don't want to be mean, but it could just confuse more than it helps.
0

The simplest way to display an array is to use string.Join() to combine the elements of the array into one comma-separated string

int[] array = new int[] { 1,2,3,4,5 };
Console.WriteLine(string.Join(",", array));

with results

1,2,3,4,5

You can use Environment.NewLine instead of "," to place each value in a new line.

Also, note in your code you are using an array as an argument in your function, which fills in the array and then returns it. But you don't need to pass the array, just the desired size of the array

static int[] numbers(int count){
    int[] values = new int[count];
    for(int ind = 0; ind < 10; ind++){
        Console.WriteLine("Type a number: ");
        values[ind]= int.Parse(Console.ReadLine());
    }
    return values;
}

int[] array = numbers(10);

or fill in however many elements there are in the array already

static void numbers(ref int[] values){
    for(int ind = 0; ind < 10; ind++){
        Console.WriteLine("Type a number: ");
        values[ind]= int.Parse(Console.ReadLine());
    }
}

int[] array = new int[10];
numbers(ref array);

depending on how many times you are going to create a new array, or want to re-use an existing array over and over. The keyword ref is not required here because you can always modify the elements of an array, but it is added as a convention to indicate to the reader that the function numbers() indeed modifies the array argument.

Finally, I am going to explain line by line what is going on to demonstrate why the code isn't behaving as expected

  1. numbers(new int[10]); this creates a new array of 10 elements and passes it the function numbers() the result of the function is never assigned to a local variable and thus lost forever.
  2. for(int s= 1; s < 10; s++){ ... } this is a loop for 9 iterations (1 to 9).
  3. Console.WriteLine(numbers(new int[s])); this creates a new array with s elements (again 1 to 9) and passes it to the function. The result of the function is passed to the WriteLine() method where the default behavior of arrays is to display the type and not the contents of the argument.

Possible fix to the above is to create the array once and access its elements in a loop (using the first update to numbers() from above)

int[] array = numbers(10);
for(int s= 0; s < array.Length; s++){
    Console.WriteLine(array[s]);
}

Note that the iteration count is driven by array.Length instead of the hard-coded number 10.

Comments

0

After read some answer, I got some ideia. This is working now. Thanks for yours help.

Console.WriteLine("Function get numbers...");
int[] values2 = new int[10];
static int[] numbers(int[] values){
    for(int ind = 0; ind < values.Length; ind++){
        Console.WriteLine("Type a number: ");
        values[ind]= int.Parse(Console.ReadLine());
    }
    return values;
}
values2 = numbers(new int[10]);

Console.WriteLine("Array Length: " + values2.Length);

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

Comments

0

Here is another variation of previous examples, outputting array elements as line of numbers, delimited with commas instead of columns of numbers.

                        int[] values2 = new int[10];            
                        int[] numbers(int[] values)
                        {
                            for (int ind = 0; ind < values.Length; ind++)
                            {
                                Console.WriteLine("Type a number, then Enter" + " " + (values2.Length - ind) + " " + "times");
                                values[ind] = int.Parse(Console.ReadLine());
                            }
                            return values;
                        }
                        values2 = numbers(new int[10]);
                        for (int n = 0; n < values2.Length; n++)
                            Console.Write(values2[n] + ",");

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.