473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C By Dissection chapter 8 exercise 6

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 6231
Banfa
9,065 Expert Mod 8TB
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
rwise5
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
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
9,065 Expert Mod 8TB
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
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
rwise5
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
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
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
#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
whodgson
542 512MB
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
whodgson
542 512MB
......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
minuns
2
hi just wanna ask kung taga pup ka ba??? magkaklase cguro tayo
Apr 12 '09 #13

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

Similar topics

30
by: Ron_Adam | last post by:
I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a...
35
by: sathya | last post by:
Q. Write a program to print all input lines that are longer than 80 characters. Code taken from http://users.powernet.co.uk/eton/kandr2/krx117.html ...
18
by: Simon Morgan | last post by:
Hi, Does anybody have a solution or a hint for Exercise 5 of Chapter 15 of K.N. King's book C Programming: A Modern Approach? I spent all of yesterday evening staring at it and drooling. The...
3
by: rwise5 | last post by:
Hello, I would like to see if anyone can help me in modifying the rock paper scissors program that is one of the exercises in this book. I am thinking that I need to somehow get the information from...
3
by: jszczepankiewicz | last post by:
Witam, mam nastepujacy problem: XSLT 2.0, Hi, i've got following problem with xslt 2: my xml doc looks something linke: <manual>
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
263
by: Malcolm McLean | last post by:
The webpages for my new book are now up and running. The book, Basic Algorithms, describes many of the fundamental algorithms used in practical programming, with a bias towards graphics. It...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.