Note that there are some explanatory texts on larger screens.

plurals
  1. POFind the biggest 3 numbers in a vector
    primarykey
    data
    text
    <p>I'm trying to make a function to get the 3 biggest numbers in a vector. For example: Numbers: 1 6 2 5 3 7 4 Result: 5 6 7</p> <p>I figured I could sort them DESC, get the 3 numbers at the beggining, and after that resort them ASC, but that would be a waste of memory allocation and execution time. I know there is a simpler solution, but I can't figure it out. And another problem is, what if I have only two numbers...</p> <p>BTW: I use as compiler BorlandC++ 3.1 (I know, very old, but that's what I'll use at the exam..)</p> <p>Thanks guys.</p> <p>LE: If anyone wants to know more about what I'm trying to accomplish, you can check the code:</p> <pre><code>#include&lt;fstream.h&gt; #include&lt;conio.h&gt; int v[1000], n; ifstream f("bac.in"); void citire(); void afisare_a(); int ultima_cifra(int nr); void sortare(int asc); void main() { clrscr(); citire(); sortare(2); afisare_a(); getch(); } void citire() { f&gt;&gt;n; for(int i = 0; i &lt; n; i++) f&gt;&gt;v[i]; f.close(); } void afisare_a() { for(int i = 0;i &lt; n; i++) if(ultima_cifra(v[i]) == 5) cout&lt;&lt;v[i]&lt;&lt;" "; } int ultima_cifra(int nr) { return nr - 10 * ( nr / 10 ); } void sortare(int asc) { int aux, s; if(asc == 1) do { s = 0; for(int i = 0; i &lt; n-1; i++) if(v[i] &gt; v[i+1]) { aux = v[i]; v[i] = v[i+1]; v[i+1] = aux; s = 1; } } while( s == 1); else do { s = 0; for(int i = 0; i &lt; n-1; i++) if(v[i] &lt; v[i+1]) { aux = v[i]; v[i] = v[i+1]; v[i+1] = v[i]; s = 1; } } while(s == 1); } </code></pre> <p>Citire = Read Afisare = Display Ultima Cifra = Last digit of number Sortare = Bubble Sort</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload