C By Dissection chapter 8 exercise 6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rwise5
    New Member
    • Jun 2006
    • 17

    #1

    C By Dissection chapter 8 exercise 6

    Hello, I was wondering if someone could help me with the following?

    Write a function that shifts the stored value of five character variables in a circular fashion. Your function should work in the following way. Suppose that c1, c2,...,c5 are variables of type char, and suppose that the values of these variables are 'A' , 'B' , ....' E', respectively. The function call shift (&c1, &c2, &c3, &c4, &c5) should cause the variables c1, c2,..., c5 to have the values 'B', 'C', 'D', 'E', 'A', respectively. Your function definition should start as follows:

    void shift(char *p1, char *p2, char *p3, char *p4, char *p5)
    {
    ....
    }

    Test your function by calling it five times and printing out, in turn, BCDEA, CDEAB, DEABC, EABCD, and ABCDE.

    ----------------------------------------------------------------------------------------------------------------

    I was wondering why the exercise calls for starting the function definition as above?

    Any help would be greatly appreciated.
    Thank you in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Erm because there are 5 character variables to be changed so 5 input parameters are required and since the function has to change the value of the variables they are passed they need to be passed by reference (i.e. char *) rather than by value (char).

    If you are having trouble getting you head round that problem then try this simpler related problem.

    Write a function that swops the stored value of 2 character variables. Suppose that c1 and c2 are variables of type char, and suppose that the values of these variables are 'A' and 'B' respectively. The function call swop(&c1, &c2) should cause the variables c1 and c2 to have the values 'B' and 'A' respectively. Your function definition should start as follows:

    void swop(char *p1, char *p2)
    {
    ....
    }

    Test your function by calling it twice and printing out, in turn, BA and AB.

    Comment

    • rwise5
      New Member
      • Jun 2006
      • 17

      #3
      I am sorry I am not following you. I have my the program book, but there is no examples that talk about switching. I have tried to figure this out but.....???? Here is what I have already.

      #include <stdio.h>

      void shift(char *p1, char *p2, char *p3, char *p4, char *p5)
      {
      char c1 = 'A', c2 = 'B', c3 = 'C', c4 = 'D', c5 = 'E';
      int tmp = 0;
      }

      I have prematurely tried to compile this and come back with errors.

      I am hoping that someone can put me on the right track.

      Comment

      • robrobrob
        New Member
        • Jul 2006
        • 4

        #4
        void shift(char *p1, char *p2, char *p3, char *p4, char *p5)
        {
        char temp;
        temp=*p1; *p1 = *p2; *p2 = *p3; *p3= *p4; *p4=*p5; *p5 = temp;
        }

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          robrobrob has basically given you the answer rwise5, however I would have put all those statements onto separate lines for clarity of code.

          Make sure you understand what this function does and how it works.

          Comment

          • robrobrob
            New Member
            • Jul 2006
            • 4

            #6
            I am sorry for giing out the answer. As I am now reading this thread more carefully, it is clear that Banfa was trying to help rwise think it through and get there his/herself. Of course this would have been better for rwise.

            I put all the lines on the same line just because I thought it would read easier in the forum. In my code document, I a agree with Banfa, and it would be one statement per line.

            Now that you know how to swap values with a function rwise, here is another problem if you would like to have another opportunity to figure some stuff out.

            Rewrite it to take the data form an array, and place the elements in alphabetical order. THis will help solidify your understanding of pointers and arrays as used with functions.

            rob

            Comment

            • rwise5
              New Member
              • Jun 2006
              • 17

              #7
              Thank you, Robrobrob and Banfa. I figure out that I was trying to compile a call function. When I wrote the main program and then called the function it worked. I had actually figured it out before reading the reply from Rob. Thanks again. Oh, I wish I had time to do the other exercise that Rob suggested, but I got to move on with what current assignments I have now. I was just looking for a new suggestions to open my mind. Thanks again.

              Comment

              • Mdewman77
                New Member
                • Jul 2007
                • 2

                #8
                Hey there,

                Seems we have another Grantham student! I'm currently working on the exact same problem in the same book. Have you had any luck with the code compiling? The instructor says as long as you attempted it, you will pass. I just wanna get this class over with! lol.

                Comment

                • Mdewman77
                  New Member
                  • Jul 2007
                  • 2

                  #9
                  Hey guys,

                  I am currently working on the same problem. I believe I have the call function done correctly, but not sure how to call it five times. Any help??

                  Comment

                  • joiner88
                    New Member
                    • Apr 2009
                    • 1

                    #10
                    solution

                    #include <stdio.h>

                    void shift(char *p1, char *p2, char *p3, char *p4, char *p5);

                    int main(void){

                    char c1='A', c2='B', c3='C', c4='D', c5='E';

                    printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);
                    shift(&c1, &c2, &c3, &c4, &c5);
                    printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);

                    shift(&c1, &c2, &c3, &c4, &c5);
                    printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);

                    shift(&c1, &c2, &c3, &c4, &c5);
                    printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);

                    shift(&c1, &c2, &c3, &c4, &c5);
                    printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);

                    shift(&c1, &c2, &c3, &c4, &c5);
                    printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);

                    return 0;
                    }

                    void shift( char *p1, char *p2, char *p3, char *p4, char *p5)
                    {
                    char tmp;

                    tmp = *p1;
                    *p1 = *p2;
                    *p2 = *p3;
                    *p3 = *p4;
                    *p4 = *p5;
                    *p5 = tmp;
                    }

                    Comment

                    • whodgson
                      Contributor
                      • Jan 2007
                      • 542

                      #11
                      or for brevity....
                      Code:
                      int main(void){
                      
                      char c1='A', c2='B', c3='C', c4='D', c5='E';
                      for(int i=0;i<5;i++)
                      {
                       printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);
                       shift(&c1, &c2, &c3, &c4, &c5);
                      }
                      
                      char q;
                      scanf("&q");
                      return 0;
                      }

                      Comment

                      • whodgson
                        Contributor
                        • Jan 2007
                        • 542

                        #12
                        ......or to use a std library function next_permutatio n
                        In this case header files <algorithm> and <vector> are required.
                        Code:
                        int main()
                        {cout<<"The 5! permutations of A,B,C,D,E \n\n";
                          vector<int> vector1(5);
                          cout<<"The vector holds: ";
                          for (int i = 0; i < 5; ++i) 
                            { vector1[i] = char('A'+i);
                             cout<<char('A'+i)<<" ";
                            } 
                          cout<<"\nPermutations are: \n";
                          for(int i=0;i<120;i++)//prinst the 120 (5!) permutations of ABCDE
                          {next_permutation(vector1.begin(), vector1.end());//call next_p()
                          //if(i<10)cout<<"0"<<i<<" .  ";//line up the letters
                          //else cout<<i<<" .  ";
                          if(i==32||i==63||i==89||i==95||i==119)//these are the 5 sought by the proff
                          {cout << char(vector1[0])<<" " ;//print 1st letter in each permutation
                          cout << char(vector1[1])<<" " ;//print 2nd
                          cout << char(vector1[2])<<" " ;//print 3rd
                          cout << char(vector1[3])<<" " ;//print 4th
                          cout << char(vector1[4])<<" " ;//print 5th 
                          cout<<endl;}
                          //if(i==40 || i==80)system("pause");
                          }
                        cout<<"\n\n";  
                        system("pause");
                          return 0;
                        }

                        Comment

                        • minuns
                          New Member
                          • Apr 2009
                          • 2

                          #13
                          pup ka ba?

                          hi just wanna ask kung taga pup ka ba??? magkaklase cguro tayo

                          Comment

                          Working...