Answer to Question #149456 in C++ for Jochelle Buenaventura

Question #149456
Create a program using a one-dimensional array that determines the highest value among the five input values from the keyboard and prints the difference of each value from the highest.
1
Expert's answer
2020-12-09T07:54:53-0500
#include <iostream>
using namespace std;
int main()
{
	int arr[5];
	int maxElement;

	for (int i = 0; i < 5; i++) {
			cin >> arr[i];
			if (i == 0) {
				maxElement = arr[i];
			}
			else {
				if (arr[i] > maxElement) {
					maxElement = arr[i];
				}
			}
	}
	while (cin.get() != '\n');

	cout << "Start array    : ";
	for (int i = 0; i < 5; i++) {
		cout << arr[i] << " ";
	}
	cout << endl;


	cout << "maxElement = " << maxElement << endl;


	for (int i = 0; i < 4; i++) {
		for (int j = i + 1; j < 5; j++) {
			if (arr[i] < arr[j]) {
				int tmp = arr[i];
				arr[i] = arr[j];
				arr[j] = tmp;
			}
		}
	}

	cout << "Sorting array : ";
	for (int i = 0; i < 5; i++) {
		cout << arr[i] << " ";
	}
	cout << endl;

	cout << "Final result  : ";
	for (int i = 0; i < 5; i++) {
		cout << maxElement-arr[i] << " ";
	}
	cout << endl;
}

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