So I was working through some extra exercises in the book for my C++ class and was coding up one that had you sort an array using a bubble sort. I had it working and the next exercise had improvements to the algorithm. One of these was making the algorithm check if a pass needed to be done and now I'm getting an error in the while loop (I switched over from a for loop). The error says "left operand must be l-value" and appears to point to the variable "passes" which is declared as an int? Anyway, here's the code.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
void randomize_array(int[], int);
void print_array(constint[], int);
int main()
{
srand(time(0));
int passes = 1;
constint array_size = 100;
int array_to_sort[array_size] = {};
int temp_hold_value;
int decrement_counter = 0;
bool loop_needed = true;
randomize_array(array_to_sort, array_size);
print_array(array_to_sort, array_size);
while (passes <= (array_size - 1) && loop_needed = true)
{
decrement_counter++;
loop_needed = false;
passes++;
for (int i = 0; i < array_size - decrement_counter; i++)
if (array_to_sort[i] > array_to_sort[i+1])
{
temp_hold_value = array_to_sort[i+1];
array_to_sort[i+1] = array_to_sort[i];
array_to_sort[i] = temp_hold_value;
loop_needed = true;
}
}
cout << "\n\nArray after sort:" << endl;
print_array(array_to_sort, array_size);
system("pause");
}
The issue is with the line while (passes <= (array_size - 1) && loop_needed = true)
I appreciate any help. I understand the basics (probably very basics) of l-values, the left side needs to be a modifiable value. Not sure why passes isn't here.
Thanks! I was so focused on where the error code was pointing (passes) I didn't read the rest of the line any of the million times I looked over it. I get why it affects passes though.