两种方法编写程序,创建包含10个数的数组,使用循环语句为数组赋值,输出其中最大值及对应的元素下标。考虑整型、小数、字符等不同类型数组的情况。
方法1函数原型:
int getMaxIndex(int a[], int size=10);
int getMaxIndex(double a[], int size=10);
int getMaxIndex(char a[], int size=10);
方法2函数模板原型:
template<typename T>
int getMaxIndex (T a[], int size=10);
基于new Bing加以修改过后的编写:
【法一】
#include <iostream>
using namespace std;
int getMaxIndex(int a[], int size=10);
int getMaxIndex(double a[], int size=10);
int getMaxIndex(char a[], int size=10);
int main() {
int choice;
cout << "请选择输入数据的类型:" << endl;
cout << "1. 整数" << endl;
cout << "2. 小数" << endl;
cout << "3. 字符" << endl;
cin >> choice;
if(choice == 1) {
int intArray[10];
cout << "请输入10个整数:" << endl;
for(int i = 0; i < 10; i++) {
cin >> intArray[i];
}
int max_idx = getMaxIndex(intArray);
cout << "整数数组的最大值为:" << intArray[max_idx] << ", index=" << max_idx << endl;
} else if(choice == 2) {
double doubleArray[10];
cout << "请输入10个小数:" << endl;
for(int i = 0; i < 10; i++) {
cin >> doubleArray[i];
}
int max_idx = getMaxIndex(doubleArray);
cout << "小数数组的最大值为:" << doubleArray[max_idx] << ", index=" << max_idx << endl;
} else if(choice == 3) {
char charArray[10];
cout << "请输入10个字符:" << endl;
for(int i = 0; i < 10; i++) {
cin >> charArray[i];
}
int max_idx = getMaxIndex(charArray);
cout << "字符数组的最大值为:" << charArray[max_idx] << ", index=" << max_idx << endl;
} else {
cout << "输入有误,请重新运行程序!" << endl;
}
return 0;
}
int getMaxIndex(int a[], int size) {
int max_idx = 0;
for(int i = 1; i < size; ++i) {
if(a[i] > a[max_idx]) {
max_idx = i;
}
}
return max_idx;
}
int getMaxIndex(double a[], int size) {
int max_idx = 0;
for(int i = 1; i < size; ++i) {
if(a[i] > a[max_idx]) {
max_idx = i;
}
}
return max_idx;
}
int getMaxIndex(char a[], int size) {
int max_idx = 0;
for(int i = 1; i < size; ++i) {
if(a[i] > a[max_idx]) {
max_idx = i;
}
}
return max_idx;
}
【法二】重构实现
#include <iostream>
using namespace std;
template<typename T>
int getMaxIndex(T a[], int size=10);
int main() {
int choice;
cout << "请选择输入数据的类型:" << endl;
cout << "1. 整数" << endl;
cout << "2. 小数" << endl;
cout << "3. 字符" << endl;
cin >> choice;
if(choice == 1) {
int intArray[10];
cout << "请输入10个整数:" << endl;
for(int i = 0; i < 10; i++) {
cin >> intArray[i];
}
int max_idx = getMaxIndex(intArray);
cout << "整数数组的最大值为:" << intArray[max_idx] << ", index=" << max_idx << endl;
} else if(choice == 2) {
double doubleArray[10];
cout << "请输入10个小数:" << endl;
for(int i = 0; i < 10; i++) {
cin >> doubleArray[i];
}
int max_idx = getMaxIndex(doubleArray);
cout << "小数数组的最大值为:" << doubleArray[max_idx] << ", index=" << max_idx << endl;
} else if(choice == 3) {
char charArray[10];
cout << "请输入10个字符:" << endl;
for(int i = 0; i < 10; i++) {
cin >> charArray[i];
}
int max_idx = getMaxIndex(charArray);
cout << "字符数组的最大值为:" << charArray[max_idx] << ", index=" << max_idx << endl;
} else {
cout << "输入有误,请重新运行程序!" << endl;
}
return 0;
}
template<typename T>
int getMaxIndex(T a[], int size) {
int max_idx = 0;
for(int i = 1; i < size; ++i) {
if(a[i] > a[max_idx]) {
max_idx = i;
}
}
return max_idx;
}
方法一的代码如下
#include <iostream>
using namespace std;
int getMaxIndex(int a[], int size = 10) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int getMaxIndex(double a[], int size = 10) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int getMaxIndex(char a[], int size = 10) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int main() {
int a[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
double b[] = {1.2, 3.4, 5.6, 7.8, 9.0, 2.3, 4.5, 6.7, 8.9, 0.1};
char c[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
cout << "整型数组最大值的下标是:" << getMaxIndex(a) << endl;
cout << "小数数组最大值的下标是:" << getMaxIndex(b) << endl;
cout << "字符数组最大值的下标是:" << getMaxIndex(c) << endl;
return 0;
}
方法二的代码如下:
#include <iostream>
using namespace std;
template<typename T> int getMaxIndex(T a[], int size = 10) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int main() {
int a[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
double b[] = {1.2, 3.4, 5.6, 7.8, 9.0, 2.3, 4.5, 6.7, 8.9, 0.1};
char c[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
cout << "整型数组最大值的下标是:" << getMaxIndex(a) << endl;
cout << "小数数组最大值的下标是:" << getMaxIndex(b) << endl;
cout << "字符数组最大值的下标是:" << getMaxIndex(c) << endl;
return 0;
}
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
以下是两种方法的示例代码:
方法1:
#include <iostream>
using namespace std;
int getMaxIndex(int a[], int size=10) {
int max_index = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[max_index]) {
max_index = i;
}
}
return max_index;
}
int getMaxIndex(double a[], int size=10) {
int max_index = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[max_index]) {
max_index = i;
}
}
return max_index;
}
int getMaxIndex(char a[], int size=10) {
int max_index = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[max_index]) {
max_index = i;
}
}
return max_index;
}
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double b[10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
char c[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
cout << "Max index of int array: " << getMaxIndex(a) << endl;
cout << "Max index of double array: " << getMaxIndex(b) << endl;
cout << "Max index of char array: " << getMaxIndex(c) << endl;
return 0;
}
方法2:
#include <iostream>
using namespace std;
template<typename T>
int getMaxIndex(T a[], int size=10) {
int max_index = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[max_index]) {
max_index = i;
}
}
return max_index;
}
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double b[10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
char c[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
cout << "Max index of int array: " << getMaxIndex(a) << endl;
cout << "Max index of double array: " << getMaxIndex(b) << endl;
cout << "Max index of char array: " << getMaxIndex(c) << endl;
return 0;
}
在这两种方法中,我们都定义了一个数组,然后使用循环为数组赋值,并调用 getMaxIndex 函数来获取数组中最大值的下标。方法1中,我们分别为整型、小数和字符数组定义了 getMaxIndex 函数,每个函数的参数类型不同。方法2中,我们使用了函数模板,可以同时适用于不同类型的数组,只需要在调用时指定数组类型即可。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
引用chatGPT作答,
方法1的代码如下:
#include <iostream>
using namespace std;
int getMaxIndex(int a[], int size=10);
int getMaxIndex(double a[], int size=10);
int getMaxIndex(char a[], int size=10);
int main() {
int intArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double doubleArray[10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
char charArray[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int maxIntIndex = getMaxIndex(intArray);
cout << "The maximum value of intArray is " << intArray[maxIntIndex]
<< " at index " << maxIntIndex << endl;
int maxDoubleIndex = getMaxIndex(doubleArray);
cout << "The maximum value of doubleArray is " << doubleArray[maxDoubleIndex]
<< " at index " << maxDoubleIndex << endl;
int maxCharIndex = getMaxIndex(charArray);
cout << "The maximum value of charArray is " << charArray[maxCharIndex]
<< " at index " << maxCharIndex << endl;
return 0;
}
int getMaxIndex(int a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int getMaxIndex(double a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int getMaxIndex(char a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
这种方法的缺点是代码重复,需要写多个函数来处理不同类型的数组,而且当需要支持更多类型时,需要再写更多函数。而且,如果想要在函数中输出数组的最大值,需要再写一个函数来实现。
方法2的代码如下:
#include <iostream>
using namespace std;
template<typename T>
int getMaxIndex(T a[], int size=10);
int main() {
int intArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double doubleArray[10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
char charArray[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int maxIntIndex = getMaxIndex(intArray);
cout << "The maximum value of intArray is " << intArray[maxIntIndex]
<< " at index " << maxIntIndex << endl;
int maxDoubleIndex = getMaxIndex(doubleArray);
cout << "The maximum value of doubleArray is " << doubleArray[maxDoubleIndex]
<< " at index " << maxDoubleIndex << endl;
int maxCharIndex = getMaxIndex(charArray);
cout << "The maximum value of charArray is " << charArray[maxCharIndex]
<< " at index " << maxCharIndex << endl;
return 0;
}
template<typename T>
int getMaxIndex(T a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
这种方法的优点是代码简洁,只需要写一个函数模板就可以处理不同类型的数组,而且可以通过模板参数来自动推导数组的类型。缺点是对于复杂的数据类型,比如自定义的结构体或者类,需要实现相应的比较函数才能使用该函数模板。
引用chatGPT作答,
方法1的代码如下:
#include <iostream>
using namespace std;
int getMaxIndex(int a[], int size=10);
int getMaxIndex(double a[], int size=10);
int getMaxIndex(char a[], int size=10);
int main() {
int intArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double doubleArray[10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
char charArray[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int maxIntIndex = getMaxIndex(intArray);
cout << "The maximum value of intArray is " << intArray[maxIntIndex]
<< " at index " << maxIntIndex << endl;
int maxDoubleIndex = getMaxIndex(doubleArray);
cout << "The maximum value of doubleArray is " << doubleArray[maxDoubleIndex]
<< " at index " << maxDoubleIndex << endl;
int maxCharIndex = getMaxIndex(charArray);
cout << "The maximum value of charArray is " << charArray[maxCharIndex]
<< " at index " << maxCharIndex << endl;
return 0;
}
int getMaxIndex(int a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int getMaxIndex(double a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
int getMaxIndex(char a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
这种方法的缺点是代码重复,需要写多个函数来处理不同类型的数组,而且当需要支持更多类型时,需要再写更多函数。而且,如果想要在函数中输出数组的最大值,需要再写一个函数来实现。
方法2的代码如下:
#include <iostream>
using namespace std;
template<typename T>
int getMaxIndex(T a[], int size=10);
int main() {
int intArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double doubleArray[10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
char charArray[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int maxIntIndex = getMaxIndex(intArray);
cout << "The maximum value of intArray is " << intArray[maxIntIndex]
<< " at index " << maxIntIndex << endl;
int maxDoubleIndex = getMaxIndex(doubleArray);
cout << "The maximum value of doubleArray is " << doubleArray[maxDoubleIndex]
<< " at index " << maxDoubleIndex << endl;
int maxCharIndex = getMaxIndex(charArray);
cout << "The maximum value of charArray is " << charArray[maxCharIndex]
<< " at index " << maxCharIndex << endl;
return 0;
}
template<typename T>
int getMaxIndex(T a[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (a[i] > a[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
这种方法的优点是代码简洁,只需要写一个函数模板就可以处理不同类型的数组,而且可以通过模板参数来自动推导数组的类型。缺点是对于复杂的数据类型,比如自定义的结构体或者类,需要实现相应的比较函数才能使用该函数模板。
函数的定义是函数具体功能实现的全部代码
函数的声明是当函数的定义在main函数之后时,在main函数先写一个函数声明,它不需要全部的代码,把花括号换成一个分号即可。