Print all possible rotations of a given Array

Here given code implementation process.

// C Program
// Print all possible rotations of a given Array
#include <stdio.h>

// Print all rotations in array
void printRoation(int arr[],int size)
{
    // Loop controlling variables
    int i = 0;
    int j = 0;
    int k = 0;

    // Outer loop
    for ( i = 0; i < size; ++i)
    {
        k = size - i;

        // Inner loop executes n times
        for ( j = 0; j < size; ++j)
        {  
            if(k >= size)
            {
                // When k is exceed limit of last element then start of first element
                k = 0;
            }    
            // Display element
            printf("  %d",arr[k]);
            
            k++;

           
        }
        printf("\n");
    }

    printf("\n");


}

int main()
{
    // Define array of integer elements
    int arr1[] = {1,  3,  4, 6,  8, 15 };
   
    int arr2[] = {6,  8,  9, 5,  3, 3 , 2};

    //Get the size
    int size = sizeof(arr1)/sizeof(arr1[0]);

    //Test Case 1
    printRoation(arr1,size);

    //Get the size
    size = sizeof(arr2)/sizeof(arr2[0]);
    
    //Test Case 2
    printRoation(arr2,size);
  return 0;
}

Output

  1  3  4  6  8  15
  15  1  3  4  6  8
  8  15  1  3  4  6
  6  8  15  1  3  4
  4  6  8  15  1  3
  3  4  6  8  15  1

  6  8  9  5  3  3  2
  2  6  8  9  5  3  3
  3  2  6  8  9  5  3
  3  3  2  6  8  9  5
  5  3  3  2  6  8  9
  9  5  3  3  2  6  8
  8  9  5  3  3  2  6
/*
    Java program 
    Print all possible rotations of a given Array 
*/

public class MyMatrix
{
   // Print all rotations in array
    public void printRoation(int[] arr, int size)
    {
        // Loop controlling variables
        int i = 0;
        int j = 0;
        int k = 0;
        // Outer loop
        for (i = 0; i < size; ++i)
        {
            k = size - i;
            // Inner loop executes n times
            for (j = 0; j < size; ++j)
            {
                if (k >= size)
                {
                    // When k is exceed limit of last element then start of first element
                    k = 0;
                }
                // Display element
                System.out.print("   " + arr[k] );
                k++;
            }
            System.out.print("\n");
        }
        System.out.print("\n");
    }
    public static void main(String[] args)
    {
        MyMatrix obj = new MyMatrix();
        // Define array of integer elements
        int[] arr1  = { 1 , 3 , 4 , 6 , 8 , 15 };
        int[] arr2  = { 6 , 8 , 9 , 5 , 3 , 3 , 2 };
        //Get the size
        int size = arr1.length;
        //Test Case 1
        obj.printRoation(arr1, size);
        //Get the size
        size = arr2.length;
        //Test Case 2
        obj.printRoation(arr2, size);
    }
}

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6
// Include header file
#include <iostream>
using namespace std;

/*
    C++ program 
    Print all possible rotations of a given Array
*/

class MyMatrix
{
    public:
        //  Print all rotations in array
        void printRoation(int arr[], int size)
        {
            //  Loop controlling variables
            int i = 0;
            int j = 0;
            int k = 0;
            //  Outer loop
            for (i = 0; i < size; ++i)
            {
                k = size - i;
                //  Inner loop executes n times
                for (j = 0; j < size; ++j)
                {
                    if (k >= size)
                    {
                        //  When k is exceed limit of last element then start of first element
                        k = 0;
                    }
                    //  Display element
                    cout << "   " << arr[k];
                    k++;
                }
                cout << "\n";
            }
            cout << "\n";
        }
};
int main()
{
    MyMatrix obj = MyMatrix();
    //  Define array of integer elements
    int arr1[] = {
        1 , 3 , 4 , 6 , 8 , 15
    };
    int arr2[] = {
        6 , 8 , 9 , 5 , 3 , 3 , 2
    };
    // Get the size
    int size = sizeof(arr1) / sizeof(arr1[0]);
    // Test Case 1
    obj.printRoation(arr1, size);
    // Get the size
    size = sizeof(arr2) / sizeof(arr2[0]);
    // Test Case 2
    obj.printRoation(arr2, size);
    return 0;
}

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6
// Include namespace system
using System;
/*
    C# program 
    Print all possible rotations of a given Array
*/
public class MyMatrix
{
    //  Print all rotations in array
    public void printRoation(int[] arr, int size)
    {
        //  Loop controlling variables
        int i = 0;
        int j = 0;
        int k = 0;
        //  Outer loop
        for (i = 0; i < size; ++i)
        {
            k = size - i;
            //  Inner loop executes n times
            for (j = 0; j < size; ++j)
            {
                if (k >= size)
                {
                    //  When k is exceed limit of last element then start of first element
                    k = 0;
                }
                //  Display element
                Console.Write("   " + arr[k]);
                k++;
            }
            Console.Write("\n");
        }
        Console.Write("\n");
    }
    public static void Main(String[] args)
    {
        MyMatrix obj = new MyMatrix();
        //  Define array of integer elements
        int[] arr1 = {
            1 , 3 , 4 , 6 , 8 , 15
        };
        int[] arr2 = {
            6 , 8 , 9 , 5 , 3 , 3 , 2
        };
        // Get the size
        int size = arr1.Length;
        // Test Case 1
        obj.printRoation(arr1, size);
        // Get the size
        size = arr2.Length;
        // Test Case 2
        obj.printRoation(arr2, size);
    }
}

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6
<?php
/*
    Php program 
    Print all possible rotations of a given Array
*/
class MyMatrix
{
    //  Print all rotations in array
    public  function printRoation( & $arr, $size)
    {
        //  Loop controlling variables
        $i = 0;
        $j = 0;
        $k = 0;
        //  Outer loop
        for ($i = 0; $i < $size; ++$i)
        {
            $k = $size - $i;
            //  Inner loop executes n times
            for ($j = 0; $j < $size; ++$j)
            {
                if ($k >= $size)
                {
                    //  When k is exceed limit of last element then start of first element
                    $k = 0;
                }
                //  Display element
                echo "   ". $arr[$k];
                $k++;
            }
            echo "\n";
        }
        echo "\n";
    }
}

