Answer to Question #198547 in C++ for shuo

Question #198547

Write a void function printArray() that takes a double array and the size of the array as an int and prints the values. void shift left(double values[], int size); void printArray(double values[], int size); Here is the main function to test your function.

int main()

{

const int LENGTH = 10;

double p[LENGTH] = {10, 15, 7.9, 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5};

cout << "\nThe original array follows ...\n";

printArray(p, LENGTH);

cout << "\n\nThe left shifted array follows: \n";

shift_left(p, LENGTH);

printArray(p, LENGTH); //Prints 15, 7.9, 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5, 10

cout << "\n\nThe left shifted array follows: \n";

shift_left(p, LENGTH);

printArray(p, LENGTH);//Prints 7.9, 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5, 10, 15

cout << "\n\nThe left shifted array follows: \n";

shift_left(P, LENGTH);

printArray(p, LENGTH);//Prints 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5, 10, 15, 7.9

return 0;

}


1
Expert's answer
2021-05-27T19:11:16-0400
#include <iostream>
#include <iomanip>

using namespace std;

void printArray(double values[], int size)
{
    for(int i = 0; i < size; ++i)
    {
        cout << values[i];
        
        if(i < size - 1)
        {
            cout << (i == size - 1 ? "\n" : " ");
        }
    }
}

void shift_left(double values[], int size)
{
    if(size > 1)
    {
        double tmp = values[0];

        for(int i = 1; i < size; ++i)
        {
            values[i - 1] = values[i];
        }

        values[size - 1] = tmp;
    }
}

int main()
{
    const int LENGTH = 10;
    double p[LENGTH] = {10, 15, 7.9, 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5};

    cout << "\nThe original array follows ...\n";
    printArray(p, LENGTH);

    cout << "\n\nThe left shifted array follows: \n";
    shift_left(p, LENGTH);
    printArray(p, LENGTH); //Prints 15, 7.9, 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5, 10

    cout << "\n\nThe left shifted array follows: \n";
    shift_left(p, LENGTH);
    printArray(p, LENGTH);//Prints 7.9, 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5, 10, 15

    cout << "\n\nThe left shifted array follows: \n";
    shift_left(p, LENGTH);
    printArray(p, LENGTH);//Prints 8.5, 12, 14.3, 16.0, 17.99, 19.3, 23.5, 10, 15, 7.9

    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