any flaws in this sorting code?

hi guys I made a small algorithm to find the smallest element and number of the element in an array,but just a question can you see any flaws in this way I did it and is there any ways I can improve this code if so how?

Thanks people

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
  #include <iostream>

using namespace std;

int findSmallest(int values[],int index,int size){

     int smallest = index;

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

        if (values[i] < values[smallest]){

            smallest = i;

        }
    }
    cout << "smallest number is " << values[smallest] << endl;
    return smallest;
}

int main()
{
   int ray[5];
   for(int i = 0;i <5;i++){

       cout << "enter values" << endl;
       cin >> ray[i];
   }

   cout << findSmallest(ray,0,5);
}
Only that lines 17 and 18 belong in main.

Why is 'index' a formal argument?

[edit] Oops. Fixed.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;
int findSmallest(int values[], int size)
{ 
     int smallest = values[0];
    for(int i = 1;i < size; i++){
        if (values[i] < smallest){
            smallest = values[i];
        }
    }
    cout << "The smallest number is ";
    return smallest;
}

int main()
{
   int ray[5];
   for(int i = 0;i <5;i++){
       cout << "Enter values (" << i+1 << ") : ";
       cin >> ray[i];
   }
   cout << findSmallest(ray,5); return 0;
}


Remarks : Your code is full of flaws (pun intended)
Last edited on
I use index as an arqument because when I don't use it as one my code does not work the way I want it to

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

  #include <iostream>

using namespace std;

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

     int smallest = values[0];

    for(int i = values[0] + 1;i < size; i++){

        if (values[i] < values[smallest]){

            smallest = i;

        }
    }
    cout << "smallest number is " << values[smallest] << endl;
    return smallest;
}

int main()
{
   int ray[5];
   for(int i = 0;i <5;i++){

       cout << "enter values" << endl;
       cin >> ray[i];
   }

   cout << findSmallest(ray,5);
}




so just say if I enter 1,2,3,4,5
it prints the second number 2 as the smallest(I don't know why it does that but it does)

any ideas how come that happens ^^
My solution is here. See above.
duoas wrote:
Only that lines 17 and 18 belong in main.

Line 18 should remain in findSmallest(). The function needs to return the smallest index.
@AbstractionAnon
The OP didn't make it clear whether to find the smallest number or find the smallest index. Look at how OP named the function : findSmallest(){}
it prints the second number 2 as the smallest(I don't know why it does that but it does)

Look at line 8: You're setting the index the smallest number to the value of the first entry in the array. You want to set the smallest index to 0. Then at line 10, you want to start the for loop from 1. Not value[0]+1. closed account had this correct in the code he posted above. It would have been clearer if you had named "smallest", "smallest_index" instead.

As closed account also pointed out, there seems to be some confusion over whether you want to return the smallest number, or the index of the smallest entry. Line 19, you're returning the smallest index. Line 31, you're printing the smallest index, not the smallest value.

Last edited on
I don't think there was any confusion at all that he was returning the smallest index.
That said, a small comment at the top of the function indicating that it returns an index and not a value might be useful to readers.

Again I repeat that there is nothing wrong with your original code (except for the misplaced cout). It is a very efficient O(n).
Last edited on
Thanks guys for the help credit to all of you guys to help me sorting that out now I just need to solve the bigger picture using this in my insertion sort code
Topic archived. No new replies allowed.