C program, struct and functions help

  • Thread starter Thread starter jam12
  • Start date Start date
  • Tags Tags
    Functions Program
AI Thread Summary
The discussion focuses on creating a function to calculate the ratio of the cube of a planet's radius to the square of its orbital period, specifically for Earth. The initial code resulted in a -NaN error due to an uninitialized `Planet` structure. The solution involved removing the unnecessary local `Earth` variable in the `estM` function and directly using the passed `body` pointer. The corrected function utilizes the pointer notation `body->radius` and `body->period` for clarity and efficiency. Additionally, the user learned to use a pointer to reference the Earth data from the `plist` array, which resolved the issue. The final implementation allows for proper calculations by calling `estM(plist[2])`, ensuring that the correct values for Earth are used.
jam12
Messages
37
Reaction score
0
Hello, i want to write a function in the form of "float estM( Planet *body);" to calculate radius^3/period^2 for the planet earth, where i have an array of values for radius and period of different planets.
Here is the program so far, i am getting a -NaN error:

#include <stdio.h>
#include <math.h>

typedef struct pt {
char *name;
float period; /* orbital period in years */
float radius; /* orbital radius in ast units */
} Planet;

main() {

float estM( Planet *body); /* Have been told that the function is of this form to calculate radius^3/period^2 for earth*/

Planet Earth;
Planet plist[6];
int n, i;
Planet *body;
body=&Earth;

char nameMe[]="Mercury", nameV[]="Venus", nameE[]="Earth", nameMa[]="Mars", nameJ[]="Jupiter", nameS[]="Saturn";


plist[0].name = nameMe;
plist[0].period = 0.241;
plist[0].radius = 0.387;

plist[1].name = nameV;
plist[1].period = 0.615;
plist[1].radius = 0.723;

plist[2].name = nameE; /*Here is info for Earth */
plist[2].period = 1.0;
plist[2].radius = 1.0;

plist[3].name = nameMa;
plist[3].period = 1.881;
plist[3].radius = 1.524;

plist[4].name = nameJ;
plist[4].period = 11.86;
plist[4].radius = 5.203;

plist[5].name = nameS;
plist[5].period = 29.46;
plist[5].radius = 9.540;


n=6;
printf("Planet name\t period\t radius\n");

for (i=0;i<n;i++) {

printf("%s\t\t %.3f\t %.3f\n",plist.name, plist.period, plist.radius);

}

printf("%f\n",estM(body)); /* I want to calculate radius^3/period^2 for Earth */


}

float estM( Planet *body) { /*function to caculate radius^3/period^2 for Earth */

float m;
Planet Earth;
body=&Earth;
m=pow((*body).radius,3)/pow((*body).period,2);
return m;
}
 
Technology news on Phys.org
jam12 said:
Hello, i want to write a function in the form of "float estM( Planet *body);" to calculate radius^3/period^2 for the planet earth, where i have an array of values for radius and period of different planets.
Here is the program so far, i am getting a -NaN error:

Code:
float estM( Planet *body) {		/*function to caculate radius^3/period^2 for Earth */

float m;
Planet Earth;
body=&Earth;
m=pow((*body).radius,3)/pow((*body).period,2);
return m;
}

Looks like you accidentally left a non-initialized Earth structure in which you point the body pointer to (non-initialized floating point numbers are very likely to give a NaN). You should probably leave out the Earth variable, like

Code:
float estM( Planet *body) {		/*function to caculate radius^3/period^2 for Earth */
  return pow(body->radius,3)/pow(body->period,2);
}

where I have also replaced the (*pointer).member notation with pointer->member. You should then call this function with something sensible, like

Code:
printf("%f\n",estM(plist[2])); /* I want to calculate radius^3/period^2 for Earth */

Note, that in your code, the Earth variable in main is again non-initialized and will contain garbage, so either use plist[2] directly to refer to the values for Earth (as I did in the above snippet), or declare, say, Earth as "Planet *earth = plist[2];" after the plist declaration and then use "estM(earth)" later. If you want to call estM in your loop you will probably want to call with "estM(plist)".
 
Thanks just did it now, the code i used is:
in main:

Planet plist[6];
Planet *earth;
int n, i;
earth=&plist[2];

Here i didnt know that i could use a pointer to point to a member in the array, but it makes sense since all the members in the array are of type Planet.

then my function definition is just:

float estM( Planet *body) { /*function to caculate radius^3/period^2 for Earth */

return pow(body->radius,3)/pow(body->period,2);
}

Thanks again
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
2K
Replies
89
Views
5K
Replies
75
Views
6K
Replies
2
Views
1K
Replies
5
Views
3K
Replies
36
Views
4K
Replies
21
Views
9K
Back
Top