to find the minimum

Hi!! anyone can give me hint how to start this.
here the function that im using , but I dont know how to start
int findMin(int A[20], int i, int j); and im looking to find the index of the minimum element of the array
thanks
[codee.
[/code]
closed account (48T7M4Gy)
1. set minimum = A[0]
2. Loop for all index = i
- if A[i] < minimum then minimum = A[i], x= i
3. On completing the loop, A[x] is the minimum and x is the index of the minimum. QED :)
thanks a lot , but what about j?
you dont need j
Not sure what the function requires. But the signature could be find min value between i-th and j-th element.
closed account (48T7M4Gy)
j might be to pass or 'return' the index and int would be to return minimum or vice versa.
But realistically all you need is the relevant index and the rest falls into place. j is redundant.
If you designed the function interface: int findMin(int A[20], int i, int j); , change the names of i and j so that they indicate their purpose. If that was given as part of a homework assignment, then the instructor should change the names or include the information in the function's comment header.

I can guess two reasonable designs and thus the interface is ambiguous:
int findMin(int A[], int size, int start) or int findMin(int A[20], int start, int end) /* Which is what liuyang is assuming. */ If I am able to change the types slightly, another possibility is int findMin(int A[20], int start, int & minidx) /* Which is one option in kemort's second post. */ See how giving different names for the parameters other than i or j could communicate a lot of meaning (in English at least).
thank you guys , but not working for me , im coding in c.

#include<stdio.h>

int findMin(int A[20],int i,int j);
int main ()
{ int myArray[20];
int A[20];
int i= 0 ;
int j= 0;
printf("Enter the numbers: \n ");
int k ;
for (k = 0 ; k < 19 ; k++)
{
scanf(" %d \n",&myArray[k]);
}
int n ;
for(n = 0; n< 19; n++)
{
printf(" %d ",findMin(A,i,j));
}

return 0;
}
//=============================

int findMin(int A[20],int i,int j)
{
int min ;
int x;
min = A[0];
j = A[19];
for (i = 0 ; i < j; i++) {
if (A[i] < min )
min = A[i];
x = min;
}
return x;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

int findMin(int[], int);
int main()
{
	int Z[] = { 1, 2, 3, 5, 4, 8, 2, 34, 22, 56, 44, 22, 198, 34, 1, 7, 55, 33, 99, 4 };
	int minimum = findMin( Z, 20 );
	std::cout << minimum << std::endl;
	return 0;
}

int findMin(int A[], int length)
{
	int min;
	min = A[0];

	for (int i = 0; i < length; i++)
	{
		if (A[i] < min)
			min = A[i];
	}
	return min;
}
Last edited on
closed account (48T7M4Gy)
The other way using the index is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>

int findMin(int[], int);
int main()
{
	int Z[] = { 11, 2, 3, 5, 4, 8, 2, 34, 22, 56, 44, 22, 198, 34, 1, 7, 55, 33, 99, 4 };
	int indexOfMinimum = findMin(Z, 20);
	std::cout << Z[indexOfMinimum] << std::endl;
	return 0;
}

int findMin(int A[], int length)
{
	int min;
	int index = 0;
	min = A[0];

	for (int i = 0; i < length; i++)
	{
		if (A[i] < min)
		{
			min = A[i];
			index = i;
		}
	}
	return index;
}
Topic archived. No new replies allowed.