# Thread: I need help with a lab... very easy stuff

1. ## I need help with a lab... very easy stuff

Well, I'm in my first semester of computer science class. We've just started learning functions, and had to have four separate ones in this lab. It seems to look okay, but I can't figure out whats wrong. The goal of the lab is to make a table that shows Fahrenheit values you input, a conversion to celsius, and its supposed to measure the distance of both to their freezing points. Could anyone help me troubleshoot what I've been doing wrong? Also, I'd appreciate an explanation as to why you changed or corrected it. Thanks a bunch

Code:
```/*Lab 7*/

#include <stdio.h>

greet();
calc();
display();

/*The first function is used to declare the variables being used in the program.
It also states the three other functions used in this program, and initiates them.*/
int main()
{
//Input variables

greet();
calc();
display();

return 0;
}

/*This function is used to prompt the user to input their values so calculations can begin*/

greet()

{

float fahr1, fahr2, fahr3, fahr4;

//Put a heading to explain what the program is used for

printf("This program is designed to convert a given degree in farenheit to celsius. This");
printf("program is also used to find out how many degrees away the entered degree is "
"from it's freezing point in both farenheit and celsius\n\n\n");

//Assign values to variables
printf("Type your first degree in Farenheit: ");
scanf("%f", &fahr1);
printf("\n\nType your second degree in Farenheit: ");
scanf("%f", &fahr2);
printf("\n\nType your third degree in Farenheit: ");
scanf("%f", &fahr3);
printf("\n\nType your fourth degree in Farenheit: ");
scanf("%f", &fahr4);
printf("\n\n\n\n");

calc(fahr1, fahr2, fahr3, fahr4);
display(fahr1, fahr2, fahr3, fahr4);

}

/*This next function will be used to carry out the calculations and work out the coversions*/

calc(fahr1, fahr2, fahr3, fahr4)

{

float cel1, cel2, cel3, cel4, diffahr1, diffahr2, diffahr3, diffahr4;

//Use equation (5.0/9.0)*(Fahrenheit-32) to convert to celsius

cel1= (5.0/9.0)*(fahr1-32);
cel2= (5.0/9.0)*(fahr2-32);
cel3= (5.0/9.0)*(fahr3-32);
cel4= (5.0/9.0)*(fahr4-32);

//Farenheit to freezing point distance

diffahr1= fahr1-32;
diffahr2= fahr2-32;
diffahr3= fahr3-32;
diffahr4= fahr4-32;

display(cel1, cel2, cel3, cel4, diffahr1, diffahr2, diffahr3, diffahr4);

}

/* This function is used to simply display all of the calculations done above.*/

display(fahr1, fahr2, fahr3, fahr4, cel1, cel2, cel3, cel4, diffahr1, diffahr2, diffahr3, diffahr4)
{

/*Table which displays the farenheit degrees entered, their conversions to celsius,
and the amount of degrees from freezing for both celsius and farenheit */

printf("Number     Farenheit     Celsius       Freezing-Fahrenheit     Freezing-Celsius\n");
printf("_______________________________________________________________________________\n");
printf("1          %f     %f      %f                 %f                  \n", fahr1, cel1, diffahr1, cel1);
printf("2          %f     %f      %f                 %f                  \n", fahr2, cel2, diffahr2, cel2);
printf("3          %f     %f      %f                 %f                  \n", fahr3, cel3, diffahr3, cel3);
printf("3          %f     %f      %f                 %f                  \n", fahr4, cel4, diffahr4, cel4);

}```

2. Yea...and what exactly do you think is wrong with it?

Do you even know what's wrong with it or just assuming there is something wrong with it.

Any compilational errors? Incorrect outputs?

3. calc(fahr1, fahr2, fahr3, fahr4){

This is not the correct way to start a function.

When calling it you can leave out the data types. But when prototyping it and writing the actual function the data types must be there.

calc(float fahr1, float fahr2, float fahr3, float fahr4);

and your actual function should be:

calc(float fahr1, float fahr2, float fahr3, float fahr4) {

When calling a function like calc, then it should be:

calc(fahr1, fahr2, fahr3, fahr4);

I haven't looked at your other functions, but correct these bits and see if others need the same.

4. Code:
```int main()
{
//Input variables

greet();
calc();
display();

return 0;
}```
greet() calls to calc on it's own, no point calling calc() in main() (especially with no arguments when it's supposed to take 4). Same story goes with display, which is called by calc -- no need to call it in main() nor in greet().
And you should define argument types, which are supposed to be float (assuming what your program should do) as JFonseka said.

5. Are you familiar with arrays? If so, consider using some -- it would reduce your number of variables dramatically. http://www.cprogramming.com/tutorial/c/lesson8.html

As the other two posters have said, there is a syntactical difference between calling a function and defining or declaring it. When you call a function, you can use constants or named variables in the current scope:
Code:
`printf("&#37;i %i %i\n", 0, x, y);`
When you implement or prototype a function (in standard C), every variable must have a type before it.
Code:
`int format_person(char *buffer, const char *name, const char *age, int age);`
The name of a variable in the calling function has no effect on the name of a variable in the function that is called.

Also see this tutorial: http://www.cprogramming.com/tutorial/c/lesson4.html

6. Ohhhh... I get it. It helps alot more when you have real people helping. My teacher is always occupied with other students questions, so thanks a bunch. I'll be back every now and then to ask for help.