Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (pradius):

I need a algorithm to find the smallest number in an array. C++

OpenStudy (espex):

If you do not have a sorted list, you will probably end up using a bubble sort.

OpenStudy (pradius):

I need it to make a sorted list out of an unsorted list.

OpenStudy (pradius):

so far I have something like this.. for (x=0; x<10; ++x){ if (UnList[x] < s){ s = UnList[x]; } else if (++UnList[x] < s){ s = UnList[x]; } cout << s << "," ; } I not sure if its really working or not..

OpenStudy (pradius):

hahah no.. its not lol

OpenStudy (espex):

Presumably you have initiated 's' to some number greater than you expect to find in your array?

OpenStudy (pradius):

no, cause I not expecting what the user is going to input.

OpenStudy (espex):

int s = 1000; int x; for(x=0; x<10; x++){ if (unList[x] < s) { s = unList[x]; } } cout << s << endl;

OpenStudy (espex):

So your user is going to put in a number and you are going to get the array from?? and spit out the number smaller than the user input?

OpenStudy (espex):

Perhaps you could start by stating what it is you are trying to accomplish.

OpenStudy (pradius):

#include <iostream> #include <string> using namespace std; int main() { int UnList[10]; int SList[10]; int i,j,n,x,s; int nVal; // Ask user to insert the values for the Unsorted List. cout << "Please insert 10 integers numbers:" << endl; for(i=0; i< 10; ++i){ cout << "[" << i <<"]" << "= "; cin >> nVal; cout << endl; UnList[i]={nVal}; }// end of loop for storing the values. //Display the Unsorted List cout << "Unsorted List: "; for(j=0; j<9; ++j){ cout << UnList[j] << ","; } cout << endl; cout << "Sorting ..."; cout << endl; //Selection Sort Algorithm for (n = 0; n <9; ++n){ for (x=0; x<10; ++x){ if (UnList[x] < s){ s = UnList[x]; } else if (++UnList[x] < s){ s = UnList[x]; } SList[n] = {s}; }// end of the Linear Search method... } //end of the Sorting Method... //Display Sorted List cout << "Sorted List: "; for(i = 0; i<9; ++i){ cout << SList[i] << " "; } }

OpenStudy (pradius):

that is the code. Tasks: 1) Ask the user to insert 10 integer number and store them in a unsorted array. 2) use the Sorting Method to sort the array. 3) Display the Sorted Array.

OpenStudy (pradius):

OK, I think my method is working .. but the output is not what I want..

OpenStudy (espex):

The first thing you will want to do is assign an index to your 's', s=UnList[0]; Then when you step into your array you are just swapping.

OpenStudy (espex):

Here is a bubble sort I wrote for a project. for(i = 1; i <= numLength; i++) { for (j=0; j < (numLength -1); j++){ if (t_array[j+1] > t_array[j]){ temp = t_array[j]; // swap elements t_array[j] = t_array[j+1]; t_array[j+1] = temp; } } }

OpenStudy (pradius):

Nice. I used it to find the smallest.. cause I think u have it set it up to find the largest. Thanks, it helped .. still Im not getting the desired output, now Im working to find out why

OpenStudy (espex):

Here is your array sorted small to large. http://ideone.com/JOgmWX

OpenStudy (espex):

I had to change the last for loop to print out all the values: http://ideone.com/alTSNf

OpenStudy (espex):

The code does not presently preserve the unsorted list, but that wouldn't take much to fix if you wanted it saved.

OpenStudy (pradius):

thanks!!, Im not gonna see it yet.. I'll try to find it out by myself then either I give up or not I check ur code :)

OpenStudy (espex):

Good on ya, put in the work and feel better about the win. :D

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!