You are here:

C/pls kindly point out the error in this programme i wrote

Advertisement


Question
QUESTION: the purpose of this programme i wrote was to write a programme that contains the function called fahrenheit. I wanted to have this function generate a series of temperature conversion from celcius to fahrenheit using the formula: 32+celcius * 9/5

CODE:

#include <stdio.h>

double fahrenheit (void)    /*function prototype*/

int main(void)

{

  int celcius;
  printf("the temperature in celcius is %d:"  
 ,fahrenheit());         /*will this affect my programme?
         bcuz printf statement went to
         the next line.*/

 return 0;
}

double fahrenheit(void)
{
 int celcius;

 printf("enter temperature to convert to fahrenheit");
 scanf("%d",&celcius);

 fahrenheit = 32 + (double)celcius * 1.8 ;
 return fahrenheit;
 }

Pls, i want you to make corrections to this programme but dont write it in another way. i know it has to do with my variable declaration. that is where to declare either global or local variable but i need a clearer understanding by inserting it into this programme. Just take alook at the programme and point out why it didn't compile properly. Hope to hear again from you and thanks for the past.

ANSWER: The mistake were:
1. The function declaration had a semecolon missing in the end!
2. Name of a variable and function were same. They have to be different.

So, here is the corrected & compiling code.
But, I haven't looked into the logic & working of the code.

#include <stdio.h>

double value_of_fahrenheit (void);    /*function prototype*/

int main(void)
{

 int celcius;
 printf("the temperature in celcius is %d:"
,value_of_fahrenheit());         /*will this affect my programme?
         bcuz printf statement went to
         the next line.*/

return 0;
}

double value_of_fahrenheit(void)
{
int celcius;
double fahrenheit;

printf("enter temperature to convert to fahrenheit");
scanf("%d",&celcius);

fahrenheit = 32 + (double)celcius * 1.8 ;
return fahrenheit;
}

---------- FOLLOW-UP ----------

QUESTION: This programme is similar to the one you just explained but yet it will not compile properly! could you please take a closer look at this programme and compare it with the previous one i asked. that way, i am trying to get a major difference bcuz they both look similar and i have been trying to find out why this one isn't working.

CODE:

# include <stdio.h>

int series(void);

int main(void)
{

  int i;
  for(i = 0; i<10; i++) printf("%d" , series());

  return 0;
}

 /*this is incorrect*/ why is this so?

 int series(void)
 {

   int total;

  total = (total + 1423) % 1422;

  return 0;
 }

NOTE: I read this from the text but don't understand it.
//
This programme attemps to use series() to generate a number series in which each number is based upon the value of the preceding one. However, the value total will not be maintained between function calls, and the function fails to carry out its intended task. //

Now, using the previous similar fahrenheit conversion programe, could u correct this series() programme and stating explicitly what the NOTE statement means as concerened to this programme with detailed explanation using the fahrenheit conversion programme as a reference point?

I need this to be so sure of the use of variables as regarding function calls. Thanks alot for your assistance.

Answer
The corrected program is here:
# include <stdio.h>

int series(void);

int main(void)
{

 int i;
 for(i = 0; i<10; i++) printf("%d" , series());

 return 0;
}

/*this is incorrect why is this so?*/

int series(void)
{

  int total;

 total = (total + 1423) % 1422;

 return 0;
}

The only mistake was in the line:
/*this is incorrect*/ why is this so?
The comments start with '/*' and end with '*/'.
So, after correction it will look like this:
/*this is incorrect why is this so?*/

And your program statement says:
//
This programme attemps to use series() to generate a number series in which each number is based upon the value of the preceding one.
//
But the problem is:
//
However, the value total will not be maintained between function calls, and the function fails to carry out its intended task. //

The reason for this is:
The function series always returns 0.
Instead it should return the value total.
But, with every call, it should know what was the previous value of total. So, it should be declared as a static value.
After these two corrections, your program will become:
#include <stdio.h>

int series(void);

int main(void)
{       
       int i;

       for(i = 0; i<10; i++) printf("%d" , series());

       return 0;
}       

/*this is incorrect why is this so?*/

int series(void)
{
       static int total;

       total = (total + 1423) % 1422;

       return total;
}

Compile this and run and you will see what you are expecting.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.