---
title: "Correct program: case array and average"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:correct-program-case-array"
source: https://www.bctengineer.com/q/semester_1/computer_programming/correct-program-case-array
site: BCT Engineering
---

# Correct program: case array and average

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

- Asked in: 2074 Asoj, 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-case-array
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

Rewrite the following program by correcting any errors, if present and also write down the output of the corrected code.
```
Define MAX '5'
int main ( )
{
  int case[MAX]={2,3,5,4,10},i,sum=0;
  for(i=0,i<MAX,i+=1)
  {
    printf("Case %d = %3.2d\n",i,case[i]);
    sum +=* case+i;
  };
  average = sum/MAX;
  printf("%06.2f",average);
  return 1;
}
```

## Short answer

Correct: #include <stdio.h>, #define MAX 5, rename array to arr, fix for‑loop semicolons, sum+=arr[i], declare float average, return 0. Array sum = 24; integer division 24/5 = 4, printed as 004.00. Output shows each Case line and final 004.00.

## Answer

#### Errors found
- `Define MAX '5'` should be `#define MAX 5`; also `'5'` is a character constant (value 53), not 5.
- `#include <stdio.h>` is missing, needed for `printf`.
- `case` is a keyword, so it cannot be an array name; rename it `arr`.
- The `for` header uses commas; it needs semicolons: `for(i=0; i<MAX; i+=1)`.
- `sum +=* case+i` is invalid; it should be `sum += arr[i]`.
- `average` is not declared; declare it as `float`.
- The `;` after the closing brace of the loop is unnecessary.

#### Corrected code
```c
#include <stdio.h>
#define MAX 5
int main(void)
{
    int arr[MAX] = {2, 3, 5, 4, 10}, i, sum = 0;
    float average;
    for (i = 0; i < MAX; i += 1)
    {
        printf("Case %d = %3.2d\n", i, arr[i]);
        sum += arr[i];
    }
    average = sum / MAX;
    printf("%06.2f", average);
    return 0;
}
```

#### Output
```text
Case 0 =  02
Case 1 =  03
Case 2 =  05
Case 3 =  04
Case 4 =  10
004.00
```

#### Explanation
- `%3.2d` prints each integer with at least 2 digits (a leading zero is added) inside a width of 3, so 2 prints as ` 02`.
- `sum = 2+3+5+4+10 = 24`. Since `sum` and `MAX` are both `int`, `sum/MAX` is integer division: 24/5 = 4. It is stored in the float `average` as 4.0.
- `%06.2f` prints it in width 6, 2 decimals, padded with zeros: `004.00`.
- If `(float)sum/MAX` is used instead, the average is 4.8 and the output becomes `004.80`.

## Check yourself

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

1. case cannot be an array name because
   A. it is a keyword
   B. it is too short
   C. it is a number
   D. it is a macro
   Answer: A. Rename to arr. (Key point: Keyword)

2. Correct form is
   A. Define MAX '5'
   B. #define MAX 5
   C. #define MAX = '5'
   D. define MAX 5;
   Answer: B. '5' is char 53. (Key point: Define)

3. sum/MAX with sum=24, MAX=5 gives
   A. 4.8
   B. 5
   C. 4
   D. 24
   Answer: C. Integer division. (Key point: Integer division)

4. %06.2f prints 4.0 as
   A. 4.00
   B. 000004
   C. 4.0
   D. 004.00
   Answer: D. Width 6 zero padded. (Key point: Print)

## 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: Celsius to Fahrenheit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-celsius-fahrenheit) — Computer Programming, chapter 3
- [Output of printf statements](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aprintf-output-a-b) — Computer Programming, chapter 4
- [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
- [Pass 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-array-largest-smallest) — Computer Programming, chapter 7

## Related in other subjects

- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
- [Trellis decoding of 1/2 rate conv. code (3-bit errors)](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Aconv-trellis-3bit) — Data Communication, chapter 8, asked 2×
- [Trellis decoding of 1/2 rate conv. code (2-bit errors)](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Aconv-trellis-2bit) — Data Communication, chapter 8, asked 2×
- [Step-down chopper with 1 V drop: average and rms](https://www.bctengineer.com/question.md?key=semester_7%2Fpower_electronics%3Astep-down-chopper-1v-drop-rms) — Power Electronics, chapter 4, asked 2×
