Answer to Question #165723 in C++ for Kefilwe Mamabolo

Question #165723

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 


1
Expert's answer
2021-02-22T16:06:08-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment