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