0

replace the value of each element of an integer array with the sum of this element value and its index

How to replace the value of each element of an integer array with the sum of this element value and its index in C#

18th Jan 2021, 1:25 PM
O_high
O_high - avatar
6 Réponses
+ 2
Write each element of the returned array using `foreach` foreach( int element in Transform( numbers ) ) { Console.WriteLine( element ); }
18th Jan 2021, 3:04 PM
Ipang
+ 2
Try to change return type into `int[]` rather than `object`.
18th Jan 2021, 2:05 PM
Ipang
+ 1
What is "this" element?
18th Jan 2021, 1:29 PM
Abhay
Abhay - avatar
+ 1
using System; namespace ConsoleApp3 { class Program { public static int[] Transform(int[] numbers) { for (int i = 0; i < numbers.Length; i++) { numbers[i] = numbers[i] + i; } return numbers; } public static void Main(string[] args) { int[] numbers = { 1, 21, 3, 25 }; Console.WriteLine(Transform(numbers)); } } } Thanks! But it's still nothing in the console :-(
18th Jan 2021, 2:57 PM
O_high
O_high - avatar
+ 1
Wow! It worked! Thank you a lot!
18th Jan 2021, 3:16 PM
O_high
O_high - avatar
0
in other words - increase each array element by its index I try something like this, but it's not working: public static object Transform(int[] numbers) { for (int i = 0; i < numbers.Length; i++) { numbers[i] = numbers[i] + i; } return numbers;
18th Jan 2021, 1:53 PM
O_high
O_high - avatar