- 2069 Asar · 4+4 marks
Correct program: Celsius to Fahrenheit
Short answer
Errors include missing <stdio.h>, missing function declaration and return type, integer division 9/5, missing & in scanf, print to printf, break outside loop. With inputs 0, 100, 37, the corrected program outputs 32.00, 212.00, 98.60.
Rewrite the following source code correcting any error present in it. Also indicate the error corrected in comment. Then write the output of the program.
//program to convert list of temperatures
//in Centigrade to Fahrenheit
#include stdio.h;
int main()
{
int i, n=3;
float celc[n], faren[n];
for(i=0; i<n; i++)
{
print ("Celc[%d]=")
scanf("%d", celc[i]);
}
convert(celc, faren, n);
for(i=0; i<n; i++) print(faren[i]);
break;
};
void convert(float cel[], float far[], int n)
{
for(i=0; i<n; i++) cel2far(far[i], cel[i]);
};
cel2far(float f, float c)
{ f = 9/5*c+32; };
Corrected code (errors marked in comments)
//program to convert list of temperatures
//in Centigrade to Fahrenheit
#include <stdio.h> /* error 1: header must be #include <stdio.h> without ';' */
void convert(float cel[], float fah[], int n); /* error 2: prototype added (function used before definition) */
float cel2far(float c); /* error 3: return type was missing */
int main()
{
int i, n = 3;
float celc[3], faren[3]; /* error 4: array size must be constant in C89 (variable n not allowed) */
for (i = 0; i < n; i++)
{
printf("Celc[%d]=", i); /* error 5: print -> printf and missing ';' */
scanf("%f", &celc[i]); /* error 6: %d -> %f and & added */
}
convert(celc, faren, n);
for (i = 0; i < n; i++)
printf("%.2f\n", faren[i]); /* error 7: printf needs a format string */
return 0; /* error 8: break outside loop replaced by return */
} /* error 9: stray ';' after function body removed */
void convert(float cel[], float fah[], int n)
{
int i; /* error 10: i was not declared */
for (i = 0; i < n; i++)
fah[i] = cel2far(cel[i]); /* error 11: result must be returned/stored, not passed by value */
}
float cel2far(float c)
{
return 9.0 / 5 * c + 32; /* error 12: 9/5 is integer division = 1; use 9.0/5 */
}
Output
For the inputs 0, 100 and 37:
Celc[0]=0
Celc[1]=100
Celc[2]=37
32.00
212.00
98.60
Explanation
- Each temperature is converted by F = 9/5 × C + 32: 0 gives 32, 100 gives 212 and 37 gives 98.6.
- In the original,
f = 9/5*c+32changes only a copy of the variable (call by value), so the result never reached the array; the function must return the value. 9/5is integer division (= 1);9.0/5gives 1.8.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Correct program: do-while and pow · Computer Programming, chapter 3
- Correct program: case array and average · Computer Programming, chapter 3
- Correct program: character arrays · Computer Programming, chapter 7
- Output of arithmetic expression program · Computer Programming, chapter 3
- Need of stdio.h header file · Computer Programming, chapter 3
- Cube of int, float, double by overloading · Object Oriented Programming, chapter 3