1. ## Float problem

Code:
```/***************************************
* Pupose: Application Exercise		*
* Date: July 27, 2011			*
* Author/Reference: C Programming for CS*
* Problem: Write a program that can	*
* calculate the area of a parallelogram *
* defined by two vectors and the volume *
* of a parallelepiped defined by three	*
* vectors. Input the i, j, and k	*
* componenets of each of the three	*
* vectors from the keyboard and print	*
* the result to the screen. 		*
****************************************/

/************************************************************************************
*************************************************************************************
**			APPLICATION PROGRAM TO CALCULATE
**			AREA OF PARRALELOGRAM AND VOLUME OF
**			PARALLELEPIPED DEFINED BY VECTORS A, B, AND C
**			AREA = LENGTH OF (A CROSS B)
**			VOLUME = ABS VALUE OF (A DOT (B CROSS C)
*************************************************************************************
************************************************************************************/

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

/***********************************************************
**		FUNCTION PROTOTYPES
***********************************************************/

float dot_product (float g1, float g2, float g3, float h1, float h2, float h3, float k1, float k2, float k3);
void cross_product(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3);
float vector_magnitude(float g1, float g2, float g3);
float area_parallelogram(float g1, float g2, float g3, float h1, float h2, float h3);
float volume_parallelepiped(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3);
void read_input(float *g1, float *g2, float *g3, float *h1, float *h2, float *h3, float *k1, float *k2, float *k3);
void print_output (float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3, float area, float volume);

/***********************************************************************************
**	               FUNCTION main
**		VARIABLES
**			a1, a2, a3 = COMPONENTS OF VECTOR A
**			b1, b2, b3 = COMPONENTS OF VECTOR B
**			c1, c2, c3 = COMPONENTS OF VECTOR C
**			area	= AREA OF PARALLELOGRAM DEFINED BY A AND B
**				volume	= VOLUME OF PARALLELPIPED DEFINED BY A, B AND C
************************************************************************************/

void main(void)
{
float a1, a2, a3, b1, b2, b3, c1, c2, c3, area, volume;
read_input(&a1, &a2, &a3, &b1, &b2, &b3, &c1, &c2, &c3);
area = area_pareallelogram(a1, a2, a3, b1, b2, b3);
volume = volume_parellelepiped(a1, a2, a3, b1, b2, b3, c1, c2, c3);
print_output(a1, a2, a3, b1, b2, b3, c1, c2, c3, area, volume);
}

/***********************************************************************************
**		FUNCTION	 dot_product
**	CALCULATES THE DOT PRODUCT OF TWO VECTORS G AND H
**			VARIABLES - INPUT
**				g1, g2, g3 = COMPONENTS OF VECTOR G
**				h1, h2, h3 = COMPONENTS OF VECTOR H
**			RETURN
**				SCALAR VALUE OF THE DOT PRODUCT OF G AND H
************************************************************************************/

float dot_product (float g1, float g2, float g3, float h1, float h2, float h3)
{
float g_dot_h;
g_dot_h = g1*h1+g2*h2+g3*h3;
return (g_dot_h);
}

/***********************************************************************************
**		FUNCTION	 cross_product
**	CALCULATES THE CROSS PRODUCT OF TWO VECTORS G AND H
**			VARIABLES - INPUT
**				g1, g2, g3 = COMPONENTS OF VECTOR G
**				h1, h2, h3 = COMPONENTS OF VECTOR H
**			RETURN
**				*k1, *k2, *k3 = COMPONENTS OF VECTOR THAT IS
**								THE CROSS PRODUCT
************************************************************************************/

void cross_product(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3)
{
*k1 = g2*h3 - g3*h2;
*k2 = -(g1*h3 - g3*h1);
*k3 = g1*h2 - g2*h1;
}

/***********************************************************************************
**		FUNCTION	 vector_magnitude
**	CALCULATES THE MAGNITUDE OF A VECTOR G
**			VARIABLES - INPUT
**				g1, g2, g3 = COMPONENTS OF VECTOR G
**			RETURN
**				SCALAR VALUE OF THE MAGNITUDE OR LENGTH OF G
************************************************************************************/

float vector_magnitude(float g1, float g2, float g3)
{
float mag;
mag = sqrt((pow(g1, 2))+(pow(g2, 2))+(pow(g3, 2)));
return(mag);
}

/***********************************************************************************
**		FUNCTION	 area_parellelogram
**	CALCULATES THE AREA OF A PARALLELOGRAM DEFINED BY
**	TWO VECTORS G AND H
**	AREA = MAGNITUDE OF (G CROSS H)
**			VARIABLES - INPUT
**				g1, g2, g3 = COMPONENTS OF VECTOR G
**				h1, h2, h3 = COMPONENTS OF VECTOR H
**			RETURN
**				AREA OF THE PRALLELOGRAM
**			FUNCTION CALLS
**				CALLS cross_product TO CALCULATE G CROSS H
**				CALLS vector_magnitude TO GET MAGNITUDE OF (G CROSS H)
**
************************************************************************************/

float area_parallelogram(float g1, float g2, float g3, float h1, float h2, float h3)
{
float k1, k2, k3, area;
cross_product(g1, g2, g3, h1, h2, h3, &k1, &k2, &k3);
area = vector_magnitude(k1, k2, k3);
return(area);
}

/***********************************************************************************
**		FUNCTION	 volume_paralelepiped
**	CALCULATES THE VOLUME OF A PARALLELEPIPED DEFINED BY
**	THREE VECTORS G, H AND K
**	VOLUME = ABS VALUE OF [G DOT (H CROSS K)]
**			VARIABLES - INPUT
**				g1, g2, g3 = COMPONENTS OF VECTOR G
**				h1, h2, h3 = COMPONENTS OF VECTOR H
**				k1, k2, k3 = COMPONENTS OF VECTOR K
**				d1, d2, d3 = COMPONENTS OF VECTOR D
**			RETURN
**				VOLUME OF THE PARALLELEPIPED
**			FUNCTION CALLS
**				CALLS cross_product TO CALCULATE D = H CROSS K
**				CALLS dot_product TO GET [G dot H]
**
************************************************************************************/

float volume_parallelepiped(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3)
{
float d1, d2, d3, volume;
cross_product(h1, h2, h3, k1, k2, k3, &d1, &d2, &d3);
volume = fabs(dot_product(g1, g2, g3, d1, d2, d3));
return(volume);
}

/***********************************************************************************
**		FUNCTION	 cross_product
**	CALCULATES THE CROSS PRODUCT OF TWO VECTORS G AND H
**			VARIABLES - INPUT
**				*g1, *g2, *g3 = COMPONENTS OF VECTOR G
**				*h1, *h2, *h3 = COMPONENTS OF VECTOR H
**				*k1, *k2, *k3 = COMPONENTS OF VECTOR K
************************************************************************************/

void read_input(float *g1, float *g2, float *g3, float *h1, float *h2, float *h3, float *k1, float *k2, float *k3)
{
printf("Enter vector A (a1 a2 a3): \n");
scanf("%f %f %f", &*g1, &*g2, &*g3);
printf("Enter vector B (b1 b2 b3): \n");
scanf("%f %f %f", &*h1, &*h2, &*h3);
printf("Enter vector C (c1 c2 c3): \n");
scanf("%f %f %f", &*k1, &*k2, &*k3);
}

/***********************************************************************************
**		FUNCTION	 cross_product
**	CALCULATES THE CROSS PRODUCT OF TWO VECTORS G AND H
**			VARIABLES - INPUT
**				a1, a2, a3 = COMPONENTS OF VECTOR A
**				b1, b2, b3 = COMPONENTS OF VECTOR B
**				c1, c2, c3 = COMPONENTS OF VECTOR C
**				area	= area of parallelogram
**				volume	= volume of parallelepiped
************************************************************************************/

void print_output (float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3, float area, float volume)
{
printf("\n\n\r The input vectors are: \n\n"
"\r	i	j	k	\n"
"\r		%5.2f	%5.2f	%5.2f \n"
"\r		%5.2f	%5.2f	%5.2f \n"
"\r		%5.2f	%5.2f	%5.2f \n\n\n\n", a1, a2, a3, b1, b2, b3, c1, c2, c3);

printf("The results are : \n\n"
"\rArea of parallelogram (AxB)"
"\rVolume of parallelepiped (abs(A dot(BxC))= %f \n\n",	area, volume);
}```
My problem here is that the error is

