C program using functions to convert Fahrenheit and Celsius

In summary: All();int main(){float c1, c2, c3, f1, f2, f3;printAll();return 0;}In summary, the student is trying to solve an assignment in which they are asked to convert temperatures from Fahrenheit to Celsius. They are new to programming and do not understand all of the terminology or principles. They are having difficulty with compiling their code.
  • #1
HoojaMan33
15
0

Homework Statement


Hi i have an assignment that is asking me to convert 3 temperatures in fahrenheit to celcius and vice versa.
I am very new to programming only 2 weeks in(and i learn by playing with the program) so I do not know all of the terminology / principles.

I am not allowed to use global variables and I have to make function calls and use of call by value parameters.
My current code has many errors I believe and I think i fundamentally do not understand what is going on here, so any help would be great.

Homework Equations

The Attempt at a Solution


Code:
#include <stdio.h>
#include <math.h>
Calculation(float c1, float c2, float c3, float f1, float f2, float f3);

int main()
{

float c1, c2, c3, f1, f2, f3;
float new1, new1, new3;
float newa, newb, newc;

printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f", &f1, &f2, &f3);
new1 = calc_celcius(f1);
new2 = calc_celcius(f2);
new3 = calc_celcius(f3);

printf("The converted temperatures are: \t %.2f %.2f %.2f", new1, new2, new3);

printf("Please type in three temperatures in celcius");
scanf("%f %f %f", &c1, &c2, &c3);
newa = calc_fah(c1);
newb = calc_fah(c2);
newc = calc_fah(c3);

printf("The converted temperatures are: \t %.2f %.2f %.2f", newa, newb, newc);

return 0;

}

float calc_celcius(float f1, float f2, float f3);

{

float new1 = ((f1 - 32) * .55);
float new2 = ((f2 - 32) * .55);
float new3 = ((f3 - 32) * .55);

return(calc_celius);

}

float calc_fahr(float c1, float c2, float c3);

{

float newa = (c1 * 1.8) + 32;
float newb = (c2 * 1.8) + 32;
float newc = (c3 * 1.8) + 32;

return(calc_fahr);

}

My questions:
1) How do i properly configure my prototype function?
2) How do i properly declare variables locally or using parameters?
3) Is there anything I am missing

Thank you very much !
 
Physics news on Phys.org
  • #2
HoojaMan33 said:

Homework Statement


Hi i have an assignment that is asking me to convert 3 temperatures in fahrenheit to celcius and vice versa.
I am very new to programming only 2 weeks in(and i learn by playing with the program) so I do not know all of the terminology / principles.

I am not allowed to use global variables and I have to make function calls and use of call by value parameters.
My current code has many errors I believe and I think i fundamentally do not understand what is going on here, so any help would be great.

Homework Equations

The Attempt at a Solution


Code:
#include <stdio.h>
#include <math.h>
Calculation(float c1, float c2, float c3, float f1, float f2, float f3);

int main()
{

float c1, c2, c3, f1, f2, f3;
float new1, new1, new3;
float newa, newb, newc;

printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f", &f1, &f2, &f3);
new1 = calc_celcius(f1);
new2 = calc_celcius(f2);
new3 = calc_celcius(f3);

printf("The converted temperatures are: \t %.2f %.2f %.2f", new1, new2, new3);

printf("Please type in three temperatures in celcius");
scanf("%f %f %f", &c1, &c2, &c3);
newa = calc_fah(c1);
newb = calc_fah(c2);
newc = calc_fah(c3);

printf("The converted temperatures are: \t %.2f %.2f %.2f", newa, newb, newc);

return 0;

}

float calc_celcius(float f1, float f2, float f3);

{

float new1 = ((f1 - 32) * .55);
float new2 = ((f2 - 32) * .55);
float new3 = ((f3 - 32) * .55);

return(calc_celius);

}

float calc_fahr(float c1, float c2, float c3);

{

float newa = (c1 * 1.8) + 32;
float newb = (c2 * 1.8) + 32;
float newc = (c3 * 1.8) + 32;

return(calc_fahr);

}

My questions:
1) How do i properly configure my prototype function?
2) How do i properly declare variables locally or using parameters?
3) Is there anything I am missing

Thank you very much !

