How to fill a large array by repeatedly copying the values from a small array in C#

1 Answer

0 votes
using System;
 
public class FillLargeArrayWithValuesFromSmallArray
{
    public static void Main()
    {
        int[] smallarray = { 1, 2, 3, 4, 5 }; 
        int[] largearray = new int[30];
  
        int largelen = largearray.Length;
        int smalllen = smallarray.Length;
        
        for (int i = 0; i < largelen; i++) {
            largearray[i] = smallarray[i % smalllen];
        }
         
        for (int i = 0; i < largelen; i++) {
            Console.Write(largearray[i] + " ");
        }
    }
}

  
  
/*
run:
  
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 
  
*/

 



answered Feb 7, 2025 by avibootz
edited Feb 7, 2025 by avibootz
...