cannot convert from 'float *' to 'float'

from this statements.
cross_product(h1, h2, h3, k1, k2, k3, &d1, &d2, &d3);
cross_product(g1, g2, g3, h1, h2, h3, &k1, &k2, &k3);

i dont understand.. the book says that this is ok but the compiler and the IDE says otherwise.. just can't understand why there is a problem...

2. You need to read your book more closely, because it doesn't say this:
Code:
`float dot_product (float g1, float g2, float g3, float h1, float h2, float h3, float k1, float k2, float k3);`
at the top like your code does (assuming this is code from the book).

EDIT: HAHAHAHA I can't tell the difference between dot and cross. Go with Andrew below.

3. Code:
```float volume_parallelepiped(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3)
{
float d1, d2, d3, volume;
cross_product(h1, h2, h3, k1, k2, k3, &d1, &d2, &d3);
volume = fabs(dot_product(g1, g2, g3, d1, d2, d3));
return(volume);
}```
You are passing pointers and your function expects values. e.g. cross_product(h1, h2, h3,*k1, *k2, *k3, &d1, &d2, &d3);

4. As a matter of form...

1) The correct form of main is not void main (void)... it's int main (void) ... main passes an integer errorlevel (usually 0) back to the operating system.

2) Who on earth taught you to write such ugly code...
2A) You comment the hell out of obvius stuff, but don't comment any of the stuff that isn't self-explanatory.
2B) a1, a2 etc are not good variable names. Give them meaningful names.
2C) Instead of having multiple related variables make arrays... a1, a2, a3 can easily be area[3]
2D) Using arrays will shorten your function declarations significantly... instead of passing 3 hs just pass height[] into the function.

