Issue
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; }
Solution
implement of the function
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;
}
update :
i updated the code to return negitve value and print some error msg when error happen you have to handle this errors in function accourding To your logiC .. because if Function invoked with wrong arguments it will have wrong behavior ... in C language it not support error exception so i just return negative value indecate that funciont not worrking as expected...
if you don't like to print to console from function directly and you like to only print messages form the main function you code do like that
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);
Types of Error maybe happen inside findIndex()
all error types could happen is an argument errors.. and it come when passing a wrong argument to function ... your queries inside the file maybe is a bad queries so you have to put some error catching technique and handling in the program
- first error is if you passing start index (
int low
) bigger than end index (int high
) so here is not logical ... so this must be an error ...you could swap them or handle it as you like - seconed error is if you pass a
null
array this is obviously an error because you can't access any data - the third error this is not implemented in function ... what if you pass an end index(
int high
) bigger than the maximum size of the array !! ... this will cause acces data out of range (and this is very dangerous in C) so to avoid this error you have to change the function signature to pass the maximum size of array then the function Prototype will be likeint findMinIndex(int data[], int maxSize,int low, int high);
and then you could add more chek condetion like thisif (high > maxSize) { return -3; }
you could submit the input schedule.in
file if the error still exist !
Answered By - Ibram Reda Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.