76.7k views
5 votes
Write a complete function called lowestPosition() which takes as array (of double) and the number of elements used in the array (as an int) and returns the position of the element with the least value (as an int). You may assume the number of elements is >

1 Answer

6 votes

Answer:

#include <bits/stdc++.h>

using namespace std;

int lowestPosition(double elements[],int n)//function to find position of lowest value element.

{

int i,pos;

double min=INT_MAX;

for(i=0;i<n;i++)

{

if(elements[i]<min)

{

min=elements[i];

pos=i;//storing index.

}

}

return pos+1;//returning pos +1 to give the correct position in the array.

}

int main() {

double ele[500];

int n;

cout<<"Enter number of values"<<endl;

cin>>n;

cout<<"Enter elements"<<endl;

for(int i=0;i<n;i++)

{

cin>>ele[i];

}

int pos=lowestPosition(ele,n);//function call.

cout<<"The position of element with minimum value is "<<pos<<endl;

return 0;

}

Output:-

Enter number of values

5

Enter elements

4 6 7 5 1

The position of element with minimum value is 5

Step-by-step explanation:

I have created a function lowestPosition() with arguments double array and it's size.I have taken a integer variable pos to store the index and after finding minimum and index i am returning pos+1 just to return correct position in the array.

User Pranay Kumbhalkar
by
4.8k points