C 如何编写一个函数来查找具有最小值的索引
我正在学习通过引用传递的函数 我的问题是如何编写一个函数来查找从输入文本文件中获取的具有最低值的索引 文本文件包含用于创建范围的最小索引和最大索引。函数应返回该范围内存储的具有最低值的索引。然后函数还应该检查范围内具有相同值的索引,然后返回其中的第一个索引C 如何编写一个函数来查找具有最小值的索引,c,function,pass-by-reference,C,Function,Pass By Reference,我正在学习通过引用传递的函数 我的问题是如何编写一个函数来查找从输入文本文件中获取的具有最低值的索引 文本文件包含用于创建范围的最小索引和最大索引。函数应返回该范围内存储的具有最低值的索引。然后函数还应该检查范围内具有相同值的索引,然后返回其中的第一个索引 范例 index 0 1 2 3 4 5 6 7 8 9 10 11 value 8 3 6 7 9 5 3 8 6 7 4 5 如果从输入文本文件读取的最小索引为0,从文本文件读取的最大索引为11,则返回值应为索引1 使用的函数
范例
index 0 1 2 3 4 5 6 7 8 9 10 11
value 8 3 6 7 9 5 3 8 6 7 4 5
如果从输入文本文件读取的最小索引为0,从文本文件读取的最大索引为11,则返回值应为索引1
使用的函数原型
int findMinIndex(int data[],int low,int high)代码>
完整程序
#包括
#定义最大值100
int findMinIndex(int data[],int low,int high);
int main(){
int i,循环,n,查询,数据[MAX];
//读入计划数据。
文件*ifp=fopen(“schedule.in”,“r”);
fscanf(ifp、%d、&n);
对于(i=0;i函数的实现
int-findMinIndex(int-data[],int-low,int-high){
//对这个论点作一些核对
如果(高<低){
//这里一定有错误
//TODO:抛出参数表达式
//更新:
//您需要根据您的逻辑处理此错误
//你可以交换它们
//但现在只需打印一个错误消息
printf(“发生错误:开始索引(%d),大于结束索引(%d)”,低,高);
返回-1;
}
如果(数据==NULL){
//这里一定有错误
//TODo:抛出参数表达式null数据
//更新:
//您需要根据您的逻辑处理此错误
//现在只需打印一个错误消息
printf(“发生错误:找不到数据!!findMinIndex函数要求int数组引用…”);
返回-2;
}
//假设minimumValue是重新排列中的第一个元素
int最小值=数据[低];
int指数=低;
对于(int i=低;i<高;i++){
if(数据[i]<最小值){
最小值=数据[i];
指数=i;
}
}
收益指数;
}
更新:
我更新了代码以返回negitve值,并在发生错误时打印一些错误消息。您必须根据您的逻辑在函数中处理此错误。因为如果函数使用错误的参数调用,它将有错误的行为…在C语言中,它不支持错误异常,所以我只返回负值,表示函数不工作国王果然
如果您不想直接从函数打印到控制台,并且只想打印来自主函数的消息,那么您可以这样做
int minmumLocation = findMinIndex(data, low, high);
// cheke if error is hapen inside findMinIndex
if (minmumLocation < 0) {
// you could handle error here
switch (minmumLocation){
case -1: /*handle first type of error*/break;
case -2: /*handle second type of erro*/break;
}
// you could stop the program or if you handel it then continue normally
// return;
}
// normall process when function has no error
printf("%d\n", minmumLocation);
int-minmumLocation=findMinIndex(数据、低、高);
//检查findMinIndex中是否存在错误
如果(最小位置<0){
//您可以在这里处理错误
开关(最小位置){
案例1:/*处理第一类错误*/中断;
案例2:/*处理第二类错误*/中断;
}
//您可以停止该程序,或者如果您处理了它,则可以正常继续
//返回;
}
//函数无错误时的正常进程
printf(“%d\n”,最小位置);
错误类型可能发生在findIndex()内部
所有可能发生的错误类型都是一个参数错误。当向函数传递一个错误的参数时会发生错误。文件中的查询可能是一个错误的查询,因此您必须在程序中使用一些错误捕获技术和处理
第一个错误是,如果传递的起始索引(int-low
)大于结束索引(int-high
),则此处不符合逻辑…因此这一定是一个错误…您可以交换它们或根据需要处理它
seconded error是指如果您传递一个null
数组,这显然是一个错误,因为您无法访问任何数据
第三个错误此错误未在函数中实现…如果传递的结束索引(int high
)大于数组的最大大小,该怎么办!!…这将导致acces数据超出范围(在C中这是非常危险的)因此,为了避免这个错误,您必须更改函数签名以通过数组的最大大小,然后函数原型将像int findMinIndex(int data[],int maxSize,int low,int high);
然后您可以像这样添加更多的chek压缩if(high>maxSize){return-3;}
您可以提交输入计划。如果错误仍然存在,请在文件中提交!您所说的“通过引用传递”是什么意思?C是通过值传递。您可以通过传递地址来模拟通过引用传递,但您应该停止将其视为“通过引用传递”。@RetiredInja我相信OP不需要数组长度,因为他需要“数据的有效低/高指数"@Kakarotto如果您将low=0
,您将丢失从调用者那里获得的信息。或者使用其他迭代变量,或者像您现在这样递增low
,但不事先将其归零。@Al.G.Ah,我读错了。我认为min/max是数组中的值,他们正在寻找最低值的索引“WestAppSurvi:”是通过引用的方式。地址是引用。历史上单词是这样使用的。C++后来采用“引用”一词来指代该语言中的特定特征,但它不改变C++上下文之外的单词的含义。(使用和或指针计算)或自动(依靠数组的自动转换),传递的是一个确实引用了预期对象的对象,因此它是英语意义上和技术上非C++意义上的引用。您好,谢谢您的帮助。我现在唯一的问题是,输出在开始时打印3个零,并且
int findMinIndex(int data[], int low, int high) {
// make some checks on the argument
if (high < low) {
// here must be error
// TODO : throw an argument exption
// update:
// you need to handle this error according your logic
// you could swap them
// but for now just print an error msg
printf("error happen : start index (%d),is bigger than end index(%d) ", low, high);
return -1;
}
if (data == NULL) {
// here must be error
// TODo: throw an argument exption null data
// update:
// you need to handle this error according your logic
// for now just print an error msg
printf("error happen : data not found !! findMinIndex function require refrance for int array... ");
return -2;
}
// assume that the minimumValue is first element in the reange
int minimumValue = data[low];
int index = low;
for (int i = low;i < high;i++) {
if (data[i] < minimumValue) {
minimumValue = data[i];
index = i;
}
}
return index;
}
int minmumLocation = findMinIndex(data, low, high);
// cheke if error is hapen inside findMinIndex
if (minmumLocation < 0) {
// you could handle error here
switch (minmumLocation){
case -1: /*handle first type of error*/break;
case -2: /*handle second type of erro*/break;
}
// you could stop the program or if you handel it then continue normally
// return;
}
// normall process when function has no error
printf("%d\n", minmumLocation);