finding biggest and smallest values in array

hi guys I wrote some code to find the smallest and biggest values in an array the smallest function does just that it finds the smallest number and I've tested it with different variables but the problem is with the find the biggest funtion it does not print the biggest number out but for some reason prints out the second biggest I've tried going through the logic but it still does not make sense why it's not printing out the biggest number,

Thanks



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include <iostream>

using namespace std;

int findSmallest(int ray[],int size){

      int index = 0;
      int smallest = ray[0];

      for(int i = 1; i < size; i++){


          if(ray[index] > ray[i]){

               smallest = ray[i];
               index++;
          }

      }

      return smallest;
}

int findBiggest(int ray[],int size){

     int index = 0;
     int biggest = ray[0];

     for(int i = 1;i < size; i++){


        if(ray[index] < ray[i]){


            biggest = ray[i];
            index++;

        }
     }


  return biggest;
}


int main()
{

        int aray[5];
        aray[0] = 5;
        aray[1] = 2;
        aray[2] = 3;
        aray[3] = 9;
        aray[4] = 7;

        int one = findSmallest(aray,5);
        cout << one << endl;

        int two = findBiggest(aray,5);
        cout << two;

}
Last edited on
I threw some cout statements into the code to print out the intermediate values in the function. You can see what happens in that last part - index gets incremented after the value of biggest changes, but the next comparison isn't comparing the current biggest, it's comparing a different number which happens to be 2 in this test data.

1
2
3
4
5
6
7
biggest starts at 5
now in for loop
i is 1, index is 0 : ray[index] 5 vs. ray[i] 2 biggest is now 5
i is 2, index is 0 : ray[index] 5 vs. ray[i] 3 biggest is now 5
i is 3, index is 0 : ray[index] 5 vs. ray[i] 9 biggest is now 9
i is 4, index is 1 : ray[index] 2 vs. ray[i] 7 biggest is now 7
7 


I think you can simplify this a bit by not using a separate index variable. Just initialize biggest to the first element of the array and compare that to every other element. If a larger value is found, update biggest.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
int findBiggest(int ray[],int size){
    //int index = 0;
    int biggest = ray[0];
    
    for(int i = 1;i < size; i++){
        if( biggest < ray[i]) { //ray[index] < ray[i]){
            biggest = ray[i];
            //index++;
        }
    }
    return biggest;
}
thanks for all the advice guys

I got the code working which is good but I don't understand why it's working now this is the new code which gets the biggest number correctly

1
2
3
4
5
6
7
8
9
10
11
12
13
14

for(int i = 1;i < size; i++){


        if(ray[i-1] < ray[i]){


            biggest = ray[i];


        }
     }




why does that code work as opposed to my other code all I did was change ray[index] with ray[i-1] I thought they were pretty much the same because ray[index] =ray[0] and ray[i-1] = ray[0]??

also

when I put index outside the if statement it also seems to work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


int index = 0;
     int biggest = ray[0];

     for(int i = 1;i < size; i++){


        if(ray[index] < ray[i]){


            biggest = ray[i];


        }

        index++;



but when I do the same with the smallest function I don't get the smallest so the question is how come I get the biggest when I put the index++ after the if statement yet I only get the smallest when the index++ is inside the if statement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


int index = 0;
      int smallest = ray[0];

      for(int i = 1; i < size; i++){


          if(ray[index] > ray[i]){

               smallest = ray[i];
               

          }

index++ 


Last edited on
closed account (48T7M4Gy)
why does that code work

I think you might still be in trouble. If I swap the 5 and 9 in the array, the biggest is 7 :(

I suggest you write the answers down on paper as you manually go through the array. Try it for the choices you have from the various posts.

Believe me, we have all had the same experience, but the half-hour involved in the manual check with your 5 numbers will pay off.
scratch that after experimenting with different numbers both of those solutions well what I thought were solutions still do not print the biggest number =(
good idea Kemort will do
closed account (48T7M4Gy)
Like this:

step 1: numbers are 5 2 3 9 7 biggest bucket holds first no. 5
step 2: take 2nd no., 2, is that bigger than no. in bucket? No
step 3: take 3rd no., 3, is that bigger than no. in bucket? No
step 4: take 4th no., 9, is that bigger than no. in bucket? Yes, put 9 in the bucket
step 5: take 5th no., 7, ... no ...
... no more numbers left in array, so what's in the bucket ... 9 bingo!
closed account (48T7M4Gy)
PS: now do the same with your algorithm :)
thanks Kemort will give it a shot I've wrote down the code and now I see why it does not work =) now just going to find the solution
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
#include<iostream>
using namespace std;

int main()
{
	int *arr, size, first = 0, store = 0;
	cout << "\nEnter the Size of the array  : ";
	cin >> size;
	arr = new int[size];
	for (int i = 0; i < size; i++)
	{
		cin >> arr[i];
	}

	first = arr[0];

	// minimum

	for (int i = 0; i < size; i++)
	{
		if (first>arr[i])
			store = arr[i];
		else
			continue;
	}
	cout << "\nThe Smallest Number is : " << store << endl;
	for (int i = 1; i < size; i++)
	{
		if (arr[i] > first)
			store = arr[i];
		else
			continue;
	}

	cout << "\nThe Greater Number is : " << store << endl;
	system("pause");
}

good solution Bird,thanks for the input
closed account (48T7M4Gy)
A small embellishment:
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
#include <iostream>

void display_Big_Small(int ray[], size_t size)
{
    
    int biggest = ray[0];
    int smallest = ray[0];
    
    for(size_t i = 1; i < size; i++)
    {
        if( ray[i] < smallest)
            smallest = ray[i];
        
        if( ray[i] > biggest)
            biggest = ray[i];
    }
    
    std::cout << " Biggest = " << biggest << '\n';
    std::cout << "Smallest = " << smallest << '\n';
}

int main()
{
    int aray[] = {5,2,9,-1,3,7};
    display_Big_Small(aray, sizeof(aray)/sizeof(int));

    return 0;
}
Biggest = 9
Smallest = -1
Program ended with exit code: 0
The next step could be: http://www.cplusplus.com/reference/algorithm/minmax_element/

(That, however, hides the logic and were are here interested in the logic; not numbers big or small.)
thanks guys much appreciated for all the help
Topic archived. No new replies allowed.