Code:
double sec_mod (int base, int height) /* take values of base and height, as determined by for loop and return (base*height^2)/6.0 */
{
return ((double)base*height*height)/6.0; /* cast to double because base and height are ints */
}
Is the cast to double completely unecessary or is it good programming practice to make it an explicit cast?
Also, the following code generates a compiler error:
Code:
void display_table (void)
{
printf("Lumber Size\tCross-Sectional Area\tMoment of Inertia\tSection Modulus\n"); /* table header */
int base=0; /* base is the base */
int height=0; /* height is the height */
.
.
.
but, moving the printf after the declarations works. Does C require declarations to precede all other statements in a function body?
If it helps here's the rest of the code (which compiles correctly):
Code:
#include <stdio.h> /* for printf and scanf */
void display_table (void); /* prototype for function to display complete table */
void lumber_size (int base, int height); /* prototype for function to display lumber size */
int x_sec(int base, int height); /* prototype for function to display cross-section size */
double moi(int base, int height); /* prototype for function to display moment of inertia */
double sec_mod(int base, int height); /* prototype for function to display cross-sectional area */
int main (void)
{
display_table(); /* call function to display complete lumber table */
return 0;
}
void display_table (void)
{
int base=0; /* base is the base */
int height=0; /* height is the height */
printf("Lumber Size\tCross-Sectional Area\tMoment of Inertia\tSection Modulus\n"); /* table header */
for (base=2; base<11;base+=2) /* base values are 2, 4, 6, 8, 10 */
{
for (height=2; height<13; height+=2) /* height values are 2,4,6,8,10,12 */
{
lumber_size(base,height); /* call function to display lubmer size */
printf("\t\t\t%d",x_sec(base,height)); /* print values returned by the function to compute cross-sectional size */
printf("\t\t\t%.2lf",moi(base,height)); /* print values returned by the function to compute moment of intertia */
printf("\t\t\t%.2lf\n",sec_mod(base,height)); /* print values returned by the function to compute section modulus */
}
}
}
void lumber_size (int base, int height) /* take values of base and height, as determined by for loop and display as base * height */
{
printf("%d x %d", base,height);
}
int x_sec(int base, int height) /* take values of base and height, as determined by for loop and return base*height */
{
return base*height;
}
double moi(int base, int height) /* take values of base and height, as determined by for loop and return (base * height^3)/12.0 */
{
return ((double)base*height*height*height)/12.0; /* cast to double because base and height are ints */
}
double sec_mod (int base, int height) /* take values of base and height, as determined by for loop and return (base*height^2)/6.0 */
{
return ((double)base*height*height)/6.0; /* cast to double because base and height are ints */
}
In the interest of full disclosure i'll mention this is homework that i'll be graded on.