编写一个数组,
求出其最大值,最小值,并输出两者的下标,
然后求出差值 排序(从大到小) ?
#define NUM 5
#include <iostream>
#include <string>
using namespace std;
int main()
{
int arr[NUM];
int MAX = 0, MIN = 0;
int num =0;
cout << "请输入数组数据:" << endl;
for (int i = 0; i < NUM; i++)
{
cin >> arr[i];
}
for (int i = 0; i < NUM; i++)
{
if (arr[MAX] < arr[i])
{
MAX = i;
}
}
for (int i = 0; i < NUM; i++)
{
if (arr[MIN] > arr[i])
{
MIN = i;
}
}
cout << "最大值:" << arr[MAX] << "位置:" << MAX + 1 << endl;
cout << "最小值:" << arr[MIN] << "位置:" << MIN + 1 << endl;
cout << "差值:" << arr[MAX]-arr[MIN] << endl;
cout << "排序:" << endl;
for (int i = NUM; i > 1; i--)
{
for (int j = 0; j < i; j++)
{
if (arr[j] < arr[j + 1])
{
arr[j] = arr[j] + arr[j + 1];
arr[j + 1] = arr[j] - arr[j + 1];
arr[j] = arr[j] - arr[j + 1];
}
}
}
for (int i = 0; i < NUM; i++)
{
cout << arr[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
|