In your function calc_celcius, you should subtract 32. from the fahrenheit temperature, rather than 32, since this temp. is declared float. Also, the conversion 5. / 9. should be used in place of 0.55, since 5/9 is a non-terminating decimal, and using 0.55 cuts off precision of your result at two decimal places.

In your function calc_fahr, similarly, you should add 32. rather than 32

The correct name for the temperature scale is celsius with two s's. I don't know how celcius with two c's got started, probably the same place ect. comes from, rather than the correct etc. etc. = et cetera, which is Latin for "and other things".
 
  • #3
In addition to what SteamKing said, your calc_celsius() function should NOT calculate three Celsius temperatures at a time. The function should take a single Fahrenheit temperature and return the corresponding Celsius temperature.

Also, this is not how functions return a value in C:
C:
float calc_celcius(float f1, float f2, float f3);
{
   // ...
   return(calc_celius);
}
In C, the name of a function evaluates to the location in memory where the code for the function is stored. The statement
Code:
return(calc_celsius);
is returning a memory address, not the computed temperature value.

Please change the names in your program to the correct spelling -- celsius.
 
Last edited:
  • #4
Ok, i have changed the typo in my code. But still I am having issues with my code compiling.

Code:
#include <stdio.h>
#include <math.h>
float calc_celsius( float );
float calc_fahr( float );

void printAll();

int main()
{

float c1, c2, c3, f1, f2, f3;

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

return 0;

}

float calc_celsius( float fahr )
{
  float celc = (fahr - 32) *  9/5;
  return celc;
}

float calc_fahr(float celc)

{

return celc * 1.8 + 32;

}

void printAll() {
float calc_celsius( float celc);
float calc_fahr( float fahr );

printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1));
printf("%.2f \t\t %.2f \n", f2, calc_celsius(f2));
printf("%.2f \t\t %.2f \n", f3, calc_celsius(f3));

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, calc_fahr(c1));
printf("%.2f \t\t %.2f \n", c2, calc_fahr(c2));
printf("%.2f \t\t %.2f \n", c3, calc_fahr(c3));}

Here is my new code; In my assignment I am supposed to have a function that is dedicated to printing the results of my function. How can i declare functions calc_celsius and calc_fahr inside of printAll ? Is this even the right way of going about things?
Error logs say that everything inside of printAll is not declared(f1, f2, f3, c1, c2, c3) etcEdit: I mean how can i access variables in the main function for printAll ?Thanks .
 
  • #5
Nvm i got it. It took me a while to understand this stuff, does anyone have any tips on where i can get definitive info about C? It feels like I am just learning by trial and error which i don't really like.
Like a book for somewhat beginners(I guess i am still at intro level)?
 
  • #6
HoojaMan33 said:
Nvm i got it. It took me a while to understand this stuff, does anyone have any tips on where i can get definitive info about C? It feels like I am just learning by trial and error which i don't really like.
Like a book for somewhat beginners(I guess i am still at intro level)?
Isn't there a textbook suggested/required for your class? If not, this one isn't too bad -- "C Primer Plus" Sixth Ed., by Stephen Prata (https://www.amazon.com/dp/0321928423/?tag=pfamazon01-20).

What did you get for the final version of your program? I'm sure we could give you some tips for improving it.
 
  • #7
HoojaMan33 said:
Ok, i have changed the typo in my code. But still I am having issues with my code compiling.

Code:
#include <stdio.h>
#include <math.h>
float calc_celsius( float );
float calc_fahr( float );

void printAll();

int main()
{

float c1, c2, c3, f1, f2, f3;

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

return 0;

}

float calc_celsius( float fahr )
{
  float celc = (fahr - 32) *  9/5;
  return celc;
}

float calc_fahr(float celc)

{

return celc * 1.8 + 32;

}

void printAll() {
float calc_celsius( float celc);
float calc_fahr( float fahr );

printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1));
printf("%.2f \t\t %.2f \n", f2, calc_celsius(f2));
printf("%.2f \t\t %.2f \n", f3, calc_celsius(f3));

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, calc_fahr(c1));
printf("%.2f \t\t %.2f \n", c2, calc_fahr(c2));
printf("%.2f \t\t %.2f \n", c3, calc_fahr(c3));}

