I have been learning C++ over the last couple of days. I know a few other programming languages well, and it hasn't been horribly difficult. I would appreciate any comments on my code so that I may improve:
- Readability
- CPP "style"
- Efficiency
- Vector vs array--which is better in this case?
Thanks in advance for any help you can give me.
//Bubblesort algorithm
#include <iostream>
#include <cstdlib>
#include <ctime>
#define pass ;
int main()
{
// int unsorted[] = {1,2,3,4,5,6,7,8,9,10};
int length;
int max;
srand(time(NULL));
std::cout << "Please input how many numbers you want to sort and the max value on those numbers: " << std::endl;
std::cin >> length;
std::cin >> max;
int unsorted[length];
int count = -1;
while(count++<length)
{
unsorted[count] = rand()%max;
}
//int length = sizeof( unsorted ) / sizeof( int );
int i = 0;
int num_changes_per_loop = 1; /* Should be set to zero at the beginning of each loop, but is nonzero here to prevent the loop from being cancelled on the first iteration */
int general_loop_count = 0;
while(general_loop_count++<length)
{
if(num_changes_per_loop==0) { break; }
for(i=0;i<length-1;i++)
{
int current_value = unsorted[i];
int adjacent_value = unsorted[i+1];
if(adjacent_value > current_value) {pass}
else if(current_value > adjacent_value) {
std::cout << "These two values need to be switched!" << std::endl;
int tmp = adjacent_value;
unsorted[i+1] = current_value;
unsorted[i] = tmp;
num_changes_per_loop++;
}
}
}
std::cout << "Finished!" << std::endl;
for(i=0;i<length;i++)
{
std::cout << unsorted[i] << ", ";
}
}