Thread: One dimensional array rotating progarm

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37

    Thumbs down One dimensional array rotating progarm

    Hi friends
    I need the one dimensional array rotating program
    that is
    the array element is
    012345
    123450
    234501
    345012
    etc

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Announcements - General Programming Boards
    Announcements - C++ Programming
    Announcements - General Programming Boards
    Since you missed them first time around.

    You need to show some effort.
    You can't just say "I need" and leave it at that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37
    please give the logic
    i am not an experts

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37
    Code:
    #include <iostream>
    
    using namespace std;
    
    int array[]={1,2,3,4},temp[10],i,l,j;
    int main()
    
    {
    
       for(j=0; j<3; j++)
    {
    
       std::cout<<array[j]<<std::endl;
    }
    cout<<std::endl;
    for(l=0;l<3; l++)
    {
        for(i=0; i<3; i++)
        {
        temp[i] = array[i];
        array[i]= array[i+1];
        std::cout<<array[i]<<std::endl;
    
        }}
    
        std::cout<<array[1]<<std::endl;
        return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First, you need to indent your code better, if you hope to "see" the logic.
    Code:
    #include <iostream>
    using namespace std;
    
    int array[] = { 1, 2, 3, 4 }, temp[10], i, l, j;
    
    int main()
    {
      for (j = 0; j < 3; j++) {
        std::cout << array[j] << std::endl;
      }
      cout << std::endl;
    
      for (l = 0; l < 3; l++) {
        for (i = 0; i < 3; i++) {
          temp[i] = array[i];
          array[i] = array[i + 1];
          std::cout << array[i] << std::endl;
        }
      }
    
      std::cout << array[1] << std::endl;
      return 0;
    }
    Second, think about some of the steps involved, and try to solve each one in turn.
    Eg.
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
      const int SIZE = 9;
      int array[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, j;
      // before
      for (j = 0; j < SIZE; j++) {
        std::cout << array[j] << " ";
      }
      cout << std::endl;
    
      for (j = 0; j < SIZE-1; j++) {
        array[j] = array[j+1];
      }
    
      // after
      for (j = 0; j < SIZE; j++) {
        std::cout << array[j] << " ";
      }
      cout << std::endl;
    
      return 0;
    }
    Can you see how this solves part of the problem (don't just look, run the code)?

    When you see it, can you then think how to get the 1 from the start to the end?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37
    But sir by this code there is no rotating the output is as follows
    123456789
    234567899
    the 9 is repeating
    the last 9 is needed to change as 1 thus rotating .thanks for the code

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thannara123
    But sir by this code there is no rotating the output is as follows
    123456789
    234567899
    the 9 is repeating
    the last 9 is needed to change as 1 thus rotating .thanks for the code
    The code example was meant to help you along, not provide you with a completely working solution. So, what can you do about this observation?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37
    Okay thanks for helping i am trying to get my need i will post soon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rotating array
    By Darkinyuasha1 in forum C++ Programming
    Replies: 7
    Last Post: 08-15-2009, 09:20 AM
  2. BIOS progarm in C needed
    By naveenaks03 in forum C Programming
    Replies: 3
    Last Post: 05-03-2008, 05:20 AM
  3. Rotating function for array
    By Fredir in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2007, 01:37 PM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM