> For instance we stated that temp is float in the beginning, then why are we stating it as float again.
those are two different variables, they have the same name, but their scope is different.
1 2 3 4 5 6
|
float temp; //to distinguish, let's call this `temp1'
for(...)
for(...)
if(...){
float temp; //and this one `temp2'
}
| |
temp2 only exists inside the `if', temp1 may be seen in all main.
now, given the purpose of `temp' there is no advantage for it to live outside the conditional. You should limit the scope of your variables so i suggest you to remove the declaration at line 10.
the same may be said about `stemp', you should declare it inside the if.
> Also, we are intializing (i) and not using it.
it is used as the stop limit of the second loop
1 2
|
for (unsigned int i = 0; i < n - 1; ++i)
for (unsigned int j = 0; j < n - i - 1 /*here*/; ++j)
| |
first you'll traverse the whole array (i=0)
then you leave out the last element (i=1)
you will be leaving out the elements to the right, that the algorithm already sorted
at the end you only check two elements (i=n-2, so j is 0 and you check 0 and 1)