Nothing personal, but I've seldom seen such wildly overdecorated code.

5. Originally Posted by CommonTater
2) Who on earth taught you to write such ugly code...
What's ugly?

6. Originally Posted by Matticus
What's ugly?
Was leaving that opening intentional?

Quzah.

7. @tabstop yup thanks for that one, I am also checking why I have an error about the dot_product...
@Andrew Hunter I still dont get it... but i will try maybe the book is wrong, cause i check it 3 times and it goes that way.
@Common Tater My instructor says to read the book, and solve the problem, the only given was the function prototype and it is up to us on how we make it into a fullw riten source code. And my instructor says use functions, and I did ask about arrays, and he told me we will use that at our next lesson..

8. i think the problem is the &d1, &d2, &d3 and not the k since k is Ok... did i just rhyme the word k??? o.O

anyways, i still dont get it... I want to pass the values of d2 to k2, and and function send the k2 to d2. But since I have not learned that well about pointers. I am studying about functions and how it works. All I know is that when you pass a variable from function to main function, you use '*' and when you get a variable from function to the main function you use "&". And I'm still confused with actual and formal parameters.... very very confused.....

9. I gave it a faaast shot and the first thing I realized is that your declaration of dot_product paramaters:
Code:
`float dot_product (float g1, float g2, float g3, float h1, float h2, float h3, float k1, float k2, float k3);`
And your definition's paramaters don't match:
Code:
```float dot_product (float g1, float g2, float g3, float h1, float h2, float h3)
{
float g_dot_h;
g_dot_h = g1*h1+g2*h2+g3*h3;
return (g_dot_h);
}```
Obviously what's wrong is the first one, you should remove:
Code:
`float k1, float k2, float k3`
Ok your error comes because when you call cross_product from inside of volume_parallelepiped you are not passing the values of k1, k2, k3 (floats) you are passing pointers (floats *) hence your error... what cross_product needs by your declaration of it is 3 floats, 3 floats and 3 pointers to float, you are passing 3 floats, 3 pointers to float and 3 pointers to float. Thus, what you have to do is pass the value of k1, k2 and k3 will leave that for you as you are struggling with pointers

P.S: Also you have 2 more errors:
Code:
```area = area_pareallelogram(a1, a2, a3, b1, b2, b3);
volume = volume_parellelepiped(a1, a2, a3, b1, b2, b3, c1, c2, c3);```
Should be:
Code:
```area = area_parallelogram(a1, a2, a3, b1, b2, b3);
volume = volume_parallelepiped(a1, a2, a3, b1, b2, b3, c1, c2, c3);```
And also you'll get an error in c1, c2, c3 because you are passing the value you need to pass a pointer

10. ok, i have now changed the float dot_product (float g1, float g2, float g3, float h1, float h2, float h3)... also the wrong spellings of the parallelogram and the parallelepiped, but i still have a problem with the float pointer...

@beta3designs what should I do? I'm just a novice at this. I have to pass a variable to the function which is void and i have to retrieve it. So, how do I fix it? what should I do? what should i change instead? do I have to change my whole function. And please correct me about the passing idea thing... Im struggling right now about pointers, and I have a very hard time understanding it.

11. Post your updated code and errors.

12. Originally Posted by Matticus
What's ugly?
In my experience (because I thought I commented the heck out of everything) too many comments can actual obscure the code making it harder to follow... The OP has more comments than code and they're all dressed up in stars and stripes... I started looking at his code and gave up because of all the clutter.