Here is my new code; In my assignment I am supposed to have a function that is dedicated to printing the results of my function. How can i declare functions calc_celsius and calc_fahr inside of printAll ? Is this even the right way of going about things?
Error logs say that everything inside of printAll is not declared(f1, f2, f3, c1, c2, c3) etcEdit: I mean how can i access variables in the main function for printAll ?Thanks .
It looks like you're doing the same conversion from fahrenheit to celsius as celsius to fahrenheit. Check the celsius to fahrenheit conversion formula.
 
  • #8
SteamKing said:
It looks like you're doing the same conversion from fahrenheit to celsius as celsius to fahrenheit. Check the celsius to fahrenheit conversion formula.
calc_celsius is meant to convert a celsius temperature into fahrenheit, which i think i didnt clarify. That is my bad

Here was my old code: (My teacher told me to use lots of comments but i think they are very annoying...)
Code:
#include <stdio.h>
#include <math.h>
float calc_celsius( float ); // prototype declarations
float calc_fahr( float ); // and again
float printAll(float f1, float f2, float f3, float c1, float c2, float c3); // last declaration

int main()
{

float c1, c2, c3, f1, f2, f3; // declaring input variables

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3); // storing them inside of main
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3); // and again storing

printAll(f1, f2, f3, c1, c2, c3); // calling printAll function for the previously stored variables

return 0;

}

float calc_celsius( float fahr ) // function definition
{
  float celc = (fahr - 32) * 5/9; // function and formula that is used to convert fahrenheit to celsius
  return celc; // returns the value back to main
}float calc_fahr(float celc) // function definition

{

return celc * 1.8 + 32; // function and formula that is used to convert celsius to fahrenheit, and returns the value to main

}

float printAll(float f1, float f2, float f3, float c1, float c2, float c3) { // parameters here include main function variables, so it can be used later on

printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1)); // calc_celsius is then called here in this statement, which converts f1(first input value from the user) into celsius
printf("%.2f \t\t %.2f \n", f2, calc_celsius(f2)); // and again, same thing is repeated with the 2nd value from the user
printf("%.2f \t\t %.2f \n", f3, calc_celsius(f3)); // and lastly the third value from the user is converted to celsius

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, calc_fahr(c1)); // calc_fahr is called in this line so that c1(first input value from user that is in celsius) is converted into fahrenheit
printf("%.2f \t\t %.2f \n", c2, calc_fahr(c2)); // and again
printf("%.2f \t\t %.2f \n", c3, calc_fahr(c3)); // and again

}

 /*

 printAll calls the functions calc_celsius and calc_fahr in order to print out the needed temperatures. Main function simply calls printAll after the user finishes typing his input, and from there the user is
 is able to see the converted temperatures */

now my question is, how can i use pointers instead of call by value? would this code suffice? (it does not compile)
Code:
printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f",&f1,&f2,&f3);printf("Please type in three temperatures in celcius");
scanf("%f %f %f",&c1,&c2,&c3);

calc_celsius(&f1,&f2,&f3)
printf("The converted temperatures are: \t %.2f %.2f %.2f", f1, f2, f3 );

calc_fahr(&c1,&c2,&c3))
printf("The converted temperatures are: \t %.2f %.2f %.2f", c1, c2, c3);return0;

}

void calc_celsius(float*f1,float*f2,float*f3);

{

*f1 =(((*f1)-32)*.55);*f2 =(((*f2)-32)*.55);*f3 =(((*f3)-32)*.55);

}

void calc_fahr(float*c1,float*c2,float*c3)

{

*c1 =((*c1)*1.8)+32;*c2 =((*c2)*1.8)+32;*c3 =((*c3)*1.8)+32;

}

I wasn't sure if i was to not have 3 temperatures calculated at the same time since I am using pointers(i thought it was spuposed to change stored values for each variable in the main function) or follow the same logic as my initial code.

and am i calling my functions correctly?

Thanks
 
  • #9
HoojaMan33 said:
C:
float calc_celsius( float fahr ) // function definition
{
  float celc = (fahr - 32) * 5/9; // function and formula that is used to convert fahrenheit to celsius
  return celc; // returns the value back to main
}
I don't know whether you realize this, but in C, 5/9 is equal to zero, due to the difference between integer division and floating point division. Your code probably works because of some casting going on behind the scenes, but you should not rely on this. It would be much better to write the fraction as 5.0/9.0 or 5.0f/9.0f.
HoojaMan33 said:
C:
printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f",&f1,&f2,&f3);
From the user's perspective, this is bad practice. It's much better to ask for one temperature at a time.

