Answer to Question #344545 in C++ for sami

Question #344545

Write a program that gives the maximum value from an array with size 8. The data stored in the array should be accepted from the user and should be an integer value. Write another program which orders the same data in an ascending order



1
Expert's answer
2022-05-24T13:57:39-0400

Max value:

#include <iostream>

int main()
{
	int arr[8];
	for (int i = 0; i < 8; ++i)
	{
		std::cout << "Enter element " << i << ": ";
		std::cin >> arr[i];
	}
	
	int max = arr[0];
	for (int i = 1; i < 8; ++i)
	{
		if (arr[i] > max)
			max = arr[i];
	}
	std::cout << "Max value: " << max << std::endl;

	return 0;
}


Ascending order:

#include <iostream>

int main()
{
	int arr[8];
	for (int i = 0; i < 8; ++i)
	{
		std::cout << "Enter element " << i << ": ";
		std::cin >> arr[i];
	}
	
	int max = arr[0];
	for (int i = 0; i < 7; ++i)
	{
		int min_index = i;
		for (int j = i + 1; j < 8; ++j)
		{
			if (arr[j] < arr[min_index])
				min_index = j;
		}
		
		int tmp = arr[i];
		arr[i] = arr[min_index];
		arr[min_index] = tmp;
	}

	std::cout << "Ascending order: " << std::endl;
	for (int i = 0; i < 8; ++i)
	{
		std::cout << arr[i] << std::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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS