[try Beta version]
Not logged in

 
finding smallest number to no avail

Jun 10, 2016 at 10:38pm
greetings,I'm trying to make a program which finds the smallest number in an array of numbers yet everytime when I run the program the result I get(number that is returned) is 1 all the time even if i don't enter 1 in the values 1 will still be returned,

what am I doing wrong here

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

using namespace std;

int findSmallest(int ray[],int size,int index);

int main()
{
    int values[10];
    cout << "enter 10 values" << endl;

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

        cin >> values[i];

    }

    int display;
    display = findSmallest(values,10,0);
    cout << display;

}

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

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

            if(i > smallest){

                smallest = i;
            }
            return smallest;
      }
 }
Last edited on Jun 10, 2016 at 10:45pm
Jun 10, 2016 at 10:59pm
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
#include <iostream>

using namespace std;

const int size = 10;

int findSmallest(int ray[]){
	int smallest = ray[0];

	for(int i = 1; i < size; i++){
		if(ray[i] < smallest){
		    smallest = i;
		}
	}
	return smallest;
}

int main(){
    int values[size];
    
    cout << "enter 10 values" << endl;
    for(int i = 0;i<10;i++){
        cout << "Value " << i << ": ";
        cin >> values[i];
    }
    cout << endl;

    int display;
    display = findSmallest(values);
    cout << display << endl << endl;

	return 0;
}
Jun 10, 2016 at 11:04pm
Currently you are comparing the iterator of the for-loop for smallest number rather than the numbers in the array.
1
2
3
4
if(i > smallest){

     smallest = i;
}


where i is the iterator of your for-loop. You want to instead use this to reference all the indices of your array that you will loop through.

Did you choose the function prototype for your findSmallest function or did a teach/professor require you to use it?

If you get to choose your own function prototype, then I would suggest just passing in the first two parameters:

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

instead of

int findSmallest(int ray[],int size,int index);

Your function can then return either the smallest number or the index in the array that references the smallest number.

Jun 10, 2016 at 11:07pm
Well it looks like PBachmann already answered it for you. He changed the function definition as well, so if you are in some way supposed to use that third parameter you will need to explain in more detail.

Using const int size = 10; allows you to not need as many parameters for your function, but also makes your function less robust. It is up to you to decide which is more useful.

For PBachmann, might as well change the main to

1
2
cout << "enter " << size << " values" << endl;
for(int i = 0;i<size;i++)

just to be consistent.
Last edited on Jun 10, 2016 at 11:11pm
Jun 10, 2016 at 11:18pm
Thanks guys from what I hear it's called the insertion sort algorithm unfortuantly Alex Allain didn't really explain it in detail in his book I really need to have a look over PBachmann's code and study it =) thanks guys
Jun 11, 2016 at 12:11am
Hey adam2016, that makes a lot more sense now! The assignment is asking you to sort your array via the insertion sort algorithm, so studying PBachmann's answer is not going to be too useful to you. While he/she are wrote what we thought you were trying to do correctly, we now know you are actually trying to implement something else.

I would study how insertion sort works, and then try implementing it.
Last edited on Jun 11, 2016 at 12:11am
Jun 11, 2016 at 12:17am
thanks man

do you know any good tutorials online for insertion sort?
Jun 11, 2016 at 12:34am
None in particular sorry! However I do suggest you look up "visualize insertion sort" and look at some of the visual animations of what is going on in the insertion sort. When I was learning about insertion sort, actually seeing the array change was what made it click.

There are other threads of people asking about insertion sort, so you could look at those as well:
http://www.cplusplus.com/forum/beginner/3322/
http://www.cplusplus.com/forum/beginner/184950/
Jun 11, 2016 at 12:36am
Jun 11, 2016 at 12:38am
I figured out how to get the smallest number I guess that's a start just where to go from here is the question

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


#include <iostream>

using namespace std;

int findSmallest(int ray[],int size);
void sortRay(int ray[],int size);

int main()
{
    int values[10];
    cout << "enter 10 values" << endl;

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

        cin >> values[i];

    }

    int display;
    display = findSmallest(values,10);
    cout << display;

}

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

           int smallest = ray[0];

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

            ray[i];
            if(smallest > ray[i]){
                
                smallest = ray[i];
            }
           }

           return smallest;
 }


Thanks guys I'm going to get some rest I'll finish this in the morning =)
Last edited on Jun 11, 2016 at 12:45am
Jun 11, 2016 at 12:47am
This is another way to avoid function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  
   int main () 
 {
    int nr , smallest , v[101] , i ;        // nr = number of numbers you have in that array 
         cin>>nr;                                // v[]  =  is the array ; 
for(i=1;i<=nr;i++)          
         cin>>v[i];  
    smallest = v[1];                              //first , smallest will be the first position , than you 
for(i=2;i<=nr;i++)                            //compare the whole array with it to check if there 
 if( v[i] < smallest )                           // are  smaller numbers 
     smallest = v[i];

cout << smallest; 

return 0; }     

Last edited on Jun 11, 2016 at 12:49am
Jun 11, 2016 at 10:10am
Best tutorial on insertion sort: https://www.youtube.com/watch?v=i-SKeOcBwko
Topic archived. No new replies allowed.