Answer to Question #145451 in C++ for lalith

Question #145451
Write a program that declares three one-dimensional arrays named miles, gallons, and mpg(miles per gallons). Each array should be capable of holding 10 elements. In the miles array, store the numbers 240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4, and 192.3. In the gallons array, store the numbers 10.3, 15.6, 8.7, 14, 16.3, 15.7, 14.9, 10.7, 8.3, and 8.4. Each element of the mpg array should be calculated as the corresponding element of the miles array divided by the equivalent element of
the gallons array: for example, mpg[0] =miles[0] / gallons[0]. Use pointers when calculating and displaying the elements of the mpg array.
1
Expert's answer
2020-11-19T16:48:23-0500
#include <iostream>


int main()
{
	const int N = 10;
	const float miles[N] = { 240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4, 192.3 };
	const float gallons[N] = { 10.3, 15.6, 8.7, 14, 16.3, 15.7, 14.9, 10.7, 8.3, 8.4 };
	float mpg[N];


	for (int i = 0; i < N; ++i)
	{
		*(mpg + i) = (*(miles + i)) / (*(gallons + i));
	}


	for (int i = 0; i < N; ++i)
	{
		std::cout << "Miles: " << *(miles + i) << " ";
		std::cout << "Gallons: " << *(gallons + i) << " ";
		std::cout << "Mpg: " << *(mpg + i) << "\n";
	}


	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