I'm learning about functions with pass by reference.
My question is how can I write a function that will find the index with the lowest value that was taken from an input text file.
The text file contains the min and the max index to create a range. the function should return the index with the lowest value stored in that range. Then function should also check for indexes with the same value within range and then return the first one of those indexes.
example
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
If min index read from input text file was 0 and max index read from text file was 11, the return should be index 1.
Function Prototype used int findMinIndex(int data[], int low, int high);
Full program
#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
}
Expected output link
UPDATE: I have implemented Ibram Reda's function and the program is printing similar indexes to the expected output, but is not the exact same for some reason.
This is the updated code and I have also attached a link to the output file.
#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;
}
low = 0, you're losing the information you got from the caller. Either use a different iterating variable, or incrementlowlike you do now but without zeroing it in advance. - Al.G.&or pointers calculations) or automatically (relying on automatic conversion of arrays), what is passed is a thing that does refer to the object intended, so it is a reference in both an English sense and a technical non-C++ sense. - Eric Postpischil