Write the exact output of the following algorithms:
1.1 Study the following array in an algorithm called arr. Indicate what the values in arr
would be after the given code has been executed.
4 6 8 10 120 14 15 99 102 55
for x = 0 to 9
if arr(x) Mod 2 = 0 then
arr(x) = arr(x) + 5
endif
if x Mod 2 = 0 then
arr(x) = arr(x) + 1
endif
next x
x = x – 5
for y = 2 to 8 step 2
arr(y) = arr(x) + y
next y
for z = 1 to 6 step 3
arr(z) = z * y + x
next
#include <iostream>
using namespace std;
int main () {
int x, y, z;
int arr[10] = {4, 6, 8, 10, 120, 14, 15, 99, 102, 55};
cout << "Before the algorithm: ";
for (int i = 0; i < 10; i++) {
cout << arr[i] << " ";
}
// algorithm
for (int x = 0; x <= 9; x++) {
if (arr[x] % 2 == 0) {
arr[x] += 5;
}
if (x % 2 == 0) {
arr[x] += 1;
}
}
x -= 5;
for (y = 2; y <= 8; y += 2) {
arr[y] = arr[x] + y;
}
for (z = 1; z <= 6; z += 3) {
arr[x] = z * y + x;
}
cout << "\nAfter the algorithm: ";
for (int i = 0; i < 10; i++) {
cout << arr[i] << " ";
}
return 0;
}
Comments
Leave a comment