13. Code:
```/****************************************
* Pupose: Application Exercise		*
* Date: July 27, 2011			*
* Author/Reference: C Programming for CS*
* Problem: Write a program that can	*
* calculate the area of a parallelogram *
* defined by two vectors and the volume *
* of a parallelepiped defined by three	*
* vectors. Input the i, j, and k	*
* componenets of each of the three	*
* vectors from the keyboard and print	*
* the result to the screen. 		*
****************************************/

/************************************************************************************
*************************************************************************************
**						APPLICATION PROGRAM TO CALCULATE
**						AREA OF PARRALELOGRAM AND VOLUME OF
**						PARALLELEPIPED DEFINED BY VECTORS A, B, AND C
**						AREA = LENGTH OF (A CROSS B)
**						VOLUME = ABS VALUE OF (A DOT (B CROSS C)
*************************************************************************************
************************************************************************************/

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

/***********************************************************
**				FUNCTION PROTOTYPES
***********************************************************/

float dot_product (float g1, float g2, float g3, float h1, float h2, float h3);
void cross_product(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3);
float vector_magnitude(float g1, float g2, float g3);
float area_parallelogram(float g1, float g2, float g3, float h1, float h2, float h3);
float volume_parallelepiped(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3);
void read_input(float *g1, float *g2, float *g3, float *h1, float *h2, float *h3, float *k1, float *k2, float *k3);
void print_output (float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3, float area, float volume);

/***********************************************************************************
**			FUNCTION main
**				VARIABLES
**					a1, a2, a3 = COMPONENTS OF VECTOR A
**					b1, b2, b3 = COMPONENTS OF VECTOR B
**					c1, c2, c3 = COMPONENTS OF VECTOR C
**						area	= AREA OF PARALLELOGRAM DEFINED BY A AND B
**						volume	= VOLUME OF PARALLELPIPED DEFINED BY A, B AND C
************************************************************************************/

void main(void)
{
float a1, a2, a3, b1, b2, b3, c1, c2, c3, area, volume;
read_input(&a1, &a2, &a3, &b1, &b2, &b3, &c1, &c2, &c3);
area = area_parallelogram(a1, a2, a3, b1, b2, b3);
volume = volume_parallelepiped(a1, a2, a3, b1, b2, b3, &c1, &c2, &c3);
print_output(a1, a2, a3, b1, b2, b3, c1, c2, c3, area, volume);
}

/***********************************************************************************
**			FUNCTION	 dot_product
**		CALCULATES THE DOT PRODUCT OF TWO VECTORS G AND H
**				VARIABLES - INPUT
**					g1, g2, g3 = COMPONENTS OF VECTOR G
**					h1, h2, h3 = COMPONENTS OF VECTOR H
**				RETURN
**				S	CALAR VALUE OF THE DOT PRODUCT OF G AND H
************************************************************************************/

float dot_product (float g1, float g2, float g3, float h1, float h2, float h3)
{
float g_dot_h;
g_dot_h = g1*h1+g2*h2+g3*h3;
return (g_dot_h);
}

/***********************************************************************************
**			FUNCTION	 cross_product
**		CALCULATES THE CROSS PRODUCT OF TWO VECTORS G AND H
**				VARIABLES - INPUT
**					g1, g2, g3 = COMPONENTS OF VECTOR G
**					h1, h2, h3 = COMPONENTS OF VECTOR H
**				RETURN
**					*k1, *k2, *k3 = COMPONENTS OF VECTOR THAT IS
**									THE CROSS PRODUCT
************************************************************************************/

void cross_product(float g1, float g2, float g3, float h1, float h2, float h3, float *k1, float *k2, float *k3)
{
*k1 = g2*h3 - g3*h2;
*k2 = -(g1*h3 - g3*h1);
*k3 = g1*h2 - g2*h1;
}

/***********************************************************************************
**			FUNCTION	 vector_magnitude
**		CALCULATES THE MAGNITUDE OF A VECTOR G
**				VARIABLES - INPUT
**					g1, g2, g3 = COMPONENTS OF VECTOR G
**				RETURN
**					SCALAR VALUE OF THE MAGNITUDE OR LENGTH OF G
************************************************************************************/

float vector_magnitude(float g1, float g2, float g3)
{
float mag;
mag = sqrt((pow(g1, 2))+(pow(g2, 2))+(pow(g3, 2)));
return(mag);
}

/***********************************************************************************
**			FUNCTION	 area_parellelogram
**		CALCULATES THE AREA OF A PARALLELOGRAM DEFINED BY
**		TWO VECTORS G AND H
**		AREA = MAGNITUDE OF (G CROSS H)
**				VARIABLES - INPUT
**					g1, g2, g3 = COMPONENTS OF VECTOR G
**					h1, h2, h3 = COMPONENTS OF VECTOR H
**				RETURN
**					AREA OF THE PRALLELOGRAM
**				FUNCTION CALLS
**					CALLS cross_product TO CALCULATE G CROSS H
**					CALLS vector_magnitude TO GET MAGNITUDE OF (G CROSS H)
**
************************************************************************************/

float area_parallelogram(float g1, float g2, float g3, float h1, float h2, float h3)
{
float d1, d2, d3, area;
cross_product(g1, g2, g3, k1, k2, k3, &d1, &d2, &d3);
area = vector_magnitude(k1, k2, k3);
return(area);
}

/***********************************************************************************
**			FUNCTION	 volume_paralelepiped
**		CALCULATES THE VOLUME OF A PARALLELEPIPED DEFINED BY
**		THREE VECTORS G, H AND K
**		VOLUME = ABS VALUE OF [G DOT (H CROSS K)]
**				VARIABLES - INPUT
**					g1, g2, g3 = COMPONENTS OF VECTOR G
**					h1, h2, h3 = COMPONENTS OF VECTOR H
**					k1, k2, k3 = COMPONENTS OF VECTOR K
**					d1, d2, d3 = COMPONENTS OF VECTOR D
**				RETURN
**					VOLUME OF THE PARALLELEPIPED
**				FUNCTION CALLS
**					CALLS cross_product TO CALCULATE D = H CROSS K
**					CALLS dot_product TO GET [G dot H]
**
************************************************************************************/

float volume_parallelepiped(float g1, float g2, float g3, float d1, float d2, float d3, float *k1, float *k2, float *k3)
{
float volume;
cross_product(h1, h2, h3, d1, d2, d3, &(*k1), &*(k2), &(*k3);
volume = fabs(dot_product(g1, g2, g3, d1, d2, d3));
return(volume);
}

/***********************************************************************************
**			FUNCTION	 cross_product
**		CALCULATES THE CROSS PRODUCT OF TWO VECTORS G AND H
**				VARIABLES - INPUT
**					*g1, *g2, *g3 = COMPONENTS OF VECTOR G
**					*h1, *h2, *h3 = COMPONENTS OF VECTOR H
**					*k1, *k2, *k3 = COMPONENTS OF VECTOR K
************************************************************************************/

void read_input(float *g1, float *g2, float *g3, float *h1, float *h2, float *h3, float *k1, float *k2, float *k3)
{
printf("Enter vector A (a1 a2 a3): \n");
scanf("%f %f %f", &*g1, &*g2, &*g3);
printf("Enter vector B (b1 b2 b3): \n");
scanf("%f %f %f", &*h1, &*h2, &*h3);
printf("Enter vector C (c1 c2 c3): \n");
scanf("%f %f %f", &*k1, &*k2, &*k3);
}

/***********************************************************************************
**			FUNCTION	 cross_product
**		CALCULATES THE CROSS PRODUCT OF TWO VECTORS G AND H
**				VARIABLES - INPUT
**					a1, a2, a3 = COMPONENTS OF VECTOR A
**					b1, b2, b3 = COMPONENTS OF VECTOR B
**					c1, c2, c3 = COMPONENTS OF VECTOR C
**					area	= area of parallelogram
**					volume	= volume of parallelepiped
************************************************************************************/

void print_output (float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3, float area, float volume)
{
printf("\n\n\r The input vectors are: \n\n"
"\r	i	j	k	\n"
"\r		%5.2f	%5.2f	%5.2f \n"
"\r		%5.2f	%5.2f	%5.2f \n"
"\r		%5.2f	%5.2f	%5.2f \n\n\n\n", a1, a2, a3, b1, b2, b3, c1, c2, c3);

printf("The results are : \n\n"
"\rArea of parallelogram (AxB)"
"\rVolume of parallelepiped (abs(A dot(BxC))= %f \n\n",	area, volume);
}```
i tried changing and updating the codes except for the cross product.. i tried changing the variable names for 'k' and 'd' but still nothing work...

