Array reverse will not work.

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hello,

Just though I make an array and then put the output to the console which went well...

C#:
char[] cArray = { 'A', 'B', 'C', 'D', 'E' };
            for (int i = 0; i < cArray.Length; i++)
            {
                for (int j = 1; j <= 10; j++)
                {
                    string sf01 = string.Format($"{cArray[i]}{j}");
                    if (sf01.Equals("C5") == true) break;
                    Console.Write(sf01); Console.Write(" ");
                }
                Console.WriteLine();
            }

Then I tried to reverse this output so that it prints the Array outcomes: A1, A2, A3...... in a reversed format with this code:

C#:
string reversedString = " ";
            for (int index = cArray.Length - 1; index >= 0; index--)
            {
                reversedString += cArray[index];
            }
            Console.WriteLine($"Reversed Array: {reversedString}");


            Console.ReadKey();

But the problem is that it literally reverses the actual char cArray, which is kind of pointless because the whole point my exercise was to reverse the actual array output.

What I can see that 'j' which is used as a local variable is the main piece that holds all of the information. I tried making that public and it hated it.

Is there a way to make the j public and then use it to reverse the array outcome? Maybe re-write parts of the main code?

Thanks
 
I'm afraid that your description doesn't really make sense. It appears that your first code will generate the following output:

A1 A2 ... A9 A10
B1 B2 ... B9 B10
C1 C2 C3 C4
D1 D2 ... D9 D10
E1 E2 ... E9 E10

Is that what you're seeing? If not, what are you seeing?

What are you actually trying to achieve with that second code snippet? Are you trying to get similar results to the first but with the lines in reverse order? The values on each line reversed? Both? Something else?
 
It appears that your first code will generate the following output:

A1 A2 ... A9 A10
B1 B2 ... B9 B10
C1 C2 C3 C4
D1 D2 ... D9 D10
E1 E2 ... E9 E10

Yeah, thats exactly correct. That is the output that I am getting.

However, I am now trying to reverse that so that it starts from E10 and goes to A1.

But at run time I am seeing:

'your string reversed: E,D,C,B,A'.

So it's reversing the letters and not the letters and the numbers.
 
The second code should be almost exactly the same as the first code, just with the limits for the loops reversed. You'll have to give some thought to what to do for C though, because reversing the loop limits will produce C10 to C6 rather than C4 to C1.
 
Back
Top Bottom