merge 2 arrays, sum of integers from front & back
Sun Aug 25 2024 12:40:10 GMT+0000 (Coordinated Universal Time)
Saved by
@Xyfer
#include <stdio.h>
int main() {
int n, m;
// Input size of the first array
scanf("%d", &n);
// Input elements of the first array
int arr1[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
// Input size of the second array
scanf("%d", &m);
// Input elements of the second array
int arr2[m];
for (int i = 0; i < m; i++) {
scanf("%d", &arr2[i]);
}
// Determine the size of the merged array
int merged_size = (n > m) ? n : m; // Choose the maximum size
// Initialize the merged array and add elements from both arrays
int merged[merged_size];
for (int i = 0; i < merged_size; i++) {
int sum = 0;
if (i < n) {
sum += arr1[i];
}
if (i < m) {
sum += arr2[i];
}
merged[i] = sum;
}
// Output the merged array in reverse order
for (int i = merged_size - 1; i >= 0; i--) {
printf("%d ", merged[i]);
}
printf("\n");
return 0;
}
content_copyCOPY
Comments