Write a function that takes as input two arrays of type Double say A and B of th
ID: 3721730 • Letter: W
Question
Write a function that takes as input two arrays of type Double say A and B of the same size. The function will find all the min values in A and save their indices in B in that order. Finally, the function returns the number of min values found. For instance, if A = [9.77 76.3 14.7 6.49 3.15 76.3 21.5 76.3] and B = [0 0 0 0 0 0 0] initially, then after the function is called, B will become [0 0 0 0 5 0 0 0] (as they are the indices of 3.15 – the maximum value in A). Finally, the function returns 1 as it indicates how many min values found.
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
int getMinCount(double a[], double b[], int n) {
double min = a[0];
int c=0;
for(int i=0;i<n;i++){
if(min > a[i])
min = a[i];
}
for(int i=0;i<n;i++){
if(min==a[i]) {
b[i]=i+1;
c++;
}
}
cout<<"Minimum Value: "<<min<<endl;
return c;
}
int main()
{
double A[8] = {9.77, 76.3, 14.7, 6.49, 3.15, 76.3, 21.5, 76.3} ;
double B[8]={0,0,0,0,0,0,0,0};
double min = getMinCount(A,B,8);
cout<<"Number of Mins: "<<min<<endl;
cout<<"Indices array: "<<endl;
for(int i=0;i<8;i++){
cout<<B[i]<<" ";
}
cout<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.