By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,029 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
12 Replies


Banfa
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 <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.
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

Banfa
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 <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;
}
Apr 9 '09 #10

100+
P: 542
or for brevity....
Expand|Select|Wrap|Line Numbers
  1. int main(void){
  2.  
  3. char c1='A', c2='B', c3='C', c4='D', c5='E';
  4. for(int i=0;i<5;i++)
  5. {
  6.  printf("%c %c %c %c %c\n", c1, c2, c3, c4, c5);
  7.  shift(&c1, &c2, &c3, &c4, &c5);
  8. }
  9.  
  10. char q;
  11. scanf("&q");
  12. return 0;
  13. }
Apr 10 '09 #11

100+
P: 542
......or to use a std library function next_permutation
In this case header files <algorithm> and <vector> are required.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {cout<<"The 5! permutations of A,B,C,D,E \n\n";
  3.   vector<int> vector1(5);
  4.   cout<<"The vector holds: ";
  5.   for (int i = 0; i < 5; ++i) 
  6.     { vector1[i] = char('A'+i);
  7.      cout<<char('A'+i)<<" ";
  8.     } 
  9.   cout<<"\nPermutations are: \n";
  10.   for(int i=0;i<120;i++)//prinst the 120 (5!) permutations of ABCDE
  11.   {next_permutation(vector1.begin(), vector1.end());//call next_p()
  12.   //if(i<10)cout<<"0"<<i<<" .  ";//line up the letters
  13.   //else cout<<i<<" .  ";
  14.   if(i==32||i==63||i==89||i==95||i==119)//these are the 5 sought by the proff
  15.   {cout << char(vector1[0])<<" " ;//print 1st letter in each permutation
  16.   cout << char(vector1[1])<<" " ;//print 2nd
  17.   cout << char(vector1[2])<<" " ;//print 3rd
  18.   cout << char(vector1[3])<<" " ;//print 4th
  19.   cout << char(vector1[4])<<" " ;//print 5th 
  20.   cout<<endl;}
  21.   //if(i==40 || i==80)system("pause");
  22.   }
  23. cout<<"\n\n";  
  24. system("pause");
  25.   return 0;
  26. }
Apr 10 '09 #12

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

Post your reply

Sign in to post your reply or Sign up for a free account.