HoojaMan33 said:
C:
calc_celsius(&f1,&f2,&f3)
printf("The converted temperatures are: \t %.2f %.2f %.2f", f1, f2, f3 );
Why is your function calculating all three temperatures at a time? Also, the variables f1, f2, and f3, which represent Fahrenheit temperatures, will be overwritten by their Celsius equivalents.
I would write this function's header like so:
C:
void calc_celsius(float temp_f, float * temp_c)
After the function returns, the variable temp_c will be set to the equivalent Celsius temperature.

Same idea for the other conversion function.
HoojaMan33 said:
C:
*f1 =(((*f1)-32)*.55);*f2 =(((*f2)-32)*.55);*f3 =(((*f3)-32)*.55);
.55 is not very precise. It's better to use 5.0f/9.0f.
 
  • #10
ok so i adjusted my new code to be similar to my old one a bit and here it is:
Code:
#include <stdio.h>
#include <math.h>
float calc_celsius( float fahr, float *celc );
float calc_fahr( float celc, float *fahr );
float printAll(float f1, float f2, float f3, float c1, float c2, float c3); // last declaration

int main()
{

float c1, c2, c3, f1, f2, f3;

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

printAll(f1, f2, f3, c1, c2, c3);

return 0;

}

float calc_celsius( float fahr, float *celc )
{
*celc = (fahr - 32) * 5.0/9.0;
  return *celc;
}float calc_fahr(float celc, float *fahr)

{

*fahr = (celc + 32) * 1.8;
return;
}

float printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, calc_celsius(&f1));
printf("%.2f \t\t %.2f \n", f2, calc_celsius(&f2));
printf("%.2f \t\t %.2f \n", f3, calc_celsius(&f3));

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, calc_fahr(&c1));
printf("%.2f \t\t %.2f \n", c2, calc_fahr(&c2));
printf("%.2f \t\t %.2f \n", c3, calc_fahr(&c3));

}

and these are the errors i get
Code:
\asn44.c||In function 'printAll':|
\asn44.c|42|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|42|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|43|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|43|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|44|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|44|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|49|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|49|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
\asn44.c|50|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|50|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
\asn44.c|51|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|51|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
||=== Build failed: 12 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

i don't understand these errors..? why is it telling me incomptaible types and too few arguments?
 
  • #11
HoojaMan33 said:
ok so i adjusted my new code to be similar to my old one a bit and here it is:
Code:
#include <stdio.h>
#include <math.h>
float calc_celsius( float fahr, float *celc );
float calc_fahr( float celc, float *fahr );
float printAll(float f1, float f2, float f3, float c1, float c2, float c3); // last declaration

int main()
{

float c1, c2, c3, f1, f2, f3;

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

printAll(f1, f2, f3, c1, c2, c3);

return 0;

}

float calc_celsius( float fahr, float *celc )
{
*celc = (fahr - 32) * 5.0/9.0;
  return *celc;
}float calc_fahr(float celc, float *fahr)

{

*fahr = (celc + 32) * 1.8;
return;
}

float printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, calc_celsius(&f1));
printf("%.2f \t\t %.2f \n", f2, calc_celsius(&f2));
printf("%.2f \t\t %.2f \n", f3, calc_celsius(&f3));

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, calc_fahr(&c1));
printf("%.2f \t\t %.2f \n", c2, calc_fahr(&c2));
printf("%.2f \t\t %.2f \n", c3, calc_fahr(&c3));

}

and these are the errors i get
Code:
\asn44.c||In function 'printAll':|
\asn44.c|42|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|42|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|43|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|43|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|44|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|44|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|49|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|49|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
\asn44.c|50|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|50|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
\asn44.c|51|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|51|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
||=== Build failed: 12 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

i don't understand these errors..? why is it telling me incomptaible types and too few arguments?
One thing I don't understand is you declare these functions before the main program:
Code:
float calc_celsius( float fahr, float *celc );
float calc_fahr( float celc, float *fahr );
float printAll(float f1, float f2, float f3, float c1, float c2, float c3); // last declaration

And then you declare them again after the main program. What's the point of doing this? Most compilers get confused if there are multiple declarations of the same function.
 
  • #12
SteamKing said:
One thing I don't understand is you declare these functions before the main program:
Code:
float calc_celsius( float fahr, float *celc );
float calc_fahr( float celc, float *fahr );
float printAll(float f1, float f2, float f3, float c1, float c2, float c3); // last declaration

And then you declare them again after the main program. What's the point of doing this? Most compilers get confused if there are multiple declarations of the same function.
Unless I'm missing something, what you're calling declarations after main() are actually definitions of these functions. The declarations (AKA function prototypes) are at the top.

The main problem I see is that the functions are declared and defined with two arguments, but are called with only one. Here is an example:
C:
float calc_celsius( float fahr, float *celc );
.
.
.
// In printAll()
printf("%.2f \t\t %.2f \n", f1, calc_celsius(&f1));
The call to calc_celsius should look like this:
C:
... calc_celsius(f1, &c1)...

All of the calls to calc_celsius() need to be fixed, and all of the calls to calc_fahr() need a similar fix.

In addition, calc_Xxx() doesn't return a usable value (both should return void), so it is an error to have it as an argument in printf(). When it returns, the value at the address specified in the call to calc_Xxx() will be set to a value.

The declarations (prototypes) and definitions should look like this:
Code:
void calc_celsius(float fahr, float * cels); // Declaration
.
.
.
void calc_celsius(float fahr, float * cels)   // Definition
{
   // Body of function
}
Note that I abbreviated Celsius as cels.
 
  • #13
Ok so i got the new program to work with pointers finally. Here it is
Code:
#include <stdio.h>
#include <math.h>
void calc_celsius( float fahr, float *cels );
void calc_fahr( float cels, float *fahr );
void printAll(float f1, float f2, float f3, float c1, float c2, float c3);

int main()
{

float c1, c2, c3, f1, f2, f3;

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

printAll(f1, f2, f3, c1, c2, c3);

return 0;

}

void calc_celsius( float fahr, float *cels )
{
*cels = (fahr - 32.) * 5.0 / 9.0;
}void calc_fahr(float cels, float *fahr)

{

  *fahr = (cels * 1.8 + 32);
}

void printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

float cels, fahr;

printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
  calc_celsius(f1, &cels);
printf("%.2f \t\t %.2f \n", f1, cels);
  calc_celsius(f2, &cels);
printf("%.2f \t\t %.2f \n", f2, cels);
  calc_celsius(f3, &cels);
printf("%.2f \t\t %.2f \n", f3, cels);

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
  calc_fahr(c1, &fahr);
printf("%.2f \t\t %.2f \n", c1, fahr);
  calc_fahr(c2, &fahr);
printf("%.2f \t\t %.2f \n", c2, fahr);
  calc_fahr(c3, &fahr);
printf("%.2f \t\t %.2f \n", c3, fahr);

}

but how can i use global variables instead of using any function calls ?

i have this code which compiles fine, but it does not calculate any of my temperatures. All my outputs return as 0
Code:
#include <stdio.h>
#include <math.h>
int calc_celsius();
int calc_fahr();
void printAll();

float c1, c2, c3, f1, f2, f3;
float fahr, cels;

int main()
{

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

printAll();

return 0;

}

int calc_celsius()
{

cels = (fahr - 32) * 5/9;

}int calc_fahr()

{

 fahr = (cels * 1.8 + 32);

}

void printAll() {printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1));
printf("%.2f \t\t %.2f \n", f2, calc_celsius(f2));
printf("%.2f \t\t %.2f \n", f3, calc_celsius(f3));

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, calc_fahr(c1));
printf("%.2f \t\t %.2f \n", c2, calc_fahr(c2));
printf("%.2f \t\t %.2f \n", c3, calc_fahr(c3));}
i feel like this is due to the printAll function and how I am not using global variables properly.
 
  • #14
HoojaMan33 said:
My teacher told me to use lots of comments but i think they are very annoying...)
Informative comments are very useful to someone else reading your code, or even to you a few weeks or months after you've written your code.

HoojaMan33 said:
C:
#include <stdio.h>
#include <math.h>
float calc_celsius( float ); // prototype declarations
float calc_fahr( float ); // and again
float printAll(float f1, float f2, float f3, float c1, float c2, float c3); // last declaration
The first comment above is marginally OK, provided that you include useful comments with the definitions of these functions. The comments "// and again" and "// last declaration" are useless, as they provide no information to the reader.
HoojaMan33 said:
C:
int main()
{

float c1, c2, c3, f1, f2, f3; // declaring input variables
A comment should not restate the obvious. A better comment would state that c1, c2, c3 are intended to hold Celsius temperatures, and that f1, f2, and f3 are intended to hold Fahrenheit temperatures.
HoojaMan33 said:
C:
printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3); // storing them inside of main
What the line above is doing is reading three Fahrenheit temperatures from the console. As mentioned before, a better strategy, and one that is less confusing to a naive user, is to read these values one at a time. The only time that reading multiple values in a single scanf() call is reasonable is when you are taking input from a file, or possibly pulling data from a string (using sscanf() for example).
HoojaMan33 said:
C:
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3); // and again storing

printAll(f1, f2, f3, c1, c2, c3); // calling printAll function for the previously stored variables
Again, a comment should never merely restate what the code is doing. It's obvious that printAll() is being called. A better comment would be to state that all three F. temps and all three C. temps are to be printed.
HoojaMan33 said:
C:
return 0;
 
  • #15
Ok thanks, its just my professor said add lots of comments so i commented practically everything in hopes she would give me full marks haha.. she also said all programs have to be in her exact lay out(so i can not have the user type in 3 values separately as she doesn't have it that way)
but what about the second program?
i am trying to make a program using only global variables and no call to functions. I can get the program to compile fine but all the answers are absolute jibberish?

Code:
#include <stdio.h>
#include <math.h>
int calc_celsius();
int calc_fahr();
void printAll();

float c1, c2, c3, f1, f2, f3;
float fahr, cels;

int main()
{

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

printAll();

return 0;

}

int calc_celsius()
{

cels = (fahr - 32) * 5/9;

}int calc_fahr()

{

 fahr = (cels * 1.8 + 32);

}

void printAll() {printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, f1);
printf("%.2f \t\t %.2f \n", f2, f2);
printf("%.2f \t\t %.2f \n", f3, f3);

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, c1);
printf("%.2f \t\t %.2f \n", c2, c2);
printf("%.2f \t\t %.2f \n", c3, c3);}
is it due to my printAll function - or is it because my other functions are returning incorrect values ?
 
  • #16
HoojaMan33 said:
Ok thanks, its just my professor said add lots of comments so i commented practically everything in hopes she would give me full marks haha.. she also said all programs have to be in her exact lay out(so i can not have the user type in 3 values separately as she doesn't have it that way)
As I said earlier, it's not a good idea to input more than one variable at a time when you're taking input from the console. It's way too easy for a novice user to misunderstand what he needs to enter. I agree completely with your instructor.
HoojaMan33 said:
but what about the second program?
i am trying to make a program using only global variables and no call to functions.
Why would you write a program using only globals? This is generally not a good idea. In the code you posted below, you are using functions. Do you mean you are using functions with no parameters? That, too, is a bad idea.
HoojaMan33 said:
I can get the program to compile fine but all the answers are absolute jibberish?

Code:
#include <stdio.h>
#include <math.h>
int calc_celsius();
int calc_fahr();
void printAll();

float c1, c2, c3, f1, f2, f3;
float fahr, cels;

int main()
{

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

printAll();

return 0;

}

int calc_celsius()
{

cels = (fahr - 32) * 5/9;

}int calc_fahr()

{

fahr = (cels * 1.8 + 32);

}

void printAll() {printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, f1);
printf("%.2f \t\t %.2f \n", f2, f2);
printf("%.2f \t\t %.2f \n", f3, f3);

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, c1);
printf("%.2f \t\t %.2f \n", c2, c2);
printf("%.2f \t\t %.2f \n", c3, c3);}
is it due to my printAll function - or is it because my other functions are returning incorrect values ?
Your calc_celsius() and calc_fahr() functions are returning int values, but you are storing these values in float variables.
 
  • #17
i changed that, and still my answers are jibberish
 
  • #18
Ok, i finally got it to work. But i just have a question as this is a guideline for my program
"The first program should not use any global variables, must pass all input parameters using call-by-value parameters and must pass all function result parameters using callby-reference parameters."

Did i not do this in my first program ? is this asking me to convert 3 temperatures at once individually? (which was my initial logic)
 
  • #19
HoojaMan33 said:
Ok, i finally got it to work. But i just have a question as this is a guideline for my program
"The first program should not use any global variables, must pass all input parameters using call-by-value parameters and must pass all function result parameters using callby-reference parameters."

Did i not do this in my first program ? is this asking me to convert 3 temperatures at once individually? (which was my initial logic)
I don't see anything in the guideline that requires you to throw multiple temperatures at the conversion routines at one time. As has been indicated above, doing so makes the program less flexible.
 
  • #20
HoojaMan33 said:
Ok, i finally got it to work. But i just have a question as this is a guideline for my program
"The first program should not use any global variables, must pass all input parameters using call-by-value parameters and must pass all function result parameters using callby-reference parameters."

Did i not do this in my first program ?
You didn't do this in your first program that you showed in this thread.

Your later attempt did satisfy this requirement: "pass all input parameters using call-by-value parameters and pass all function result parameters using callby-reference parameters."
Here's a snippet of code that shows this:
C:
void calc_celsius(float fahr, float * cels)   // Definition
{
   // Body of function
}
The first parameter, fahr, is a call-by-value input parameter. The second parameter, cels, is a pointer, which is how C simulates call-by-reference parameter passing.

HoojaMan33 said:
is this asking me to convert 3 temperatures at once individually? (which was my initial logic)
No, not at all. See my explanation above.
 
  • #21
Oh ok, see the thing is my first program(the assignment which i did however long ago) didnt have this guideline for me to fulfill. This is a new assignment where i have to fulfill this condition so it is okay.

my professor deducted grades off my assignment because i did not hand trace the program ? What the heck is hand tracing ? I googled it and it looks like I am acting like the computer .. Is this a common practice in programming? It does not seem to be
 
  • #22
HoojaMan33 said:
Oh ok, see the thing is my first program(the assignment which i did however long ago) didnt have this guideline for me to fulfill. This is a new assignment where i have to fulfill this condition so it is okay.

my professor deducted grades off my assignment because i did not hand trace the program ? What the heck is hand tracing ? I googled it and it looks like I am acting like the computer .. Is this a common practice in programming? It does not seem to be
Yeah, in hand tracing, you are simulating being the computer, by keeping track of variables line by line in your code. It's a very useful technique that has been around just about forever.
Computers are very stupid, so your explanations to them (the code your write) has to be very logical. If you don't know what each line of your code is doing, in detail, it's just about impossible to write code.
 
  • #23
Ok, so i am trying to hand trace some code now and here it is:

Code:
#include <stdio.h>
/* Global variables */
int Num4, Num5, Num6, Sum, Diff;
/* Function Prototypes are declared now */
void FindDiff(int, int, int *, int *);
int FindSum(int, int, int);
void main(void)
{
 int Num1, Num2, Num3, Diff1, Sum1;
/* Executable instructions in main follow */
 scanf("%d %d %d", &Num1, &Num2, &Num3);
 scanf("%d %d %d", &Num4, &Num5, &Num6);
 FindDiff(Num1, Num2, &Diff1, &Sum1);
 printf("%d %d \n", Diff1, Sum1);
 FindDiff(Num4, Num6, &Diff1, &Sum1);
 printf("%d %d\n", Diff1, Sum1);
 Sum = FindSum(Num3, Num5, Num1);

 printf("%d %d\n", Sum, Diff);
}
int Num7 = 10;
/* The function definitions are presented next */
void FindDiff(int first, int second, int *diffp, int *sump)
{
 *diffp = (first - second) / 2;
 *sump = (first + second) + (*diffp);
 printf(" %d %d\n", *diffp, Num7);
}
int FindSum(int Num1, int Num2, int Num3)
{
 int sums;
 sums = Num1 + Num2 + Num3;
 Diff = sums * 10;
 return(sums);
}

And my question is, what output would the line
Sum = FindSum(Num 3, Num5, Num1) return?

Using the input 10, 5, 2, 3, 2, 1 respectively from num1-num6, that line prints out 14, 140