function main()
{
    $obj = new MyMatrix();
    //  Define array of integer elements
    $arr1 = array(1, 3, 4, 6, 8, 15);
    $arr2 = array(6, 8, 9, 5, 3, 3, 2);
    // Get the size
    $size = count($arr1);
    // Test Case 1
    $obj->printRoation($arr1, $size);
    // Get the size
    $size = count($arr2);
    // Test Case 2
    $obj->printRoation($arr2, $size);
}
main();

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6
/*
    Node Js program 
    Print all possible rotations of a given Array
*/
class MyMatrix
{
    //  Print all rotations in array
    printRoation(arr, size)
    {
        //  Loop controlling variables
        var i = 0;
        var j = 0;
        var k = 0;
        //  Outer loop
        for (i = 0; i < size; ++i)
        {
            k = size - i;
            //  Inner loop executes n times
            for (j = 0; j < size; ++j)
            {
                if (k >= size)
                {
                    //  When k is exceed limit of last element then start of first element
                    k = 0;
                }
                //  Display element
                process.stdout.write("   " + arr[k]);
                k++;
            }
            process.stdout.write("\n");
        }
        process.stdout.write("\n");
    }
}

function main()
{
    var obj = new MyMatrix();
    //  Define array of integer elements
    var arr1 = [1, 3, 4, 6, 8, 15];
    var arr2 = [6, 8, 9, 5, 3, 3, 2];
    // Get the size
    var size = arr1.length;
    // Test Case 1
    obj.printRoation(arr1, size);
    // Get the size
    size = arr2.length;
    // Test Case 2
    obj.printRoation(arr2, size);
}
main();

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6
#  Python 3 program 
#  Print all possible rotations of a given Array

class MyMatrix :
    #   Print all rotations in array
    def printRoation(self, arr, size) :
        #   Loop controlling variables
        i = 0
        j = 0
        k = 0
        #   Outer loop
        while (i < size) :
            k = size - i
            #   Inner loop executes n times
            j = 0
            while (j < size) :
                if (k >= size) :
                    #   When k is exceed limit of last element then start of first element
                    k = 0
                
                #   Display element
                print("   ", arr[k], end = "")
                k += 1
                j += 1
            
            print(end = "\n")
            i += 1
        
        print(end = "\n")
    

def main() :
    obj = MyMatrix()
    #   Define array of integer elements
    arr1 = [1, 3, 4, 6, 8, 15]
    arr2 = [6, 8, 9, 5, 3, 3, 2]
    #  Get the size
    size = len(arr1)
    #  Test Case 1
    obj.printRoation(arr1, size)
    #  Get the size
    size = len(arr2)
    #  Test Case 2
    obj.printRoation(arr2, size)

if __name__ == "__main__": main()

Output

    1    3    4    6    8    15
    15    1    3    4    6    8
    8    15    1    3    4    6
    6    8    15    1    3    4
    4    6    8    15    1    3
    3    4    6    8    15    1

    6    8    9    5    3    3    2
    2    6    8    9    5    3    3
    3    2    6    8    9    5    3
    3    3    2    6    8    9    5
    5    3    3    2    6    8    9
    9    5    3    3    2    6    8
    8    9    5    3    3    2    6
#   Ruby program 
#   Print all possible rotations of a given Array

class MyMatrix 
    #   Print all rotations in array
    def printRoation(arr, size) 
        #   Loop controlling variables
        i = 0
        j = 0
        k = 0
        #   Outer loop
        while (i < size) 
            k = size - i
            #   Inner loop executes n times
            j = 0
            while (j < size) 
                if (k >= size) 
                    #   When k is exceed limit of last element then start of first element
                    k = 0
                end

                #   Display element
                print("   ", arr[k])
                k += 1
                j += 1
            end

            print("\n")
            i += 1
        end

        print("\n")
    end

end

def main() 
    obj = MyMatrix.new()
    #   Define array of integer elements
    arr1 = [1, 3, 4, 6, 8, 15]
    arr2 = [6, 8, 9, 5, 3, 3, 2]
    #  Get the size
    size = arr1.length
    #  Test Case 1
    obj.printRoation(arr1, size)
    #  Get the size
    size = arr2.length
    #  Test Case 2
    obj.printRoation(arr2, size)
end

main()

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6

/*
    Scala program 
    Print all possible rotations of a given Array
*/
class MyMatrix
{
    //   Print all rotations in array
    def printRoation(arr: Array[Int], size: Int): Unit = {
        // Loop controlling variables
        var i: Int = 0;
        var j: Int = 0;
        var k: Int = 0;
        // Outer loop
        while (i < size)
        {
            k = size - i;
            // Inner loop executes n times
            j = 0;
            while (j < size)
            {
                if (k >= size)
                {
                    // When k is exceed limit of last element then start of first element
                    k = 0;
                }
                // Display element
                print("   " + arr(k));
                k += 1;
                j += 1;
            }
            print("\n");
            i += 1;
        }
        print("\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var obj: MyMatrix = new MyMatrix();
        //   Define array of integer elements
        var arr1: Array[Int] = Array(1, 3, 4, 6, 8, 15);
        var arr2: Array[Int] = Array(6, 8, 9, 5, 3, 3, 2);
        //  Get the size
        var size: Int = arr1.length;
        //  Test Case 1
        obj.printRoation(arr1, size);
        //  Get the size
        size = arr2.length;
        //  Test Case 2
        obj.printRoation(arr2, size);
    }
}

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6
/*
    Swift 4 program 
    Print all possible rotations of a given Arra
*/
class MyMatrix
{
    //   Print all rotations in array
    func printRoation(_ arr: [Int], _ size: Int)
    {
        //   Loop controlling variables
        var i: Int = 0;
        var j: Int = 0;
        var k: Int = 0;
        //   Outer loop
        while (i < size)
        {
            k = size - i;
            //   Inner loop executes n times
            j = 0;
            while (j < size)
            {
                if (k >= size)
                {
                    //   When k is exceed limit of last element then start of first element
                    k = 0;
                }
                //   Display element
                print("   ", arr[k], terminator: "");
                k += 1;
                j += 1;
            }
            print(terminator: "\n");
            i += 1;
        }
        print(terminator: "\n");
    }
}
func main()
{
    let obj: MyMatrix = MyMatrix();
    //   Define array of integer elements
    let arr1: [Int] = [1, 3, 4, 6, 8, 15];
    let arr2: [Int] = [6, 8, 9, 5, 3, 3, 2];
    //  Get the size
    var size: Int = arr1.count;
    //  Test Case 1
    obj.printRoation(arr1, size);
    //  Get the size
    size = arr2.count;
    //  Test Case 2
    obj.printRoation(arr2, size);
}
main();

Output

    1    3    4    6    8    15
    15    1    3    4    6    8
    8    15    1    3    4    6
    6    8    15    1    3    4
    4    6    8    15    1    3
    3    4    6    8    15    1

    6    8    9    5    3    3    2
    2    6    8    9    5    3    3
    3    2    6    8    9    5    3
    3    3    2    6    8    9    5
    5    3    3    2    6    8    9
    9    5    3    3    2    6    8
    8    9    5    3    3    2    6
/*
    Kotlin program 
    Print all possible rotations of a given Arra 
*/
class MyMatrix
{
    //   Print all rotations in array
    fun printRoation(arr: Array<Int>, size: Int): Unit
    {
        //   Loop controlling variables
        var i: Int = 0;
        var j: Int;
        var k: Int;
        //   Outer loop
        while (i<size)
        {
            k = size - i;
            //   Inner loop executes n times
            j = 0;
            while (j<size)
            {
                if (k>= size)
                {
                    //   When k is exceed limit of last element then start of first element
                    k = 0;
                }
                //   Display element
                print("   " + arr[k]);
                k += 1;
                j += 1;
            }
            print("\n");
            i += 1;
        }
        print("\n");
    }
}
fun main(args: Array<String>): Unit
{
    var obj: MyMatrix = MyMatrix();
    //   Define array of integer elements
    var arr1: Array<Int> = arrayOf(1, 3, 4, 6, 8, 15);
    var arr2: Array<Int> = arrayOf(6, 8, 9, 5, 3, 3, 2);
    //  Get the size
    var size: Int = arr1.count();
    //  Test Case 1
    obj.printRoation(arr1, size);
    //  Get the size
    size = arr2.count();
    //  Test Case 2
    obj.printRoation(arr2, size);
}

Output

   1   3   4   6   8   15
   15   1   3   4   6   8
   8   15   1   3   4   6
   6   8   15   1   3   4
   4   6   8   15   1   3
   3   4   6   8   15   1

   6   8   9   5   3   3   2
   2   6   8   9   5   3   3
   3   2   6   8   9   5   3
   3   3   2   6   8   9   5
   5   3   3   2   6   8   9
   9   5   3   3   2   6   8
   8   9   5   3   3   2   6


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved