I know that this isn't a big issue but I just can't stand to see the style of declaring a variable at the start of your programs that is meant for your for-loops but is however being reset for each for-loop. And the way how you made your array and refer to its size is a little sketchy.
Also, how do you run your program? What IDE do you use? I don't see how you're running your program without closing on you but by opening it with cmd/command.com
#include <iostream>
// using namespace std; // This is really a bad habit I find
using std::cout; using std::cin;
using std::endl;
int main()
{
staticconstint numSize = 5; // Making the array a fixed size and using a variable to refer to its size
int num[numSize];
// int i, y, z, x; -- You need to get rid of this, if you're going to use the same value
// throughout your for-loops, then declare before the loops, however use a meaningful name
// Also when you declare the variable within the for loop, it's only going to be local in that
// loop scope, so you don't have to worry about for (int i.... mixing up with another
// int i outside the for loop
for (int i = 0; i < numSize; ++i) /* // int i will only be from here ---\/ */
{
cout << "Enter a number: ";
cin >> num[i];
} /* // to here ------------------------/\ */
for (int i = 0; i < numSize; ++i) // I like using the prefix ++ since its safer sometimes
{
for (int z = 0; z < numSize; ++z)
{
if (num[i] < num[z])
{
int y = num[i];
//cout<< "y = " <<y<<endl;
num[i] = num[z];
//cout<< "num["<<i<<"] = "<<num[i]<<endl;
num[z] = y;
//cout<< "num["<<z<<"] = "<<num[z]<<endl;
cout << endl;
for (int x=0; x < numSize; ++x)
{
cout<< num[x] << " ";
}
cout << endl;
}
/*
else
{
for (int x=0; x < numSize; x++)
{
cout<< num[x]<<" ";
}
cout<< endl;
}
*/
}
}
cout << "\n\n\nAscending order: ";
for (int i = 0; i < numSize; ++i)
{
cout << num[i];
cout << " ";
}
cout << endl;
system("pause"); // This stops the program from closing on you when you're debugging it
return 0;
}
bubble sort! ( = the least complex sorting algorithm ).
I disagree. The least complex is selection sort (minimum, swap, next)
And std::sort is too general, if you know something about the data (or the initial state of the array) you could do better
@ne555: I'm using Visual Studio 2010, I'm guessing you might be recommending me to use a breakpoint but I want the pause to be included in my console executable. I'm using another way to pause at the moment but if you have a more efficient way, please show me. Here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream>
int main()
{
std::cout << "Press Enter to Exit ... " << std::endl;
//in case a badbit or failbit has been set
std::cin.clear();
//i prefer this to ignore() because ignore sometimes means hitting enter twice
std::cin.sync();
std::cin.get();
return 0;
}
The least complex is selection sort (minimum, swap, next)
Let's say it's a tie..
And std::sort is too general, if you know something about the data (or the initial state of the array) you could do better
You could, but usually you don't really need to. std::sort is fine unless you need super optimized code.
@eNergizer, what ne555 linked to is best, but cin.ignore().get(); (or cin.get(); cin.get() or whatever (twice, becaus if you used cin >> a newline char is in the stream) ) would work fine. You don't need to worry about the error flags. If your code is well written, you'll have handled those before the program ends, right?