Thread: Array shifting help

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    19

    Array shifting help

    Can someone help me with array shifting. I am trying to shift an array a number of times that someone enters.

    Example: The array is abcdef and I enter 1 to shift the array once. The output is bcdefa. If I enter 2 then the output is cdefab.

    Here is my code so far. Can someone help me find out what I'm doing wrong. Thank you.

    Code:
    #include <stdio.h> 
    char shift( int yy, char loop[7]);
    int main() 
    {
    char loop[7] = "abcdef";
    char arrloop[7];
    int y;
    printf("How many times do you want to shift this string?\n");
    scanf("%d", y);
    
    shift( y, char arrloop[7]);
    
    printf("%s\n", loop);
    
    }
    
    {char shift(int yy, char loop[7]);
    while( yy != 0)
    {char x;
    x = loop[5];
    loop[5] = loop[4];
    loop[4] = loop[3];
    loop[3] = loop[2];
    loop[2] = loop[1];
    loop[1] = loop[0];
    loop[0] = x;
    yy--;}
    
    printf("%s", loop);
    getchar();
    getchar();
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest you indent your code, and use the code bbcode tags.
    I believe that your code blocks are somewhat misaligned.

    btw, I think your idea is to keep shifting the places by one until the number of shifts is satisfied.
    There should be a faster (and cleaner) way of doing it, by calculating the final place of each element, given the number of shifts.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%d", y);
    > shift( y, char arrloop[7]);

    Should be
    scanf("%d", &y);
    shift( y, loop );

    > &#123;char shift(int yy, char loop[7]);
    It's a bit mis-placed
    Try
    char shift(int yy, char loop[7]) &#123;
    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.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    Thanks guys, you are a big help to me. I've changed a few things that helped, but now it prints out the array as many times as the scanf input. I just want it to print one time after the shifting takes place. How do I go about doing that. I even pulled the printf out of the function into the main and it still prints as many times as the scanf input. Here is what I have:

    Code:
    #include <stdio.h> 
    char shift( int y, char loop[7]);
    int main() 
    {
        char loop[7] = "abcdef";
        int y;
        printf("How many times do you want to shift this string?\n");
        scanf("%d", &y);
    
           shift(y,  loop);
    
        printf("%s\n", loop);
        
    getchar();
    getchar();
    return 0;
    }
    
    char shift(int y, char loop[7]) {
    
    while( y != 0)
    {
        char x;
        x = loop[5];
        loop[5] = loop[4];
        loop[4] = loop[3];
        loop[3] = loop[2];
        loop[2] = loop[1];
        loop[1] = loop[0];
        loop[0] = x;
        y--;
    }
    Thanks again.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Store the length of the string. That way, the function will work with any size of array. Here's an example:

    Code:
    char *
     shift(char str[], int times)
    {
     int end = strlen(str)-1;
    
     int i, loops;
    
     char value, temp;
    
        for(loops = 0; loops < times; ++loops)
       {
        value = str[0];
    
           for(i = end; i >= 0; --i)
          {
           temp = str[i];
           str[i] = value;
           value = temp;
          }
       }
    
     return str;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> but now it prints out the array as many times as the scanf input.

    Recompile. The code you posted should only print once.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    Okay, I thik I'm getting closer. I have narrowed it down to one error I think. It says line 33 parse error at end of input. Strange that it has only 32 lines of code.

    Sebastiani I tried fooling around with what you posted but I got more errors and crashes than I can shake a stick-of-ram at. I'm still new at this and I think you were using pointers. Lord knows how bad I can mess pointers up. If my current code method does not work for me then I will try your method again.

    Code:
    #include <stdio.h> 
    char shift( int y, char loop[7]);
    int main() 
    {
        char loop[7] = "abcdef";
        int y;
        printf("How many times do you want to shift this string?\n");
        scanf("%d", &y);
    
           shift(y,  loop);
    
        printf("%s\n", loop);
        
    getchar();
    getchar();
    return 0;
    }
    
    char shift(int y, char loop[7])
     {
        while( y != 0)
        {
        char x;
        x = loop[5];
        loop[5] = loop[4];
        loop[4] = loop[3];
        loop[3] = loop[2];
        loop[2] = loop[1];
        loop[1] = loop[0];
        loop[0] = x;
        y--;
    }
      **// I get a "parse error at end of input" here for some reason.//**

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    I also get a " [Warning] In function `char shift(int, char*)':"
    Is this what you were talking about with using the pointers? I'm a bit confused.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char shift(int y, char loop[7])
     {
        while( y != 0)
         {
        char x;
        x = loop[5];
        loop[5] = loop[4];
        loop[4] = loop[3];
        loop[3] = loop[2];
        loop[2] = loop[1];
        loop[1] = loop[0];
        loop[0] = x;
        y--;
     }
    See the problem?

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    hehe, what can I say? Thank you very much. But I still get the error even after I recompile this:

    Code:
    #include <stdio.h> 
    char shift( int y, char loop[7]);
    int main() 
    {
        char loop[7] = "abcdef";
        int y;
        printf("How many times do you want to shift this string?\n");
        scanf("%d", &y);
    
           shift(y,  loop);
    
        printf("%s\n", loop);
        
    getchar();
    getchar();
    return 0;
    }
    
    char shift(int y, char loop[7])
     {
        while( y != 0)
        {
        char x;
        x = loop[5];
        loop[5] = loop[4];
        loop[4] = loop[3];
        loop[3] = loop[2];
        loop[2] = loop[1];
        loop[1] = loop[0];
        loop[0] = x;
        y--;
        }
    }**// I get a "parse error at end of input" below this line that does not exist for some reason.//**

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    161
    That code compiles fine for me. The only change I made was to make the shift function return void instead of char (since you're not returning anything from it)

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    Thanks for the help so far. Okay here is what I've got. Does anyone else have any problems compiling and running this program?

    Code:
    #include <stdio.h> 
    void shift( int y, char loop[7]);
    int main() 
    {
        char loop[7] = "abcdef";
        int y;
        printf("How many times do you want to shift this string?\n");
        scanf("%d", y);
    
           shift(y,  loop);
    
        printf("%s\n", loop);
        
    getchar();
    getchar();
    return 0;
    }
    
    void shift(int y, char loop[7])
     {
        while( y != 0)
        {
        char x;
        x = loop[5];
        loop[5] = loop[4];
        loop[4] = loop[3];
        loop[3] = loop[2];
        loop[2] = loop[1];
        loop[1] = loop[0];
        loop[0] = x;
        y--;
        }
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%d", y);
    This started off wrong,
    Sometime later, you got it right
    and now it's wrong again
    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.

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    Okay, I've got it now. Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Replies: 4
    Last Post: 05-13-2003, 04:54 PM