--- In [email protected], "mdnizdil" <[EMAIL PROTECTED]> wrote:
>
> Hi..
> Plz help me to solve this problem. This is about functions.
>
> 1. Write a program to print the following table.
>
> Input lenght in inches & output should be in lenght in
> mm,metre,feet. Have to use functions.
> The prototype should be
>
> double mm(double inches);
> double metre(double mm);
> double feet(double inches);
>
> ........................................................
> Inches mm m feet
> 10 .... .... ....
> 20 .... .... ....
> .
> .
> .
> 100 .... .... ....
>
> ..........................................................
>
> I wrote a program for this. But it gives errors. Can you
> correct my programme.
No, but I'll give you a few starting points; there are many.
> #include<iostream>
Insert a blank between #include and <iostream> .
> using namespace std;
>
> double mm(double inches);
> double metre(double mm);
> double feet(double inches);
>
> int main()
> {
> double inches,mm,metre,feet;
In general it's not a good idea to have variables with the same names
as functions. Invent a system for yourself how constants, variables,
and functions are named; this system should make it obvious at first
sight which identifier is what sort of entity: a constant, a variable,
a function. For example, you can define for yourself that constants
have always names in all uppercase (#define MIN_INCHES 10); that all
variables start with an uppercase letter and have mixed uppercase and
lowercase letters; and that all function names start with a lowercase
letter. [A variation of this scheme is used in Java.]
> cout<<"mm"<<mm<<endl;
> cout<<"metre"<<metre<<endl;
> cout<<"feet"<<feet<<endl;
This would be the way to output these variables to the screen. But
first you have to SET these variables to some useful value.
> for(int i=0; i<=100; i+10)
> cout<<i<<" "<<endl;
This "for" loop is used to print out all the values of inches and
corresponding m, mm, and feet numbers. You better use a loop like this:
// Before declaring the functions, insert these constant values
// in your program:
#define MIN_INDEX 10
#define MAX_INDEX 100
#define INDEX_INC 10
...
for (int i = MIN_INDEX; i <= MAX_INDEX; i += INDEX_INC)
{ inches = (double) i;
// convert "inches" to "m"
// convert "inches" to "mm"
// convert "inches" to "feet"
cout << index << " " << m << " " << mm << " " << feet << endl;
}
> return 0;
>
> }
>
> double mm(double inches)
> double metre(double mm)
> double feet(double inches)
> {
> double mm,metre,feet,inches;
>
> mm=2.54*inches;
> metre=inches/12;
> feet=mm/1000;
>
> }
You have to define every function on its own, and you use "return" to
give back some value from the called function to the caller. So you
should write your code something like this:
double mm( double inches)
{
return inches * 25.4;
} // double mm()
double m( double inches)
{
return inches * 0.0254;
} // double m
double feet( double inches)
{
return inches / 12.0;
} // double feet
Furthermore, as mentioned above, it's not good practice to give
variables and functions identical names. While it might work with a
C++ compiler, it will never work with a C compiler. Not to forget that
it makes your code extremely difficult to understand and maintain.
Instead I propose you name your conversion functions explicitly as
such, for example:
double inchToFeet( double inches)
{
return inches / 12.0;
} // double inchToFeet()
Regards,
Nico