Pradius
  • Pradius
I need a algorithm to find the smallest number in an array. C++
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
eSpeX
  • eSpeX
If you do not have a sorted list, you will probably end up using a bubble sort.
Pradius
  • Pradius
I need it to make a sorted list out of an unsorted list.
Pradius
  • 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..

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

Pradius
  • Pradius
hahah no.. its not lol
eSpeX
  • eSpeX
Presumably you have initiated 's' to some number greater than you expect to find in your array?
Pradius
  • Pradius
no, cause I not expecting what the user is going to input.
eSpeX
  • eSpeX
int s = 1000; int x; for(x=0; x<10; x++){ if (unList[x] < s) { s = unList[x]; } } cout << s << endl;
eSpeX
  • 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?
eSpeX
  • eSpeX
Perhaps you could start by stating what it is you are trying to accomplish.
Pradius
  • Pradius
#include #include 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] << " "; } }
Pradius
  • 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.
Pradius
  • Pradius
OK, I think my method is working .. but the output is not what I want..
eSpeX
  • 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.
eSpeX
  • 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; } } }
Pradius
  • 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
eSpeX
  • eSpeX
Here is your array sorted small to large. http://ideone.com/JOgmWX
eSpeX
  • eSpeX
I had to change the last for loop to print out all the values: http://ideone.com/alTSNf
eSpeX
  • eSpeX
The code does not presently preserve the unsorted list, but that wouldn't take much to fix if you wanted it saved.
Pradius
  • 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 :)
eSpeX
  • eSpeX
Good on ya, put in the work and feel better about the win. :D

Looking for something else?

Not the answer you are looking for? Search for more explanations.