Home > Net >  Find indexes of multiple minimum value in an array in c
Find indexes of multiple minimum value in an array in c

Time:11-10

Please don't write that I should do my homework myself. I have tried but cannot find the answer.

Input:

arr[11]={1,2,3,4,1,5,6,7,8,1,9}

Output:

0 4 9

Returned position numbers are to be inserted into another array, I mean:

arr_min_index[HowManyMinValues]={0,4,9}

I tried to end it in many different ways, but I failed.

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout<<"How many elements should be in the array?"<<endl;
    cin>>n;
    int arr[n];
    int i, j, MIN = arr[0], position=0, how_many;
    for(i=0; i<n; i  )
    {
        cin>>arr[i];
    }
    for(i=0; i<n; i  )
    {
        if(arr[i]<MIN)
        {
            MIN = arr[i];
            how_many=0;
        }
        if(tab[i]==MIN)
        {
            how_many= 1;
        }
    }
}

CodePudding user response:

Whenever you don't completely understand a problem, try to decompose it into tiny bits that you can handle. In C/C you do this by implementing functions that do a single job. Look at your main code: it handles the console, processes input and probably does the output next. If you decompose your task in natural words, then there are the following jobs to do:

  1. Get an array with an arbitrary count of numbers
  2. Figure out the minimum in this array
  3. Figure out, how often the minimum from 2) is in the array
  4. Allocate memory (array) with the result from 3)
  5. Find all instances of the minimum and put the indexes into the result array you got from 4)
  6. Output the results

Thus you should come up with 6 methods and a main function which calls those 6 functions.

This main function will look like this:

int main(...)
{
    int num_input;
    int* arr    = get_input_array(&num_input);    // 1. Returns an array and the number of items inside the array
    int min     = find_minimum(arr, num_input);   // 2. ...
    int times   = how_often(arr, num_input, min); // 3. ...
    int* result = allocate_output(times);         // 4. ...
    find_indexes(result, arr, num_input, min);    // 5. ...
    send_output_array(result, times);             // 6. ...
}

Now there are quite a few items which can be optimized. For example, if you are allowed to use std::vector, you don't have to handle num_input and the like as std::vector always knows, how many items it holds. And you could also do some of the steps in one method. But I guess that's not the idea. A real implementation would also need to deallocate memory from steps 1 and 4. But never mind - memory leaks can be handled by the runtime library this time.

CodePudding user response:

Actually you are almost there, if you are new to c/c , it is a good start.

only two modification on you code could make it work:

#include <iostream>
// using namespace std; // is not a good practice
int main()
{
    int n;
    std::cout<<"How many elements should be in the array?"<<std::endl;
    std::cin >> n;
    int arr[n]; 
    int idx[n]; // define an array to store indices, maximum same size as input
    int i, j, position=0, how_many = 0;
    for(i=0; i<n; i  )
    {
        std::cin>>arr[i];
    }
    int MIN = arr[0]; // select initial guess for minimjum after array is initialized
    for(i=0; i<n; i  )
    {
        if(arr[i]<MIN)
        {
            MIN = arr[i];
            how_many=0;
        }
        if(arr[i]==MIN)
        {
            idx[how_many  ] = i; //only in this line you can store your new idx and increment
        }
    }
    for(i=0; i < how_many; i  )
    {
        if(i == 0) {std::cout << "{" ;}
        std::cout<< idx[i] << ", ";
        if(i == 0) {std::cout << "}" << std::endl ;}
    }
}

I have indicated only lines which needed to be modified. but as suggested by other, it is better to use std::vectors and avoid unnecessary big array size, and more managed, safe code. However this is just to show that you thinking was correct and was almost close.

  • Related