Write a C++ Program...

hasiwebmail

Member
Feb 27, 2007
501
52
0
Sri Lanka
Rotating an array

It reads a sequence of positive integers. It stores it
in an array, then it prompts the user to enter an
integer, and then rotates clockwise the content of the
array a corresponding number of positions. The contents
the array are printed out whenever it changes.


For example, if the sequence was 1,2,3,4,5 and the number 3 is entered, the array becomes 3,4,5,1,2
and if the number 1 is now entered, the array becomes 2,3,4,5,1


:shocked::shocked::shocked::shocked::shocked:

Me program eka ona?? Katada puluwan?? C++ ??
 
Last edited:

viraj_slk

Active member
  • Oct 1, 2007
    505
    35
    28
    the logical way to answer this prob.

    hey bro,
    this is not actually that hard. simply analyze the problem and think logically how to solve it.

    here i will put one method that i thought of solving this one. there may be more efficient methods as well.

    First read the value from whatever source, let it be user input or from file and store it in array.:
    we will call this array arr[];

    then count the number of values in the array (n).:
    n = count(arr[])

    then get the user input for the number of positions to shift the array clockwise (x):
    x = user input

    the we use a loop to loop through the current array (arr[]) and build a new array (new_arr[]):

    PHP:
    int p;  // the new position for each element in array
    
    for(i= 0; i < n; i++){
        p = (i + x)%n;   // remember n is the number of values in array & x is user input value
       
       new_arr[p] = arr[i]; 
    }

    and that's it. your new array with shifted values is ready. now simply loop through the new array output the values to the screen.

    the key to this problem is the method used to shift the values. which is:
    PHP:
        p = (i + x)%n;   // remember n is the number of values in array & x is user input value

    for example let's say your array is 1,2,3,4,5. and use enters 3 and you have to shift clockwise by 3.
    so n = 5 (because you have five elements in array)
    x = 3 (because user entered 3)

    and if we consider number 2 in the array, it's original position within the array is 1. remember arrays start with 0:
    so arr[1] => 2
    so in the above loop when i equals to 1, arr equals to 2
    so (i + x) => 1 + 3 => 4
    and (i + x)%n => 4%5 => 4
    so the new positions (p) of the value 2 is => 4
    therefore new_arr[p] => new_arr[4] => 2

    The problem is theoretically solved. Now you should be able to write the code yourself. It's better when you do that by yourself, otherwise you will never get the hang of these things. I know its hard at first. But you get it once you do more and more stuff like these. Trust me! :)

    Let me know if you have any trouble writing the code.
     
    Last edited:

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    Qus

    integer input 3 following output will be generated neda?

    This is not clear for me, If any one can write the coding, It'll be more helpful..


    4,5,1,2,3





    :confused::confused::confused::confused::confused:
     

    viraj_slk

    Active member
  • Oct 1, 2007
    505
    35
    28
    integer input 3 following output will be generated neda?

    This is not clear for me, If any one can write the coding, It'll be more helpful..


    4,5,1,2,3



    integer input 3 will shift each number by 3 places (not two). so the output should be like on your original question.

    3,4,5,1,2

    PHP:
    arr[1,2,3,4,5] // current array 
    new_arr[] // new array we are going to build
    int p;  // the new position for each element in array
    
    for(i= 0; i < n; i++){
        p = (i + x)%n;   // remember n is the number of values in array & x is user input value
       
       new_arr[p] = arr[i]; 
    }

    The above is the code solution to your problem. The only thing you have to do is put the right syntax's. I will post the code later but you would have done nothing on your part other than copy and paste.

    Write down your problem on paper and think of this solution, try to figure it out. I will write the code later tonite. dont have time now.
     
    Last edited:

    viraj_slk

    Active member
  • Oct 1, 2007
    505
    35
    28
    it's been quite a while since i had to write a c++ program so I cannot remember the c++ syntax's correctly. that's y i hoped u would b able to write the program urself :) as the problem is already solved. but it seems u still don't get the solution. i will try to explain a bit more as i can.

    I hope u know arrays and for loop.

    okay lets say the starting array is:
    arr[1, 2, 3, 4, 5]

    Always remember that arrays start with 0. So the 0th element in the array is 1.
    arr[0] => 1

    now if the user has input 3 as the number of positions to rotate clockwise, you have to move number 1 ,three positions to the left (which is clockwise) in the array.

    Again remember the position of number 1 is 0. so when u move left by 3 positions the new position is: 0 + 3 => 3

    So what number is currently in the 3rd positions of the current array?
    arr[3] => 4
    it is number 4 (again and again this is because the first position of the array is position 0).

    so when u move number 1 (which is in the 0th position at start) three time to the left, the new position of number one is : 0 + 3 => 3
    so number 1 should be moved to where number 4 is at right now.

    And that is what exactly the following formula does. at it is the solution to the problem.
    i => symbolizes the position of number 1 which is 0 in our case
    x => symbolizes the number of positions to rotate, which is 3 in our case
    n => symbolizes the number of values in the array. which is 5 in this case

    p=> finally.. 'p' symbolizes the new position of the value (which is going to be 3! as i explained )

    so.. the formula is:

    p = (i + x)%n;

    let put values into it (% stands for modulus operator. u shud know it, or google):

    p = (0 + 3) % 5

    p = 3 % 5 so..

    p = 3 (.. our new position for number 1 is 3)

    put the rest of the values into this formula and try urself. this couldn't be more easier now.
     
    Last edited:
    • Like
    Reactions: eranga_m

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    Tama hari giye ne..

    Mama liuwa but still not working... New array eke size eka kiyak denna wenawada? Previous sizemada? Dynamic allocate karala delete[] use karala mama kara but its good if i have the coding...
    C++ walinma one ne java/vb or wena language ekakin hari puluwan nam i can underestand. syntax correct wenna onat ne kohomahari puluwan nam try karala balanawada?


    :P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P
     

    viraj_slk

    Active member
  • Oct 1, 2007
    505
    35
    28
    Mama liuwa but still not working... New array eke size eka kiyak denna wenawada? Previous sizemada? Dynamic allocate karala delete[] use karala mama kara but its good if i have the coding...
    C++ walinma one ne java/vb or wena language ekakin hari puluwan nam i can underestand. syntax correct wenna onat ne kohomahari puluwan nam try karala balanawada?

    Here ! the code is right in front of you bro! why are u asking for more? you only have to correct the sytaxes according to C++ now. don't expect me to go through C++ syntaxs now and do it. it's ur job. :)

    again the code is:
    Code:
    arr[] = {1, 2, 3, 4, 5}  // or whatever other numbers u want. this is our original array.
    new_arr[];  // new array we are going to build
    int p;  // the new position for each element in array
    
    for(i= 0; i < n; i++){
        p = (i + x)%n;   // remember n is the number of values in array & x is user input value
       
       new_arr[p] = arr[i]; 
    }

    now 'new_arr' array contains the shifted numbers. This is the code. Use ur syntaxes to write it in whatever language u want.

    to answer ur questions:
    no u don't have to give a size to the array. but if you want, give it the same size as the original. it's going to contain the same number of elements any way.

    use the for loop to fill the new array using the formula inside it that I described to you in the previous post.


     
    Last edited:

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    Write a C++ Program......



    Write a C++ program to generate all the possible permutations of a given number.

    Ex:
    input : 123

    output
    123
    312
    231
    213
    321
    132



    :rofl::rofl::rofl::rofl::rofl::rofl::rofl::rofl::rofl::rofl::rofl:

    Kattiya mekat Try karanawada?
     

    dmudhitha

    Active member
  • Jan 9, 2009
    658
    49
    28
    //header files
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>


    int count=0;

    //func to display the sequence
    void dis(char a[],int n)
    {
    for(int jk=n-1;jk>=0;jk--)
    {
    cout<<a[jk];
    }
    cout<<"
    ";
    }

    //function to left shift last n elements
    //of the array by one position
    void shift(char a[],int n)
    {
    char s;
    s=a[n-1];
    for(int i=n-1;i>0;i--)
    a=a[i-1];
    a[0]=s;
    }

    //func to find all possible arrangements
    void per(char a[],int m,int n)
    {
    for(int i=0;i<m;i++)
    {
    if(m>1)
    {
    shift(a,m);
    per(a,m-1,n); //genetrating the tree
    }
    else
    {
    dis(a,n); //displaying the leaf nodes of the tree
    count++;
    }
    }
    }


    void main()
    {
    // char *a;
    int n;
    clrscr();

    cout<<"Enter a no : ";
    cin>>n;

    char *a = new (char[n]);

    for(int i=0;i<n;i++)
    a = n-i+48;

    // dis(a,n);

    cout<<"Possible permutations are :
    ";
    per(a,n,n);
    cout<<"Total No Of Permutations Is "<<count;
    getch();
    }

    /**************************************/
    OUTPUT
    ------

    Enter a no : 3
    Possible permutations are :
    213
    231
    321
    312
    132
    123
    Total No Of Permutations Is 6
    /**************************************/
     
    Last edited: