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,
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.
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]??
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++
scratch that after experimenting with different numbers both of those solutions well what I thought were solutions still do not print the biggest number =(
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!
#include<iostream>
usingnamespace std;
int main()
{
int *arr, size, first = 0, store = 0;
cout << "\nEnter the Size of the array : ";
cin >> size;
arr = newint[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];
elsecontinue;
}
cout << "\nThe Smallest Number is : " << store << endl;
for (int i = 1; i < size; i++)
{
if (arr[i] > first)
store = arr[i];
elsecontinue;
}
cout << "\nThe Greater Number is : " << store << endl;
system("pause");
}