C program, struct and functions help

In summary, the function will calculate radius^3/period^2 for a given Planet, but will error out if the body pointer is not initialized.
  • #1
jam12
38
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
  • #2
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)".
 
  • #3
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
 

Related to C program, struct and functions help

1. What is a struct in C programming?

A struct in C is a user-defined data type that allows you to store related data of different types under a single name. It is similar to a record in other programming languages. It is used to create complex data structures that can hold multiple data elements.

2. How do you declare a struct in C?

To declare a struct in C, you need to use the "struct" keyword, followed by the name of the struct and a set of curly braces containing the members of the struct. For example:
struct student {
    char name[20];
    int age;
    float gpa;
};

This creates a struct called "student" with three members: name, age, and gpa.

3. What are functions in C?

Functions in C are blocks of code that perform a specific task. They are used to break down a large program into smaller, more manageable pieces. Functions can be called multiple times from different parts of the program, making the code more organized and easier to maintain.

4. How do you declare a function in C?

To declare a function in C, you need to specify the return type, the name of the function, and any parameters it takes in. For example:
int sum(int num1, int num2) {
    int result = num1 + num2;
    return result;
}

This declares a function called "sum" that takes in two integers and returns an integer.

5. How do you use structs and functions together in C?

To use structs and functions together in C, you can pass a struct as a parameter to a function. This allows the function to access and manipulate the data stored in the struct. You can also return a struct from a function, which can be useful when creating and modifying complex data structures.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Classical Physics
Replies
2
Views
861
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
Back
Top