Given two integer arrays a[] and b[] of n elements, compute a third array c[] where each element is c[i] = a[i] + b[n-1-i]. The key insight is the index n-1-i: when i=0 it selects the last element of b, when i=n-1 it selects the first. This “reverse pairing” adds each element of a to the corresponding element of b read in reverse order. The original post contained only a dead Gist shortcode with no rendered code; this is a complete C89 implementation.
How It Works (n=4 example)
| i | a[i] | n-1-i | b[n-1-i] | c[i] |
|---|---|---|---|---|
| 0 | 1 | 3 | 8 | 9 |
| 1 | 2 | 2 | 7 | 9 |
| 2 | 3 | 1 | 6 | 9 |
| 3 | 4 | 0 | 5 | 9 |
a = [1,2,3,4], b = [5,6,7,8] → c = [9,9,9,9] ✓
C Program: c[i] = a[i] + b[n-1-i]
/* Given arrays a[] and b[] of n elements, compute c[i] = a[i] + b[n-1-i]
* Compile: gcc -ansi -Wall -Wextra arrayc.c -o arrayc */
#include <stdio.h>
#define MAX 20
int main(void)
{
int a[MAX], b[MAX], c[MAX];
int n, i;
printf("How many elements (1-%d)? ", MAX);
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
printf("Enter a number between 1 and %d.\n", MAX);
return 1;
}
printf("Enter %d elements for array a:\n", n);
for (i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1) { printf("Invalid.\n"); return 1; }
}
printf("Enter %d elements for array b:\n", n);
for (i = 0; i < n; i++) {
if (scanf("%d", &b[i]) != 1) { printf("Invalid.\n"); return 1; }
}
for (i = 0; i < n; i++)
c[i] = a[i] + b[n - 1 - i];
printf("\ni a[i] b[n-1-i] c[i]\n");
printf("--- ---- -------- ----\n");
for (i = 0; i < n; i++)
printf("%3d %4d %8d %4d\n", i, a[i], b[n - 1 - i], c[i]);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra arrayc.c -o arrayc
./arrayc
Sample Output
How many elements (1-20)? 4 Enter 4 elements for array a: 1 2 3 4 Enter 4 elements for array b: 5 6 7 8 i a[i] b[n-1-i] c[i] --- ---- -------- ---- 0 1 8 9 1 2 7 9 2 3 6 9 3 4 5 9
How many elements (1-20)? 3 Enter 3 elements for array a: 10 20 30 Enter 3 elements for array b: 1 2 3 i a[i] b[n-1-i] c[i] --- ---- -------- ---- 0 10 3 13 1 20 2 22 2 30 1 31
Code Explanation
- Index formula
n-1-i— when i=0,n-1-i = n-1(the last element of b). When i=n-1,n-1-i = 0(the first element). This maps forward iteration through a[] to reverse iteration through b[]. No reversal of b is needed — the formula handles it implicitly. - Three separate arrays — a[], b[], c[] are all declared. Writing the result into c[] keeps a[] and b[] intact, which allows the table to print both b[n-1-i] and c[i] in the same row. If you modified b[] in place it would corrupt later outputs.
- Tabular output — printing i, a[i], b[n-1-i], and c[i] side by side makes it easy to verify each computation. The printf format
%3d,%4d,%8dright-aligns the columns for readability.
What This Program Teaches
- Index arithmetic —
n-1-iis the pattern for “mirror index” — element i from the front corresponds to element n-1-i from the back. The same formula appears in array reversal, palindrome checking, and matrix transposition. - Zip-with-reversed — pairing each element of one sequence with the corresponding element of another sequence (possibly reversed) is a fundamental functional programming concept. In C, it is expressed as a loop with two index expressions.
- Preserving input arrays — writing results to a third array is cleaner than modifying the input. It follows the principle of non-destructive computation: the caller can inspect both inputs and the output after the function returns.
Related Programs
- Array Summation Using Pointers in C
- Sum of Even and Odd Numbers in C
- Palindrome String Check in C
- Average of the Two Largest in an Array in C
- Linear Search in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.