exact same code won't work

I'm to find the smallest index of where a number is at in a for loop but it just won't give me the output I want,YET when I used the exact same code yesterday for the same problem it worked and the thing is the code is pretty much the exact same yet the one I did yesterday prints out the smallest index what I want it to do YET this one isn't doing the same even though the code is the same c++ can be frustraiting.

todays code which will not work

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>

using namespace std;

int main()
{
   int values[3];
   int index = 0;

   cout << "enter numbers" << endl;

   for(int i = 0;i<3;i++){

       cin >> values[i];

       index = values[0];

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

            index = i;

       }

   }

   cout << index;
}




Code yesterday which works fine

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


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

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

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

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

            index = i;
          }


    }
       return index;
}
this is the problem:
index=values[0]
Ok I figured out the problem it's because the return is not before the last curly brace BUT how come I get a completely different output if I put the return index; in the for loops scope??

example VV

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

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

     int index;
     index = 0;
     for(int i = 0;i<size;i++){

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

              index = i;

          }

         return index;
     }
}




VS

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

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

     int index;
     index = 0;
     for(int i = 0;i<size;i++){

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

              index = i;

          }

         
     }

return index;

}






Last edited on
Return ends the execution of your function. The first example will always return 0 because the second iteration of your loop will never run
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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

int findSmallest(int[],int);

int main()
{
    int values[3];
    int index_of_smallest = 0;
    
    std::cout << "enter numbers" << std::endl;
    
    for(int i = 0; i < 3; i++)
    {
        std::cin >> values[i];
        
        if(values[i] < values[index_of_smallest])
            index_of_smallest = i;
    }
    
    std::cout << index_of_smallest << " " << values[index_of_smallest] << '\n';
    
    index_of_smallest = findSmallest(values, 3);
    std::cout << index_of_smallest << " " << values[index_of_smallest];
    
    return 0;
}

int findSmallest(int ray[], int size)
{
    int ndx_smallest = 0;
    
    for(int i = 0; i < size; i++)
    {
        if(ray[i] < ray[ndx_smallest])
            ndx_smallest = i;
    }
    return ndx_smallest;
}
Last edited on
@adam2016 - I gave you the solution here:
http://www.cplusplus.com/forum/beginner/192645/#msg927899

@kemort: line 37 - If you find a smaller entry, you never update smallest.
closed account (48T7M4Gy)
@kemort: line 37 - If you find a smaller entry, you never update smallest.

What's the fix for line 37? :]


enter numbers
77
88
99
0 77
0 77 

enter numbers
88
99
77
2 77
2 77 

enter numbers
88
77
99
1 77
1 77 
Thanks guys

and ok thanks DrZoidberg that makes sense that return would end the loop therefor not iterating again
@kemort - See the link I gave adam2016 in my previous post.
closed account (48T7M4Gy)
@ Abstract

Yeah, I saw that. The approach I decided to take is I only need to know the index of the smallest and the rest falls into place. Each to their own I guess. :)
@kemort - The problem with your approach is you're always comparing ray[i] with smallest, but you're never updating smallest if you find a smaller entry.
kemort wrote:
What's the fix for line 37? :]
[Bunch of correct results elided, implying the code is correct]


enter numbers
99
77
88
2 88
2 88


Sometimes you need to choose the correct test data.
closed account (48T7M4Gy)
@Abstract
You're right ... but now fixed the way I intended
Last edited on
Topic archived. No new replies allowed.