Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I make this code to print array if the number of array is
C#
{ 1,2,3,4,5,
  6,7,8,9, 10
, 11,12,13,14,15
,16,17,18,19,20
,21,22,23,24,25}

the out put of this code is 1,2,6,3,7,11,4,6,12,16,5,9,13,17,21,10,14,18,21,15,19,23,20,24,25
Iwant to change in this my code to be the out put begging from 25 20 24 15 19 23 10 14 18 22 5 9 13 17 21 4 8 12 16 3 7 11 2 6 1
C#
        int [,] p = new int [5,5];
        int sum = 1;
        for (int i = 1; i <= 5; i++)

        {
            for (int j = 1; j <= 5; j++)
            {
                p[i, j] = sum;
                richTextBox1.Text += p[i, j].ToString();
                sum++;

            }
        }

         int C;

         int R=1;

         for (int i = 1; i <= 5; i++)
         {

             C = i;

             for (int r = 1; r <= i; r++)
             {

                 Output = Output + p[r, C];

                 C--;

             }
         }

             richTextBox2.Text += Output.ToString();

for (int i = 2; i >= 5; i++)
{
          R = i;
    for (C = 5; C >= i; C--)
    {
        Output = Output + p[R, C];

        R++;

       }
    }

  richTextBox2.Text += Output.ToString();
Posted
Updated 11-Dec-14 5:37am
v5

1 solution

C#
string Output = "";
   int[,] p = new int[5, 5];
   int sum = 1;
   for (int i = 0; i < 5; i++)
   {
       for (int j = 0; j < 5; j++)
       {
           p[i, j] = sum;
           richTextBox1.Text += p[i, j].ToString();
           sum++;

       }
   }


   for (int i = 4; i >=0; i--)
   {

       for (int C = 0; C+i<5&& C<5; C++)
       {

           Output = Output +" "+ p[i+C, 4-C];


       }
   }
   for (int i = 3; i >= 0; i--)
   {


           for (int j = 0; j < 4 &&i-j>=0; j++)
           {
               Output = Output + " " + p[j,i -j];
           }
       }

   richTextBox2.Text += Output.ToString();
   //If you have any qustion on the Solution please ask
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 11-Dec-14 12:10pm    
I can't say if your solution is right or wrong - but it should be nice to explain what you have done with a few words...
רמי קרל 11-Dec-14 13:30pm    
שלום אני רואה שאתה ישראלי תמיד נחמד לראות ישראלים שפעילים באתרים כאלה
אתה בעיקרון צודק וגם הקוד שכתבתי לא ככה הייתי כותב אותו אבל עשיתי את זה במהירות רק בכדי לתת לו פתרון מהיר שעובד
Kornfeld Eliyahu Peter 11-Dec-14 13:57pm    
"שלום שלום לרחוק ולקרוב"...
תזכור תמיד, שכאשר אתה חולק מהידע שלך אתה צריך לעשות את זה באמת...הקוד שלך יכול להיות מושלם או תפירה-של-הרגע, אבל מה שבאמת קובע את איכות התשובה שלך זה ההבנה של השואל את התשובה!!!
[no name] 11-Dec-14 12:19pm    
thank u sooooo much ,can u explain me this please because when I want to change it to print from 25 24 20 ... I don't know how can I change the counter of loop ,please
רמי קרל 11-Dec-14 12:25pm    
I did not understand your question can you explain yourself better

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900