我如何编写一个函数来找到具有最低值的索引

How can I write a function that will find the index with the lowest value

我正在学习通过引用传递的函数。

我的问题是如何编写一个函数来找到从输入文本文件中获取的具有最低值的索引。

文本文件包含创建范围的最小和最大索引。该函数应该 return 存储在该范围内的最低值的索引。然后函数还应该检查范围内具有相同值的索引,然后 return 这些索引中的第一个。


例子

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,则 return 应为索引 1。


使用的函数原型int findMinIndex(int data[], int low, int high);


完整节目

#include <stdio.h>

#define MAX 100

int findMinIndex(int data[], int low, int high);

int main() {
    int i, loop, n, queries, data[MAX];

    // Read in the schedule data.
    FILE* ifp = fopen("schedule.in", "r");
    fscanf(ifp, "%d", &n);
    for (i=0; i<n; i++)
        fscanf(ifp, "%d", &data[i]);

    // Process each query.
    fscanf(ifp, "%d", &queries);
    for (loop=0; loop<queries; loop++) {
        int low, high;
        fscanf(ifp, "%d%d", &low, &high);
        printf("%d\n", findMinIndex(data, low, high));
    }

    return 0;
}

// Pre-condition:  low <= high and are both valid indexes to data.
// Post-condition: Returns the lowest index in [low, high] storing
//                 the minimum of array[low]�array[high].
int findMinIndex(int data[], int low, int high){      
    
     //Function needed

}

预期输出link

更新:我已经实现了 Ibram Reda 的功能,程序正在打印与预期输出相似的索引,但由于某种原因并不完全相同。

这是更新后的代码,我还在输出文件中附加了一个 link。

#define MAX 100

int findMinIndex(int data[], int low, int high);

int main() {
    int i, loop, n, queries, data[MAX];

    // Read in the schedule data.
    FILE* ifp = fopen("schedule.in", "r");
    fscanf(ifp, "%d", &n);
    for (i=0; i<n; i++)
        fscanf(ifp, "%d", &data[i]);

    // Process each query.
    fscanf(ifp, "%d", &queries);
    for (loop=0; loop<queries; loop++) {
        int low, high;
        fscanf(ifp, "%d%d", &low, &high);
        printf("%d\n", findMinIndex(data, low, high));
    }

    return 0;
    
    
}

// Pre-condition:  low <= high and are both valid indexes to data.
// Post-condition: Returns the lowest index in [low, high] storing
//                 the minimum of array[low]�array[high].

int findMinIndex(int data[], int low, int high) {

    int k = 0;
    int minimumValue;
    int index;
    // make some checks on the argument  
    if (high < low) {
        // here must be error
        // TODO : throw an argument exption
    }
    if (data == NULL) {
        // here must be error
        // TODo: throw an argument exption null data 
    }
        
    minimumValue = data[low]; 
    index = low;
    for (k = low;k < high;k++) {        
        if (data[k] < minimumValue) {
            minimumValue = data[k];
            index = k;
        }
    }//Loop to find lowest index
    return index;
}

函数的实现

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;

}

更新:

我将代码更新为return负值并在发生错误时打印一些错误消息你必须根据你的逻辑在函数中处理这个错误..因为如果函数调用错误参数它会出错行为...在 C 语言中它不支持错误异常所以我只是 return 负值表明该功能没有按预期工作...

如果您不喜欢直接从函数打印到控制台,并且您只喜欢从主函数打印消息,那么您可以像这样编写代码

 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);

错误类型可能发生在findIndex()

所有错误类型都可能发生是参数错误..当将错误的参数传递给函数时会出现...您在文件中的查询可能是错误的查询,因此您必须采用一些错误捕获技术和处理在节目中

  1. 第一个错误是如果你传递的开始索引(int low)大于结束索引(int high)所以这里不合逻辑......所以这一定是一个错误......你可以交换它们或按你喜欢的方式处理它
  2. 第二个错误是如果你传递一个null数组,这显然是一个错误,因为你不能访问任何数据
  3. 第三个错误这不是在函数中实现的......如果你传递一个结束索引(int high)大于数组的最大大小怎么办! ! ...这将导致访问数据超出范围(这在 C 中非常危险)因此为避免此错误,您必须更改函数签名以传递数组的最大大小,然后函数 Prototype 将类似于 int findMinIndex(int data[], int maxSize,int low, int high); 然后你可以像这样添加更多的 chek condetionif (high > maxSize) { return -3; }

如果错误仍然存​​在,您可以提交输入 schedule.in 文件!