but i think it should be 15 150?
I thought that num 5 was changed at some point in the program but i can't fidn where.

Thanks.
 
  • #24
HoojaMan33 said:
Ok, so i am trying to hand trace some code now and here it is:

Code:
#include <stdio.h>
/* Global variables */
int Num4, Num5, Num6, Sum, Diff;
/* Function Prototypes are declared now */
void FindDiff(int, int, int *, int *);
int FindSum(int, int, int);
void main(void)
{
int Num1, Num2, Num3, Diff1, Sum1;
/* Executable instructions in main follow */
scanf("%d %d %d", &Num1, &Num2, &Num3);
scanf("%d %d %d", &Num4, &Num5, &Num6);
FindDiff(Num1, Num2, &Diff1, &Sum1);
printf("%d %d \n", Diff1, Sum1);
FindDiff(Num4, Num6, &Diff1, &Sum1);
printf("%d %d\n", Diff1, Sum1);
Sum = FindSum(Num3, Num5, Num1);

printf("%d %d\n", Sum, Diff);
}
int Num7 = 10;
/* The function definitions are presented next */
void FindDiff(int first, int second, int *diffp, int *sump)
{
*diffp = (first - second) / 2;
*sump = (first + second) + (*diffp);
printf(" %d %d\n", *diffp, Num7);
}
int FindSum(int Num1, int Num2, int Num3)
{
int sums;
sums = Num1 + Num2 + Num3;
Diff = sums * 10;
return(sums);
}

And my question is, what output would the line
Sum = FindSum(Num 3, Num5, Num1) return?

Using the input 10, 5, 2, 3, 2, 1 respectively from num1-num6, that line prints out 14, 140

but i think it should be 15 150?
I thought that num 5 was changed at some point in the program but i can't fidn where.

Thanks.
Your program is a complicated mess.
  • You are using global variables. Why?
  • You are still entering three values at a time, in spite of the advice given here, and by your instructor.
  • Your two functions don't do what their names suggest. FindSum should take two numbers and calculate their sum. It should not have a variable named Diff in its definition. If you need to find the sum of three numbers, call FindSum() twice.
    FindDiff() should take two numbers and return their difference. It should not do output. And why are you printing Num7 in FindDiff()?
The problem you are having here is exactly why you should be tracing your code by hand.
 
  • #25
It is not my code, it is code provided by my professor that she wants you to trace

I think she wants to add complications to see how well we can understand how C functions
 
  • #26
HoojaMan33 said:
but i think it should be 15 150?
Why do you think that?

HoojaMan33 said:
I thought that num 5 was changed at some point in the program but i can't fidn where.
The variable Num5 appears 3 times in that code. It is not difficult to check if it gets modified or not.
 

1. How do I convert Fahrenheit to Celsius using a C program?

To convert Fahrenheit to Celsius in a C program, you can use the formula (°F - 32) * 5/9 = °C. This formula takes the temperature in Fahrenheit, subtracts 32, and then multiplies by 5/9 to get the equivalent temperature in Celsius.

2. How do I convert Celsius to Fahrenheit using a C program?

To convert Celsius to Fahrenheit in a C program, you can use the formula (°C * 9/5) + 32 = °F. This formula takes the temperature in Celsius, multiplies by 9/5, and then adds 32 to get the equivalent temperature in Fahrenheit.

3. Can I use functions in my C program to convert temperature?

Yes, you can use functions in your C program to convert temperature. By creating separate functions for the conversion formulas, you can easily call them in your main program and use them multiple times.

4. How can I make my C program convert both Fahrenheit and Celsius?

To make your C program convert both Fahrenheit and Celsius, you can create a menu that allows the user to choose which conversion they want to perform. Then, based on their selection, you can call the appropriate conversion function in your main program.

5. Is there a library in C that can handle temperature conversion?

Yes, there is a library in C called stdlib.h that contains the function atof() which can be used to convert temperature from one unit to another. However, it is still important to understand the conversion formulas and how to write your own functions for temperature conversion in case you do not have access to this library.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
4K
  • Biology and Chemistry Homework Help
Replies
6
Views
1K
Replies
2
Views
2K
  • Programming and Computer Science
Replies
21
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
Back
Top