0
 #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.

2
  • @CoolGuy so what you are saying is that i should add it in the main function or the "find_min_index"??? Commented Sep 24, 2015 at 16:07
  • Please indent your code. Commented Sep 24, 2015 at 16:12

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

your final printf should not have a "length" parameter of 11, it should be 10 - since OPs expected output was 1 (11 would give output of 0)
@JosephDuty I will mention it . Thanks !!
@WeatherVane Thanks Sir !! . Fixed that.
0

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

4 Comments

I think that output is no correct . You see min values in 3rd array is 0 or 1 as OP wants but you get 4 . You should length in the function calls
@ameyCU Right, it was a typo.
Having initialised min fom the first element, you can start the loop from 1.
@WeatherVane yes, you can.
-1

Before your for loop start just initialize min=0; It could solve your problem.

2 Comments

Right now also it is initialized as 0 .
No, min should be initialised either to the first element, or to INT_MAX. 0 is wrong answer if all the element values are > 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.