Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you lift your restriction on using libraries, both tasks are very simple.</p> <p>Converting char* to float is easy with Boost.lexical_cast (See <a href="http://www.boost.org/doc/libs/1_43_0/libs/conversion/lexical_cast.htm" rel="nofollow noreferrer">http://www.boost.org/doc/libs/1_43_0/libs/conversion/lexical_cast.htm</a>). It provides type-safe conversions between any two types, so long as they define the "stream" operators (&lt;&lt; and >>).</p> <pre><code>#include "boost/lexical_cast.hpp" ... const char* string_value = "1.5"; float float_value = boost::lexical_cast&lt;float&gt;(string_value); </code></pre> <p>The standard library's sort method is the simplest way to order your array. Using your example above:</p> <pre><code>float * myArrayPointer; myArrayPointer = new float[5]; myArrayPointer[0] = 65.97; myArrayPointer[1] = 21.06; myArrayPointer[2] = 21.06; myArrayPointer[3] = 509.69; myArrayPointer[4] = -41.73; // Can floats be negative? std::sort(myArrayPointer, myArrayPointer+5); //Sort lowest to highest std::sort(myArrayPointer, myArrayPointer+5, std::greater&lt;float&gt;()); //Sort highest to lowest </code></pre> <p>If you insist on doing it without any library help, you'll have to implement a sorting algorithm for yourself. There are lots to pick from, each giving you different performance characteristics. Pick the one that best matches your use-case. See <a href="http://en.wikipedia.org/wiki/Sorting_algorithm" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Sorting_algorithm</a> for lots of options. Quicksort and Merge-sort are fairly commonly seen ones that give pretty good performance.</p> <p>As for implementing char* to float conversions manually, you will essentially be implementing C's atof yourself. To do this, you'll need to loop over each character in the string, convert it to its corresponding integral value, and accumulate them into a float. You'd then use the location of the decimal point to determine how to scale all of the values. For example, to convert the string "12.5" to a float, you'd have to convert each character ('1', '2', and '5') to their respective integers, and scale them to get 1*10^1 + 2*10^0 + 5*10^-1. There may be more efficient ways to do it, but that's about as simple as I can make it.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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