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;
}
#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;
}
Comments
Leave a comment