Answer to Question #165880 in C++ for blessing

Question #165880

An array called number contains 20 integer values and must be filled with values in the following way:

 Fill the array with consecutive even numbers starting from 10.

 Start again at the first element and increase every fourth element with 3

.  Replace the value of the first four elements each with its square.

 Deduct 2 from the last 3 elements.

 Swap the value of element 5 and element 15

.  Add the value of the first element to that of the last element

 Add consecutive integers (starting from 1) to all even elements. Remember, the first element has a subscript of 0 and the last element (element 20) has a subscript of 19.



The following must be displayed:

 All elements must be displayed each on a new line. Identify each element clearly

.  Display the highest value in the array.

 The lowest value in the array must be displayed.

 Count and display how many even values and how many odd values the array contains.


1
Expert's answer
2021-02-23T04:05:05-0500
#include <iostream>
#include <cmath>
using namespace std;
int main () {
    int arr[20];
    
    // Fill the array with consecutive even numbers starting from 10.
    for (int i = 0; i < 20; i++) {
        arr[i] = (i + 5) * 2; 
    }
    // Start again at the first element and increase every fourth element with 3
    for (int i = 0; i < 20; i++) {
        if (i + 1 % 4 == 0) {
            arr[i] += 3; 
         }
    }
    // Replace the value of the first four elements each with its square.
    for (int i = 0; i < 4; i++) {
        arr[i] = pow(arr[i], 2);
    }
    // Deduct 2 from the last 3 elements.
    for (int i = 19; i >= 17; i--) {
        arr[i] -= 2;
    }
    // Swap the value of element 5 and element 15
    for (int i = 0; i < 20; i++) {
        int temp;
        arr[4] = temp;
        arr[4] = arr[14];
        arr[14] = temp; 
    }
    // Add the value of the first element to that of the last element
    arr[19] += arr[0];
    // Add consecutive integers (starting from 1) to all even elements. Remember, the first element has a subscript of 0 and the last element (element 20) has a subscript of 19.
    int i = 1;
    for (int i = 0; i < 20; i++) {
        if (arr[i] % 2 == 0) {
            arr[i] += i++;
        }
    }
    
    // All elements must be displayed each on a new line. Identify each element clearly
    cout << "Array[20]: ";
    for (int i = 0; i < 20; i++) {
        cout << arr[i] << "  ";
    }
    // Display the highest value in the array.
    int max = arr[0];
    for (int i = 0; i < 20; i++) {
        if (max < arr[i]) {
            max = arr[i];
        }
    }
    // The lowest value in the array must be displayed.
    cout << "\nmax element: " << max << endl;
    int min = arr[0];
    for (int i = 0; i < 20; i++) {
        if (min > arr[i]) {
            min = arr[i];
        }
    }
    cout << "min element: " << min << endl;
    // Count and display how many even values and how many odd values the array contains. 
    int countEven = 0, countOdd = 0;
    for (int i = 0; i < 20; i++) {
        if (arr[i] % 2 == 0) {
            countEven++;
        }
        if (arr[i] % 2 == 1) {
            countOdd++;
        }
    }
    cout << "Number of Even numbers: " << countEven << endl;
    cout << "Number of Odd numbers: " << countOdd << endl;
    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

LATEST TUTORIALS
New on Blog