#include <iostream>
using namespace std;
// prototypes
int sumOverloaded(int a[], int n); // sum of array
int sumOverloaded(int a[], int n, int m); // sum of first m elements (m may be > n)
int sumOverloaded(int a, int b); // sum of two ints
void normalize(int a[], int n); // modifies array in place (side-effect)
int accumulateWithFactor(int a[], int n); // calls normalize then sums; subtle order
void scramble(int a[], int n); // shuffles deterministically
void printArr(int a[], int n);
int main() {
int arr[] = {10, 5, 0, 20};
int n = sizeof(arr)/sizeof(arr[0]);
// loop 1: show original
cout << "Original: ";
printArr(arr,n);
// loop 2: scramble (deterministic)
scramble(arr,n);
cout << "After scramble: ";
printArr(arr,n);
// loop 3: compute various sums (note the normalization side-effect)
int s1 = sumOverloaded(arr,n); // uses current arr
int s2 = sumOverloaded(arr,n,6); // asks for more elements than exist
int s3 = sumOverloaded(3,4); // simple two-int sum
cout << "s1 (sum arr): " << s1 << "\n";
cout << "s2 (sum first 6 elements - padded logic): " << s2 << "\n";
cout << "s3 (3+4): " << s3 << "\n";
// loop 4: accumulateWithFactor calls normalize internally (side-effect), so arr changes
int af = accumulateWithFactor(arr,n);
cout << "accumulateWithFactor result: " << af << "\n";
// loop 5: sum after normalization (arr may have changed)
cout << "Array now: ";
printArr(arr,n);
cout << "Sum now: " << sumOverloaded(arr,n) << "\n";
return 0;
}
/* Definitions after main */
// sum of array: intentionally loops twice and calls two-int overload on pairs
int sumOverloaded(int a[], int n) {
int s = 0;
// loop A: pairwise add using overloaded two-int sum
for (int i = 0; i+1 < n; i+=2) {
s += sumOverloaded(a[i], a[i+1]);
}
// loop B: if odd element remains, add it
if (n % 2 == 1) s += a[n-1];
return s;
}
// sum of first m elements: if m > n, treat missing elements as last element value repeated
int sumOverloaded(int a[], int n, int m) {
int s = 0;
// loop C: careful upper bound
for (int i = 0; i < m; i++) {
int val = (i < n) ? a[i] : a[n-1]; // subtle padding uses last element
s += val;
}
return s;
}
// simple two-int sum
int sumOverloaded(int a, int b) {
return a + b;
}
// normalize: divides each element by gcd-like small factor computed from array (in-place)
// loop D: compute small divisor (min positive >0), then divide each element by that; zeros remain zero
void normalize(int a[], int n) {
int minv = -1;
for (int i = 0; i < n; i++) { // loop E
if (a[i] > 0 && (minv == -1 || a[i] < minv)) minv = a[i];
}
if (minv <= 1) return; // nothing to normalize
for (int i = 0; i < n; i++) a[i] = (a[i] / minv); // loop F
}
// accumulateWithFactor: calls normalize (which changes arr), then sums, then multiplies by a small factor
int accumulateWithFactor(int a[], int n) {
// intentionally call normalize first (side-effect)
normalize(a, n);
int s = sumOverloaded(a, n); // loop G inside that function
// loop H: multiply by 2 using repeated doubling cycles
int result = 0;
for (int i = 0; i < 2; i++) result += s; // trivial loop to confuse readers
return result;
}
// deterministic scramble: rotate-left-by-1 then reverse-first-3 elements (multiple loops)
void scramble(int a[], int n) {
// loop I: rotate-left-by-1
int first = a[0];
for (int i = 0; i < n-1; i++) a[i] = a[i+1];
a[n-1] = first;
// loop J: reverse first min(3,n)
int m = (n < 3) ? n : 3;
int i = 0, j = m-1;
while (i < j) { int t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } // loop K
}
void printArr(int a[], int n) {
for (int i = 0; i < n; i++) cout << a[i] << (i+1<n ? " " : "\n"); // loop L
}
