446,406 Members | 1,029 Online
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

# C By Dissection chapter 8 exercise 6

 P: 17 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. Jun 30 '06 #1
12 Replies

 Expert Mod 5K+ P: 8,916 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. Jun 30 '06 #2

 P: 17 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 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. Jul 1 '06 #3

 P: 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; } Jul 3 '06 #4

 Expert Mod 5K+ P: 8,916 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. Jul 3 '06 #5

 P: 4 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 Jul 4 '06 #6

 P: 17 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. Jul 5 '06 #7

 P: 2 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. Jul 25 '07 #8

 P: 2 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?? Aug 2 '07 #9

 P: 1 #include 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; } Apr 9 '09 #10

 100+ P: 542 or for brevity.... Expand|Select|Wrap|Line Numbers 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; } Apr 10 '09 #11

 100+ P: 542 ......or to use a std library function next_permutation In this case header files and are required. Expand|Select|Wrap|Line Numbers int main() {cout<<"The 5! permutations of A,B,C,D,E \n\n";   vector vector1(5);   cout<<"The vector holds: ";   for (int i = 0; i < 5; ++i)      { vector1[i] = char('A'+i);      cout<

 P: 2 hi just wanna ask kung taga pup ka ba??? magkaklase cguro tayo Apr 12 '09 #13