Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 17th, 2013, 10:54 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Function help

Help me write a function that converts a temperature measurement from celsius to fahrenheit. The formula is, Celsius = (5/9)(Fahrenheit -32).
Here's my attempt:
Code:
```int convert(int i)
{
int p;
p = ((9i/5) + 32);
return p;
}```

#2
June 17th, 2013, 10:58 AM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 152
Time spent in forums: 1 Week 7 h 24 m 27 sec
Reputation Power: 18

Jim

#3
June 17th, 2013, 11:00 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Quote:
 Originally Posted by jimblumberg And do you have a problem with your attempt? If so please ask specific questions. Jim

Yes I am having a problem. Here's the entire code:
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%d %d\n", i, convert(i));
return 0;
}
int convert(int i)
{
int p;
p = ((9i/5) + 32);
return p;
}```

I only get a series of 32.

#4
June 17th, 2013, 11:08 AM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 152
Time spent in forums: 1 Week 7 h 24 m 27 sec
Reputation Power: 18
Let's start by looking at the following line:
Code:
`p = ((9i/5) + 32);`

First in C/C++ and most other programming languages you must specify the operand, (9 * i).

Next this is integer math, so remember there are no fractions. 9/5 will yield 1. To get fractional amounts you mus use floating point numbers.

Jim

#5
June 17th, 2013, 11:15 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Quote:
 Originally Posted by jimblumberg Let's start by looking at the following line: Code: `p = ((9i/5) + 32);` First in C/C++ and most other programming languages you must specify the operand, (9 * i). Next this is integer math, so remember there are no fractions. 9/5 will yield 1. To get fractional amounts you mus use floating point numbers. Jim

Here's my revised code:
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
int convert(int i)
{
float p;
p = ((9*i/5) + 32);
return p;
}```

Now all I get is a series of 0.000s

#6
June 17th, 2013, 11:22 AM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 152
Time spent in forums: 1 Week 7 h 24 m 27 sec
Reputation Power: 18
Your function is returning an integer, but your printf() is trying to print a floating point number. The printf() function requires that the format specifier match the variable type. If they don't match you won't get the expected results.

I really suggest you change your function to return a floating point number and use floating point numbers in that function. Remember to also change your constants to floating point by specifying the value with a decimal point (9.0).

Jim

#7
June 17th, 2013, 11:22 AM
 ptr2void
I haz teh codez!

Join Date: Dec 2003
Posts: 2,530
Time spent in forums: 1 Month 2 Weeks 3 Days 18 h 49 m 46 sec
Reputation Power: 2336
You skipped part of jimblumberg's response. I'll requote it for you.

Quote:
 Next this is integer math, so remember there are no fractions. 9/5 will yield 1. To get fractional amounts you mus use floating point numbers.

Change 5 to 5.0 and see what happens.
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

#8
June 17th, 2013, 11:29 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Quote:
 Originally Posted by jimblumberg Your function is returning an integer, but your printf() is trying to print a floating point number. The printf() function requires that the format specifier match the variable type. If they don't match you won't get the expected results. I really suggest you change your function to return a floating point number and use floating point numbers in that function. Remember to also change your constants to floating point by specifying the value with a decimal point (9.0). Jim

If I try to compile this:
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```

it says "Conflicting types for 'convert'" for
Code:
`float convert(float i)`

and, "previous implicit declaration of 'convert' was here" for
Code:
`printf("%6.3f %6.3f\n", i, convert(i));`

#9
June 17th, 2013, 11:31 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Sorry if I sound too dumb. I am new in this field.

#10
June 17th, 2013, 11:38 AM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 152
Time spent in forums: 1 Week 7 h 24 m 27 sec
Reputation Power: 18
Your function needs a function prototype before main(). Remember your functions and variables must be defined before they can be used.

Also look at this snippet:
Code:
```	int i;
...
printf("%6.3f %6.3f\n", i, convert(i));```

What would be the proper format specifier for i, which happens to be an int? Remember integers don't have fractional parts and that printf() insists that the format string match the variables.

Jim
arman.khandaker agrees: Thanks a tonne for giving me time!

#11
June 17th, 2013, 11:46 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1

Quote:
 Originally Posted by jimblumberg Your function needs a function prototype before main(). Remember your functions and variables must be defined before they can be used. Also look at this snippet: Code: ``` int i; ... printf("%6.3f %6.3f\n", i, convert(i));``` What would be the proper format specifier for i, which happens to be an int? Remember integers don't have fractional parts and that printf() insists that the format string match the variables. Jim

The code finally worked!
Code:
```#include <stdio.h>

float convert(float i)
{
float p;
p = ((5.0/9.0)*(i - 32.0));
return p;
}

main()
{
float i;
for (i = 0; i <= 300; ++i)
printf("%1.1f %1.1f\n", i, convert(i));
return 0;
}```

I also changed the formula.
Thanks a lot for helping me around!

#12
June 17th, 2013, 11:49 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,871
Time spent in forums: 3 Months 1 Day 8 h 57 m 46 sec
Reputation Power: 2199
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```

Look at the warnings that you get:

Quote:
 C:>gcc -Wall convert.c convert.c:4: warning: return-type defaults to `int' convert.c: In function `main': convert.c:7: warning: implicit declaration of function `convert' convert.c:7: warning: double format, different type arg (arg 2) convert.c:7: warning: double format, different type arg (arg 3) convert.c: At top level: convert.c:11: warning: type mismatch with previous implicit declaration convert.c:7: warning: previous implicit declaration of `convert' convert.c:11: warning: `convert' was previously implicitly declared to return `int' C:>

convert.c:7: warning: implicit declaration of function `convert'
The compiler reads from the top of the file to the bottom. When it tries to call convert(), it has no idea what you are talking about because you haven't told it anything about convert. Old legacy behavior causes C to assume that you're talking about a function that takes one int argument and returns an int. That assumption caused the following warnings:
Quote:
 convert.c:7: warning: double format, different type arg (arg 3) convert.c: At top level: convert.c:11: warning: type mismatch with previous implicit declaration convert.c:7: warning: previous implicit declaration of `convert' convert.c:11: warning: `convert' was previously implicitly declared to return `int'

When the compiler reached the actual declaration for convert, that actual declaration conflicted with the assumed implicit declaration you had forced on the compiler previously, so naturally the compiler complained.

You need to either move convert above main or else you need to place a function prototype before main; eg:
Code:
```#include <stdio.h>

float convert(float i);

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```

Please pay especial attention to the fact that the function prototype ends in a semicolon, but the actual function itself does not have a semicolon there.

convert.c:7: warning: double format, different type arg (arg 2)
is because i is an int but you're telling printf to treat it like a floating-point value. That will cause really weird results. It's an int, so treat it like an int!

convert.c:4: warning: return-type defaults to `int'
What's the return type of main? It should be int, so why don't you declare it as int? That's that old legacy defaulting undeclared identifiers as int that created a generation of lazy C programmers. Do not be lazy! Declare main to be int!

#13
June 17th, 2013, 11:53 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1

Quote:
 Originally Posted by dwise1_aol Code: ```#include main() { int i; for (i = 0; i <= 300; ++i) printf("%6.3f %6.3f\n", i, convert(i)); return 0; } float convert(float i) { float p; p = ((9.0*i/5.0) + 32.0); return p; }``` Look at the warnings that you get: convert.c:7: warning: implicit declaration of function `convert' The compiler reads from the top of the file to the bottom. When it tries to call convert(), it has no idea what you are talking about because you haven't told it anything about convert. Old legacy behavior causes C to assume that you're talking about a function that takes one int argument and returns an int. That assumption caused the following warnings: When the compiler reached the actual declaration for convert, that actual declaration conflicted with the assumed implicit declaration you had forced on the compiler previously, so naturally the compiler complained. You need to either move convert above main or else you need to place a function prototype before main; eg: Code: ```#include float convert(float i); main() { int i; for (i = 0; i <= 300; ++i) printf("%6.3f %6.3f\n", i, convert(i)); return 0; } float convert(float i) { float p; p = ((9.0*i/5.0) + 32.0); return p; }``` Please pay especial attention to the fact that the function prototype ends in a semicolon, but the actual function itself does not have a semicolon there. convert.c:7: warning: double format, different type arg (arg 2) is because i is an int but you're telling printf to treat it like a floating-point value. That will cause really weird results. It's an int, so treat it like an int! convert.c:4: warning: return-type defaults to `int' What's the return type of main? It should be int, so why don't you declare it as int? That's that old legacy defaulting undeclared identifiers as int that created a generation of lazy C programmers. Do not be lazy! Declare main to be int!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Function help