gpt4 book ai didi

c++ - 使用 int、float 和 char 的模板函数来查找最小值

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:47 25 4
gpt4 key购买 nike

提示:将函数 theSmallest() 和 fill_array() 转换为模板函数。使用 3 种不同的数据类型测试您的模板函数:int、float 和 char。

问题:当我将其加载到编译器中时,没有任何错误。它适用于 INT 和 Float;然而,当我为字符选项运行 index_of_smallest 函数时......它不喜欢它。当我在 char 调用期间实际使用“cout”检查 min 的值时,它给了我“-10002e89 ..”。该值永远不会改变,因此在 for 循环中,index_of_min 始终为 0,因此编译器认为最小字符始终位于索引 0 处。

代码:

#include <iostream>
#include <cstdlib>
using namespace std;
const int DECLARED_SIZE = 20;

template <class BaseType>
int index_of_smallest(BaseType a[], int size, int& number_used)
{
BaseType min = a[0];
int index_of_min = 0;
cout << index_of_min<< endl;
for (int index = 1 ; index < number_used; index++)
{
if (a[index] < min)
{
min = a[index];
index_of_min = index;
}
}
cout << index_of_min<< endl;
return index_of_min;
}

template <class BaseType>
void fill_array(BaseType a[], int size, int& number_used)
{
int index = 0;
BaseType ans;

cout << "Enter up to " << size << " of the Data Type selected.\n"
<< "Mark the end of a list of numbers with a negative number.\n"
<< "Mark the end of a list of characters with the negative symbol \"-\". \n"
<< "Separate each input with a space. \n" << endl;
cin >> ans;

while (ans >= 0 && ans != '-')
{
a[index] = ans;
index++;
cin >> ans;
}
number_used = index;
}

void menu()
{
int number_used, choice, smallest, result;
int start_index = 0;
char menuLoop = 'Y';

while (toupper(menuLoop) == 'Y')
{
cout << "Please select a type of Input: \n"
<< "1. Integers (1, 2, 3, 4, etc..)\n"
<< "2. Decimals (1.0, 2.0, 3.0. 4.0, etc..)\n"
<< "3. Characters ( a, b, c, d, etc...)\n\n\n";
cin >> choice;


switch (choice)
{
case 1:
int arrI[DECLARED_SIZE];
system ("CLS");
fill_array(arrI, DECLARED_SIZE, number_used);
smallest = index_of_smallest(arrI, start_index, number_used);
cout << "The smallest integer is: " << arrI[smallest] << " ";
break;
case 2:
float arrF[DECLARED_SIZE];
system ("CLS");
fill_array(arrF, DECLARED_SIZE, number_used);
smallest = index_of_smallest(arrF, start_index, number_used);
cout << "The smallest float is: "<< arrF[smallest] << " ";
break;
case 3:
char arrC[DECLARED_SIZE];
system ("CLS");
fill_array(arrC, DECLARED_SIZE, number_used);
smallest = index_of_smallest(arrF, start_index, result);
cout << "The smallest character is: " << arrC[smallest] <<" \n";
break;
default:
cout << "That is an invalid choice! Exiting Program";
}

cout << "\n\nWould you like to return to the main menu? (Y/N): ";
cin >> menuLoop;

while(toupper(menuLoop) != 'Y' && toupper(menuLoop) != 'N')
{
cout << "Invalid Choice: Please type (Y/N): ";
cin >> menuLoop;
}

system("CLS");
}
}

int main()
{
using namespace std;

menu();
system("PAUSE");
}

最佳答案

你的错误在这里:

smallest = index_of_smallest(arrF, start_index, result);

应该是:

smallest = index_of_smallest(arrC, start_index, number_used);

您使用了错误的数组,并且传递的是 result(从未初始化)而不是 number_used

关于c++ - 使用 int、float 和 char 的模板函数来查找最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27118981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com