---
title: "Correct program: Celsius to Fahrenheit"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:correct-program-celsius-fahrenheit"
source: https://www.bctengineer.com/q/semester_1/computer_programming/correct-program-celsius-fahrenheit
site: BCT Engineering
---

# Correct program: Celsius to Fahrenheit

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2069 Asar, 4+4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/correct-program-celsius-fahrenheit
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

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;  };
```

## 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.

## Answer

#### Corrected code (errors marked in comments)
```c
//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:
```text
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+32` changes only a copy of the variable (call by value), so the result never reached the array; the function must return the value.
- `9/5` is integer division (= 1); `9.0/5` gives 1.8.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why did the original cel2far(float f, float c) with f = 9/5*c+32 never fill the faren array?
   A. The function changes only a copy of f, so the result must be returned
   B. Arrays cannot be passed to functions
   C. The function has no semicolon
   D. f was declared as float instead of int
   Answer: A. Arguments are passed by value, so assigning to f changes a local copy. The fixed version returns the value and stores it in fah[i]. (Key point: Call by value)

2. In the formula, why must 9/5 be written as 9.0/5?
   A. Because 9/5 is integer division and gives 1
   B. Because 9/5 gives 2
   C. Because float cannot divide by integers
   D. Because 9/5 gives a compile error
   Answer: A. Both operands are int, so 9/5 truncates to 1 and the conversion would be wrong. 9.0/5 gives 1.8. (Key point: Integer division)

3. What is the correct scanf call to read a float celc[i]?
   A. scanf("%d", celc[i]);
   B. scanf("%f", celc[i]);
   C. scanf("%f", &celc[i]);
   D. scanf("%d", &celc[i]);
   Answer: C. A float needs %f and scanf needs the address, so &celc[i]. (Key point: scanf for float)

4. For inputs 0, 100 and 37 the converted values printed are:
   A. 32.00, 100.00, 37.00
   B. 32.00, 212.00, 98.60
   C. 0.00, 180.00, 67.00
   D. 32.00, 212.00, 66.60
   Answer: B. F = 9/5*C + 32 gives 32, 212 and 98.6. (Key point: Output values)

## Related in Computer Programming

- [Correct program: do-while and pow](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-sum-root) — Computer Programming, chapter 3
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Output of arithmetic expression program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoutput-expression-xyz) — Computer Programming, chapter 3
- [Need of stdio.h header file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astdio-header-need) — Computer Programming, chapter 3

## Related in other subjects

- [Cube of int, float, double by overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acube-overloaded-program) — Object Oriented Programming, chapter 3
