PART A · PRESCRIBED EXERCISE 06
Three-subject results using one-dimensional arrays
Understand the algorithm. Run the code.
Explain the result with confidence.
LAB A06 · 01
Understand the task.
Aim: Three-subject results using one-dimensional arrays.
One-dimensional result arrays. A one-dimensional array stores a sequence of same-type elements accessed with one index. Parallel arrays use the same index to represent different fields of one logical record.
Review the related theory in Unit 3.
LAB A06 · 02
Know what goes in and what comes out.
First n, a student count from 1 to 20. Then n groups of three integer subject marks, each 0–100. Practice result rule: PASS only when all three marks are at least PASS_MARK, set to 40.
Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.
LAB A06 · 03
Plan the steps before writing code.
- Read and validate n.
- Store the three marks for student i in first[i], second[i] and third[i].
- For each student, total the three corresponding elements.
- Compute average = total / 3.0.
- Check every subject against PASS_MARK and print total, average and result.
LAB A06 · 04
One complete program. Ready to run.
#include <stdio.h>
#define MAX_STUDENTS 20
#define PASS_MARK 40
int main(void)
{
int first[MAX_STUDENTS], second[MAX_STUDENTS], third[MAX_STUDENTS];
int n, i, total, passed;
double average;
if (scanf("%d", &n) != 1 || n < 1 || n > MAX_STUDENTS) {
puts("Enter a student count from 1 to 20.");
return 1;
}
for (i = 0; i < n; i++) {
if (scanf("%d%d%d", &first[i], &second[i], &third[i]) != 3 ||
first[i] < 0 || first[i] > 100 || second[i] < 0 || second[i] > 100 ||
third[i] < 0 || third[i] > 100) {
puts("Enter three marks from 0 to 100 for each student.");
return 1;
}
}
for (i = 0; i < n; i++) {
total = first[i] + second[i] + third[i];
average = total / 3.0;
passed = first[i] >= PASS_MARK && second[i] >= PASS_MARK && third[i] >= PASS_MARK;
printf("Student %d: total = %d, average = %.2f, result = %s\n",
i + 1, total, average, passed ? "PASS" : "FAIL");
}
return 0;
}
Student 1: total = 240, average = 80.00, result = PASS
Student 2: total = 120, average = 40.00, result = PASS
Student 3: total = 220, average = 73.33, result = FAIL
Compile on your own computer
gcc A06-student-results.c -o programRun the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.
LAB A06 · 05
Connect each statement to its purpose.
- Three separate 1D arrays meet the prescribed one-dimensional-array requirement. A marks[n][3] declaration would instead be a 2D array.
- The same index i always refers to the same student across the three arrays.
- Using 3.0 produces a fractional average.
- A high average does not compensate for a failed subject under this stated practice rule. Change PASS_MARK and the condition if your prescribed policy differs.
LAB A06 · 06
Follow the sample calculation.
| Step or component | What happens |
|---|---|
| Student 1 | 80 + 70 + 90 = 240; average 80.00; all subjects >= 40 → PASS |
| Student 2 | 40 + 40 + 40 = 120; average 40.00 → PASS |
| Student 3 | 95 + 35 + 90 = 220; average 73.33; second subject < 40 → FAIL |
LAB A06 · 07
Check more than one input.
| Try this input or case | Expected behaviour |
|---|---|
| 1 student: 0 0 0 | Average 0.00; FAIL. |
| 1 student: 100 100 100 | Average 100.00; PASS. |
| n = 0 or n = 21 | Rejected before array access. |
Time and storage
O(n) time. Three arrays hold O(n) logical data, with a fixed capacity of 20 students in this program.
Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.
LAB A06 · 08
Use the real C compiler.
Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.
Load the compiler when you’re ready to practise.
Your C workspace
Edit real C code and see real compiler output.
Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.
View / copy the selected source
#include <stdio.h>
#define MAX_STUDENTS 20
#define PASS_MARK 40
int main(void)
{
int first[MAX_STUDENTS], second[MAX_STUDENTS], third[MAX_STUDENTS];
int n, i, total, passed;
double average;
if (scanf("%d", &n) != 1 || n < 1 || n > MAX_STUDENTS) {
puts("Enter a student count from 1 to 20.");
return 1;
}
for (i = 0; i < n; i++) {
if (scanf("%d%d%d", &first[i], &second[i], &third[i]) != 3 ||
first[i] < 0 || first[i] > 100 || second[i] < 0 || second[i] > 100 ||
third[i] < 0 || third[i] > 100) {
puts("Enter three marks from 0 to 100 for each student.");
return 1;
}
}
for (i = 0; i < n; i++) {
total = first[i] + second[i] + third[i];
average = total / 3.0;
passed = first[i] >= PASS_MARK && second[i] >= PASS_MARK && third[i] >= PASS_MARK;
printf("Student %d: total = %d, average = %.2f, result = %s\n",
i + 1, total, average, passed ? "PASS" : "FAIL");
}
return 0;
}
LAB A06 · 09
Explain why the program works.
Why use the same index in every array?
It keeps subject marks belonging to one student associated correctly.
Can average alone decide PASS here?
No. Every individual subject must reach the stated minimum.
Why number students with i + 1?
Array indexes start at zero; the displayed student number is conventionally one-based.
LAB A06 · 10
Build the next skill.
Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.