Reputation: 21
#include <stdio.h>
int min=0;
int find_min_index(int numbers[], int length)
{
for(int a=0; a<length; a++)
{
if(numbers[a]<min)
min=numbers[a];
}
int main( int argc, char* argv[])
{
int data_array_1 = ( 1, 3, 5, 7, 9, 11};
int data_array_2 = ( 2, -4, 6, -8, 10, -12, 14, -16};
int data_array_3 = ( 6, 4, 1, 4, 5, 3, 2};
printf("Min's index array1 is: %d\n", find_min_index(array1, 6));
printf("Min's index array2 is: %d\n", find_min_index(array2, 9));
printf("Min's index array3 is: %d\n", find_min_index(array3, 7));
return 0;
}
the output i am getting is:
Min's index array1 is: 6
Min's index array2 is: 9
Min's index array3 is: 7
while the output i expect is:
Min's index array1 is: 1
Min's index array2 is: -16
Min's index array3 is: 1
cay you guys help me with getting the appropriate results.
Upvotes: 0
Views: 77
Reputation: 24294
I do not know how the code you have pasted in you question even compiled (you didn't pass the right arrays, you didn't declare the arrays in the right way - you should specify size of the array) but I made it work. The main problem is that you have initialized min
to 0. You should initialize it to the first element of the array passed to your function as the elements in your array may be negative (consider: {-1,1,2,3}
).
You were also passing 9
instead of 8
as length of the array:
printf("Min's index array2 is: %d\n", find_min_index(data_array_2, 9));
(the same applies for the function call with data_array_3
).
It is better to declare functions that take arrays in C as pointer types, that is: int find_min_index(int* numbers, int length)
.
Also, the correct output for the given input data will be:
Min's index array1 is: 1
Min's index array2 is: -16
Min's index array3 is: 0
Here is the fixed version of you function:
#include <stdio.h>
int find_min_index(int* numbers, int length) {
int min = numbers[0];
for (int a = 1; a < length; a++) {
if (numbers[a] < min) min = numbers[a];
}
return min;
}
int main(int argc, char* argv[]) {
int data_array_1[6] = {1, 3, 5, 7, 9, 11};
int data_array_2[8] = {2, -4, 6, -8, 10, -12, 14, -16};
int data_array_3[11] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
printf("Min's index array1 is: %d\n", find_min_index(data_array_1, 6));
printf("Min's index array2 is: %d\n", find_min_index(data_array_2, 9));
printf("Min's index array3 is: %d\n", find_min_index(data_array_3, 7));
return 0;
}
Upvotes: 0
Reputation: 16607
Here is solution -
#include <stdio.h>
int find_min_index(int numbers[], int length)
{
int a; // bring a and min inside function
int min=numbers[0]; // initialize min with array's 1st element
for(a=1; a<length; a++)
{
if(numbers[a]<min)
min=numbers[a];
}
return min; //return min from function
}
int main( int argc, char* argv[])
{
int data_array_1[]={ 1, 3, 5, 7, 9, 11}; //declare and initialize array
int data_array_2[]={ 2, -4, 6, -8, 10, -12, 14, -16};
int data_array_3[]={ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ,0};
printf("Min's index array1 is: %d\n", find_min_index(data_array_1, 6)); // calling function in printf
printf("Min's index array2 is: %d\n", find_min_index(data_array_2, 8));
printf("Min's index array3 is: %d\n", find_min_index(data_array_3, 11));
// printf("Min's index array3 is: %d\n", find_min_index(data_array_3, 10)); // in case you need this output as 1
return 0;
}
Note- Don't initialize min
with 0
as array with +ve
elements without 0
won't give correct output.
Upvotes: 1
Reputation: 795
1) You need to return min in your find_min_index, currently there is no return value.
2) Setting min to 0 wont give you the correct results if there are only > 0 values.
3) You don't have a close bracket for your find_min_index function, so that should not compile
Upvotes: 2
Reputation: 488
Before your for loop start just initialize min=0;
It could solve your problem.
Upvotes: -1