Jul 27, 2008

Struct And Pointer Arithmetic/notation.

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Struct And Pointer Arithmetic/notation.

kvarnerexpress
My book that I am reading gave us an assignment that I dont fully understand.

Quote:
QUOTE
Create a program that includes the following:


A structure called ‘planet’ that includes a name, a distance from the sun and the diameter of the planet.


This I understand.
Code:

CODE
struct Planet {
string planName;
double planDistSun;
double planDiam;
};




Quote:
QUOTE
Create an array of 3 planet structures.



Does my book mean something like this? What type of array should I use? Could I use a char array?
Code:

CODE
Planet Mars;
Planet Earth;
Planet Jupiter;
char planetAry[3];

planetAry[0]=Mars;
planetAry[1]=Earth;
planetAry[2]=Jupiter;



Quote:
QUOTE
Include a function to enter the information for each planet. Use pointer notation to assign the values.



How would I do this? I understand that I can do something like:

Mars.planDiam=50000;
Mars.planDistSun=9000000

but how would I allow the user to enter a name for the planet then make a structure object though. I could just preassign the planets but thats not how he wants it.

Quote:
QUOTE
A function to show all the info for each planet. Use pointer arithmetic to move from one array position to the next.


I dont know how I would go about doing this? Any ideas would be helpful.
kvarnerexpress

 

 

 


Reply

mama_soap
QUOTE
My book that I am reading gave us an assignment that I dont fully understand.

Quote:

Does my book mean something like this? What type of array should I use? Could I use a char array?
Code:

CODE
Planet Mars;
Planet Earth;
Planet Jupiter;
char planetAry[3];

planetAry[0]=Mars;
planetAry[1]=Earth;
planetAry[2]=Jupiter;



I am afraid this is the incorrect way of going about the problem. You cannot assign a variable of an user defined datatype to a variable of a built in data type, which is exactly what you're trying to do (well, technically you can, but that's only when we come to objects and object-oriented programming). Now, creating an array of planets is not very difficult - here's what you have to do:

CODE
Planet array[3];


That is all, should work smile.gif

QUOTE
Include a function to enter the information for each planet. Use pointer notation to assign the values.


You have to write a separate function that reads data and assigns it to the Planet variable. The general format would probably be:

CODE
void readPlanet(Planet* x)
{
............your statements go in here..........
}


and while using this function in the main module, you could try

CODE

readPlanet(&array)


Similarly, write a function called writePlanet which does exactly the opposite smile.gif If you are concerned about how to use pointer arithmatic in the context of this function, then you can read the code for writePlanet below (I am hoping it will work, but I haven't tested it yet).

CODE

writePlanet(Planet* x)
{
int i=0;
while(i<3)
{
cout << "\n Planet name " << x[i].planName;
cout << "\n Planet's distance from the sun" << x[i].planSun;
cout << "\n Planet's diameter " << x[i].planDiam;
}
return;
}


You could make the function compatible for different array sizes, by making the array size a parameter, and using that parameter in place of "3". Good luck and hope this helps smile.gif

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:


Searching Video's for struct, pointer, arithmetic, notation
advertisement



Struct And Pointer Arithmetic/notation.



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE