Smallest double from array

Hi let me re-state. Why does the first function work and not the second? When I try to find the smallest area it returns either 0 (i initialised to 0) or 1999.99 (i initialised to 1999.99). However, the largest area function works fine.
//this returns the correct amount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void largestAreas (int areasArray[], int arraySize)
{
	double max = 199.99;
	for(int i =0; i<arraySize; i++)
	{
		if(areasArray[i]>max)
			max = areasArray[i];
	}
	cout<<"The largest area is: "<<max<<endl;

}
//******************************************************************************
void smallestAreas (int areasArray[], int arraySize)
{
double min = 1999.99;
	for(int i =0; i<arraySize; i++)
	{
		if(areasArray[i]<min)
			min = areasArray[i];
	}
	cout<<"The smallesr area is: "<<min<<endl;
}
Last edited on
I think you may have your syntax wrong. That function doesn't return anything, nor does it display the value of min. It prints the address of min, which is probably some unrelated number.
I think you need another variable to keep track of the index. Set min to the first element in the array and then check everything else against it.
Also shouldn't min be an int rather than a double, since that's the array type?

1
2
3
4
5
6
7
8
9
10
11
int min = areasArray[0];
int min_index = 0;
for(int i=0; i<arraySize; i++)
{
	if(areasArray[i]<min)
        {
		min = areasArray[i];
                min_index = i;
        }
}
cout<<" The index of the smallest area is: "<<min_index<<endl;
Last edited on
You've got an array of ints (areasArray[]), so why are you comparing them against values of type double (max/min)? I think already that's going to cause some weird behaviour.
I have changed them to double now but the same problem is still occurring. Can't seem to get the smallest number. Any idea why?
It's maybe a mistake somewhere else in your program. Are you calling the functions with valid data?
Here's a small example which more or less copies your code, except I initialize the variables min and max to the first element in the array before starting the loop.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

void find_smallest( int a[], int n );
void find_largest( int a[], int n );

int main()
{
    int a[] = { 14, 73, 81, 9, 10, 12, 5, 11, 100, 94 };
    int n = 10; // array size

    find_smallest( a, n );
    find_largest( a, n );

    return 0;

} // main

void find_smallest( int a[], int n )
{
    int smallest = a[0];
    for ( int i = 0; i < n; ++i )
    {
        if ( a[i] < smallest )
        {
            smallest = a[i];
        } // if
    } // for
    std::cout << "smallest=" << smallest << "\n";

} // find_smallest

void find_largest( int a[], int n )
{
    int largest = a[0];
    for ( int i = 0; i < n; ++i )
    {
        if ( a[i] > largest )
        {
            largest = a[i];
        } // if
    } // for
    std::cout << "largest=" << largest << "\n";

} // find_largest 
Hi thanks for your help. I really appreciate it. I've tried it and unfortunately I keep getting zero. I have to have a better look at my program but I think the zero is coming from the way the variable is being initialised. Thanks again :)
Topic archived. No new replies allowed.