例1,输出最大值
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
int a[2][3] = { { 15,34,23 },{ 54,27,65 } }; // 二维数组
int i, j, k;
int row, col;
int max = 0;
for (i = 0; i <= 1; i++)
for (j = 0; j <= 2; j++)
{
cout << setw(4) << a[i][j];
if (j == 2)
cout << endl;
}
cout << " *********************** " << endl;
for (i = 0; i <= 1; i++)
for (j = 0; j <= 2; j++)
{
if (max < a[i][j]) {
max = a[i][j];
row = i+1;
col = j+1;
}
}
cout << "The bigger number is:" << max << endl;
cout << "row=" << row << "," << "col=" << col << endl;
return 0;
}
------------------------------------------------------------------------------------
结果:
15 34 23
54 27 65
***********************
The bigger number is:65
row=2,col=3
------------------------------------------------------------------------------------
例2,大小排序(https://baike.baidu.com/item/冒泡排序/4602306?fr=aladdin)
#include <iostream>
using namespace std;
template<typename T>
//整数或浮点数皆可使用
void bubble_sort(T arr[],
int
len
)
{
int
i, j; T temp;
for (i = 0; i <
len
- 1; i++)
for (j = 0; j <
len
- 1 - i; j++)
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
int
main()
{
int
arr[] = { 61, 17, 29, 22, 34, 60, 72, 21, 50, 1, 62 };
int
len
= (
int
) sizeof(arr) / sizeof(*arr);
bubble_sort(arr,
len
);
for (
int
i = 0; i <
len
; i++)
cout << arr[i] <<
' '
;
cout << endl;
float arrf[] = { 17.5, 19.1, 0.6, 1.9, 10.5, 12.4, 3.8, 19.7, 1.5, 25.4, 28.6, 4.4, 23.8, 5.4 };
len
= (
int
) sizeof(arrf) / sizeof(*arrf);
bubble_sort(arrf,
len
);
for (
int
i = 0; i <
len
; i++)
cout << arrf[i] <<
' '
;
return 0;
}
来源:https://www.cnblogs.com/qq2806933146xiaobai/p/12228733.html