Answer to Question #311014 in C++ for Nobody

Question #311014

Write C++ statements to perform the following tasks. Assume that each task related to each other.

(a) Create an array to hold 10 double values with initial value of 0.0.

(b) Assign value 55.5 to the last element in the array.

(c) Display the sum of the two first elements.



(d) Display all elements of the array using for loop.

(e) Computes the sum of all elements in the array using for loop and display the

result.

(f) Find the smallest and the largest value in the array and display the result.



1
Expert's answer
2022-03-13T19:07:10-0400
#include <iostream>
using namespace std;


int main() {
    // (a) Create an array to hold 10 double values with initial value of 0.0.
    double array[10] = {0.0};


    // (b) Assign value 55.5 to the last element in the array.
    array[9] = 55.5;


    // (c) Display the sum of the two first elements.
    cout << array[0] + array[1] << endl;


    // (d) Display all elements of the array using for loop.
    for (int i=0; i<10; i++) {
        cout << array[i] << " ";
    }
    cout << endl;


    // (e) Computes the sum of all elements in the array using for loop and display the result.
    double sum = 0.0;
    for (int i=0; i<10; i++) {
        sum += array[i];
    }
    cout << "Sum: " << sum << endl;


    // (f) Find the smallest and the largest value in the array and display the result.
    double smallest, largest;
    smallest = largest = array[0];
    for (int i=1; i<10; i++) {
        if (smallest > array[i]) {
            smallest = array[i];
        }
        if (largest < array[i]) {
            largest = array[i];
        }
    }
    cout << "Smallest: " << smallest << endl;
    cout << "Largest: " << largest << 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