I am writing a program that prompts the user to enter values that will be stored in an array. When user chooses not to add new values (by typing N), the program will output the array in ascending order. The input values should be stored as floats, and the program should be able to process up to 100 values, and as few as 0 values. As the number of values entered by user is unknown, how can I use a bubble sort algorithm to sort the array? I'm confused as to how to declare the array when the length is unknown, and how to structure the function. Any assistance will be greatly appreciated!
Will it be correct to declare a global variable (constant) as the max number?
const int MAX = 100;
and then declare the array below the int_main function:
float myarray[MAX];
function I've been using:
sort(myarray, myarray + MAX);
for (int i = 0; i <= MAX; ++i)
cout << myarray[i] << ' ';
Assuming you can't use a vector, the easiest way would probably be to just count the number of valid input values. Then the sort would be sort(myarray, myarray + (count - 1));