Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have an array and i want output as below. Need this in c# implementation.

input parameter:
array = {1, 2, 3, 4, 5, 6, 7}
int indexclip = 3, int lineclip = 5

output:
123
456
712
345
678

I am trying and attached code doesn't work, need a solution.

Thank you

What I have tried:

public static void PrintLine(int[] a, int b, int c)
        {
            int startindex = 0, endindex = 0, line = 0;
            for (int i = 0; i < c; i++)
            {
                if (line <= c)
                {
                    for (int j = startindex; j < b; j++)
                    {
                        Console.Write(string.Join(" ", a[j]));
                    }
                    startindex = b;

                    Console.WriteLine();
                    line++;
                    for (int j = startindex; j < a.Length; j++)
                    {
                        Console.Write(string.Join(" ", a[j]));
                    }
                    endindex = startindex - (b - 1);
                    for (int j = 0; j < endindex; j++)
                    {
                        Console.Write(string.Join(" ", a[j]));
                    }
                    startindex = endindex;
                    Console.WriteLine();
                    line++;
                }
            }
        }
Posted
Updated 27-Jul-21 7:50am
Comments
BillWoodruff 29-Jul-21 6:40am    
678 makes no sense.

712 suggest wrapping around

why not 234, 567, 671 ?

Assuming that the "8" in your solution is a typo (because it only appears on teh final line and isn't in the input all you need is a modulus operator:
C#
int[] input = { 1, 2, 3, 4, 5, 6, 7 };
int index = 0;
int lineLength = 3;
int lineCount = 5;
for (int y = 0; y < lineCount; y++)
    {
    for (int x = 0; x < lineLength; x++)
        {
        Console.Write(input[index++]);
        index %= input.Length;
        }
    Console.WriteLine();
    }
 
Share this answer
 
Comments
Ajeet S 2021 29-Jul-21 7:10am    
is there a way to do this using LINQ lamdbda expressions
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7 };
int indexClip = 3;
int lineClip = 5;
Queue<int> q = new Queue<int>();

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

   for ( int j = 1; j <= indexClip; j++ ) {

      if ( q.Count == 0 ) { q = new Queue<int>( array ); }

      Console.Write( $"{q.Dequeue()}" );

   }  // end for.

   Console.WriteLine();

}  // end for.
 
Share this answer
 

If you are familiar with Linq you could use something like this.


C#
int[] array = new int[] { 0, 1, 2, 3, 4, 5, 6,7 };
int index = 0;
int skipCount = 3;
while (index < array.Length)
  {
   var batch = array.Skip(index).Take(skipCount).ToArray();
   if (batch.Length != skipCount) break;
   foreach(var n in batch)
   {
    Console.Write(n);
   }
    Console.WriteLine();
    index += skipCount;
  }

On reflection, this solution is over-engineered for the task at hand. It is designed to split an array into batches so that they may be processed asynchronously by different threads.

 
Share this answer
 
v3
Comments
Ajeet S 2021 27-Jul-21 12:37pm    
above code output:
123
456
78

even my code too does this. but how to skip iteration after it ends the length.
it should continue 781 and then 234 ...
George Swan 27-Jul-21 13:02pm    
I have modified the code so that only batches of size skipCount are output. But I think Griff's solution is 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