as for the parallelogram and the parallelepiped. i changed the typo errors and the dot product errors.... still not working...

14. What are the errors?

15. Code:
```\$ gcc -Wall baz.c
baz.c:55: warning: return type of ‘main’ is not ‘int’
baz.c: In function ‘area_parallelogram’:
baz.c:143: error: ‘k1’ undeclared (first use in this function)
baz.c:143: error: (Each undeclared identifier is reported only once
baz.c:143: error: for each function it appears in.)
baz.c:143: error: ‘k2’ undeclared (first use in this function)
baz.c:143: error: ‘k3’ undeclared (first use in this function)
float area_parallelogram(float g1, float g2, float g3, float h1, float h2, float h3)
{
float d1, d2, d3, area;
cross_product(g1, g2, g3, k1, k2, k3, &d1, &d2, &d3);
area = vector_magnitude(k1, k2, k3);
return(area);
}```
Where do you see k1,k2,k3 ?
What else should you be using here?

Code:
```baz.c: In function ‘volume_parallelepiped’:
baz.c:171: error: ‘h1’ undeclared (first use in this function)
baz.c:171: error: ‘h2’ undeclared (first use in this function)
baz.c:171: error: ‘h3’ undeclared (first use in this function)
baz.c:171: error: expected ‘)’ before ‘;’ token
baz.c:174: error: expected ‘;’ before ‘}’ token
baz.c:170: warning: unused variable ‘volume’
float volume_parallelepiped(float g1, float g2, float g3, float d1, float d2, float d3, float *k1, float *k2, float *k3)
{
float volume;
cross_product(h1, h2, h3, d1, d2, d3, &(*k1), &*(k2), &(*k3);
volume = fabs(dot_product(g1, g2, g3, d1, d2, d3));
return(volume);
}```
Ditto here, pick the right variable names.
Also check the balancing of () in your function call.
Also, &(*k1) is just k1

Code:
```baz.c: In function ‘print_output’:
baz.c:220: warning: too many arguments for format```
Some of the parameters you're passing to printf will NOT be printed.