1

I'm writing string[] to show multiple rows in the output. A stupid question, why the output is not 5 x 5?(I used i and j both from 0-4, then the ouput should be an array 5 x 5), can anyone help?

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>0 30 3 13 25 30 </string>
<string>0 30 10 22 27 </string>
<string>0 30 12 17 </string>
<string>0 30 5 </string>
<string>0 30 </string>
</ArrayOfstring> 

Here's my input: .../3/10/12/5

Codes below:

    public IEnumerable<string> route5(int city1, int city2, int city3, int city4)
    {
        int[] city = new int[] { 0, city1, city2, city3, city4 };
        string[] layout = new string[5];
        
        for (int i = 0; i < 5; i++)
        {
            string message = "";
            for (int j = 0; j < 5; j++)
            {
                if (j - i == 0) message= "0"+" ";
                if (j > i)
                {
                    if (j - i == 1) message+= (city[j]).ToString()+" ";
                    else if (j - i == 2) message+= (city[j] + city[j - 1]).ToString()+" ";
                    else if (j - i == 3) message+= (city[j] + city[j - 1] + city[j - 2]).ToString() + " ";
                    else  message+= (city1 + city2 + city3 + city4).ToString() + " ";
                }
                else
                {
                    if (i - j == 1) message+= (city[i]).ToString()+" ";
                    else if (i - j == 2) message+= (city[i] + city[i - 1]).ToString() + " ";
                    else if (i - j == 3) message+= (city[i] + city[i - 1] + city[i - 2]).ToString()+ " ";
                    else message+= (city1 + city2 + city3 + city4).ToString() + " ";
                }

            }
            layout[i]= message;

        }
        return layout;
    }
12
  • The problem appears to just caused by a typo. In your line if (j - i == 0) message= "0"+" ";, the message= should be message+= instead. As it is now, that code overwrites the message string rather than appending to it, which is what I believe you intended. Commented Jun 11, 2024 at 0:18
  • FYI, I also just noticed that your next line of if (j > i) should likely be else if (j > i) instead. As it's now, you have one extra value being added rather than just the likely intended 5 values. Commented Jun 11, 2024 at 0:29
  • @JohnOmielan, you are right, I updated. But it only shows half. Commented Jun 11, 2024 at 0:35
  • @JohnOmielan <string>0 3 13 25 30 </string> <string>0 10 22 27 </string> <string>0 12 17 </string> <string>0 5 </string> <string>0 </string> </ArrayOfstring> Commented Jun 11, 2024 at 0:36
  • 1
    @JohnOmielan Got it, that made a lot of sense. Thanks again and much appreciated. :) Commented Jun 11, 2024 at 2:35

1 Answer 1

1

As I stated in the comments, there are 2 problems:

  1. In your line if (j - i == 0) message= "0"+" ";, the message= should be message+=. As it is in the question, that code overwrites the message string (which is what causes 0 to always be on the left side, rather than on the diagonal, and erases the initial i values in line i for i = 0, 1, 2, 3, 4), but you intended to append to the string instead.

  2. The next line of if (j > i) should be else if (j > i) instead. As it's now, this causes one extra value being added (since instead of bypassing the j - i == 0 case, it's handled again in the else compound statement (i.e., within { and }) code below, resulting in message+= (city1 + city2 + city3 + city4).ToString() + " "; being executed since none of if (i - j == 1), if (i - j == 2) or if (i - j == 3) are true) rather than having just the intended 5 values.

You've confirmed these changes solve your problems with your comment of

it works!!! Thanks sooooo much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.