Question

Hi I wrote a code that supposed to double every value in the array. For unknown reason it doesn't display horizontal instead it displays vertical, and it combines the original number with the new double values how do I split them from the each other and display them horizontally. I be very thank full if you could help me.

 int[] A = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };

 Console.WriteLine("\n=====================\n");
 Changes(A); //<<< original index value
 Console.WriteLine("\nEvery Value gets doubled");

 Changes(A); //<<< new changes

The Changes() method:

static void Changes(int[] array) //<<<<<<<< methods
{
    Console.WriteLine("\n=====================\n");
    for (int i = 0; i < array.Length; i++)
    {
        Console.WriteLine("{0}", array[i]); //<<< display orgininal value
        array[i] *= 2;
        Console.WriteLine("{0}", array[i]); // <<new value
    }
    Console.WriteLine("\n=====================\n");
}
Was it helpful?

Solution

Console.WriteLine will show the output vertically

try this instead

        Console.Write("{0} ", array[i]); //<<< display orgininal value
        array[i] *= 2;
         //here I use = to  separate the output put you can use any other string you want 
        Console.Write(" = {0}", array[i])

OTHER TIPS

Use Console.Write() instead of Console.WriteLine()

in case like this I always used to use String.Join() method.

int[] A = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };

Console.WriteLine("\n=====================\n");
Changes(A); //<<< original index value
Console.WriteLine("\nEvery Value gets doubled");

Changes(A); //<<< new changes


static void Changes(int[] array) //<<<<<<<< methods
{
    Console.WriteLine("\n=====================\n");
    Console.WriteLine("{0}", String.Join(", ", array)); //<<< display orgininal values

    for (int i = 0; i < array.Length; i++)
      array[i] *= 2;

    Console.WriteLine("{0}", String.Join(", ", array)); // <<new values
    Console.WriteLine("\n=====================\n");
}

Try this:

You can change all your code for this ;)

int i = -1;
A.ToList().ForEach(x => Console.WriteLine(string.Format("\n=====================\nOriginal value: {0}\nNew value: {1}\n=====================\n", x, A[++i] *= 2)));

=====================

Original value: -2

New value: -4

=====================

And so on.....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top