Ask Question

Hi guys

I have a vector with 9 elements

vec[20] = 80.59, 132.40, 81.85, 79.83, 119.96, 111.89, 110.99, 101.21, 118.53

Im trying to put them in an anscending order

thats what the console shows me:

80.59 79.83 81.85 101.21 110.99 111.89 118.53 119.96 132.40

for (k=1; k<=i-1; k++)

{

for (j=k+1; j {

if (vec[k]>vec[j])

{

aux=vec[k];

vec[k]=vec[j];

vec[j]=aux;

}

}

}

Up is the formule i used but it is not working.

What Should I do? I am working in C Programming.

+4
Answers (1)
  1. 5 March, 01:16
    0
    Vectors and arrays are 0-based, so they don't start at 1 but at 0

    your sort works by switching positions of 2 elements, which in your case didn't happen for the pair of the first&second element

    your outer-loop starts with k=1, which should be k=0 instead
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Hi guys I have a vector with 9 elements vec[20] = 80.59, 132.40, 81.85, 79.83, 119.96, 111.89, 110.99, 101.21, 118.53 Im trying to put them ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers