1

I am relatively new to C++, coming from the happy-go-lucky world of Python.

I decided to write a set of functions to find the mean, median, mode, and range of a set of numbers. To aid in the calculation of the median, I decided to make l2g (least to greatest) and g2l (greatest to least). I understand I will only be using one of these, but want to make both just for the sake of completeness.

I want my program to rely on as few external (even standard) libraries as possible. The reason for this is that I will most likely be using any functions that I write again. They will be used in my OS that I am developing based off of the tutorials at BrokenThorn. Therefore I want to keep the code as independent as possible, using functions that I write instead of functions in the standard libraries.

I have two problems:

  • The first is that I want my functions to be able to handle float values. With the user input function that I created earlier, I get a pointer to a char array. I need to know how to convert that into a float value. I was thinking that maybe I could create a union with two members, of char and float types, and just reference the float member when I need to use the value. I would just put user data into the char member. However, I don't know if this approach would work. The tutorial that I use doesn't go very in-depth about defining my own data types.
  • My second dilemma is that I do not know how to create the l2g and g2l functions. I was planning on nesting two for loops. I was going to use the inside one to find the next number in the new array, and the outside one to repeat this process until it gets through all the numbers in the original array. Then the function would return a pointer to the new array of floats, after deleting the first array's dynamic memory.

Another issue is that I think I need to return the length of an array when I return a pointer to it. The reason for this is that to use a for loop to go through the contents of that array, I need to have the highest position usable in the array, so I don't get garbage. I have a way to avoid this problem by making a struct that has two members, length and the array. It looks like this:

struct arrayLength {
    int l;
    float * p;
} ;

I was hoping that the following set of calls:

float * myArrayPointer; // I will change this to user input once I get the rest of the program working
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?
cout << mean(myArrayPointer, 5); // Give function mean the pointer to array and number of elements
cout << "\n";
cout << median(myArrayPointer, 5);
cout << "\n";
cout << range(myArrayPointer, 5);
cout << "\n";
cout << mode(myArrayPointer, 5);
cout << "\n\n\n";
arrayLength myArrayPointerLG = l2g(myArrayPointer, 5);
float * myArrayLGitem;
for(int i=0;i<myArrayPointerLG.l;i++) {
    myArrayLGitem = myArrayPointerLG.p[i];
    cout << myArrayPointerLGitem << "\n";
}

Would give the following output:

115.21
21.06
551.42
21.06

-41.73
21.06
21.06
65.97
509.69

So far, I haven't gotten my code compiled. :/ I think maybe a for loop has its own scope... Could someone verify this as well? Here's a link to the code on pastebin:

Pastebin

And in case you were wondering about the <iostream> inclusion... I will remove this and use the printing functions I wrote if I end up adding it to my OS. OR I might just define std::cout. The errors that I'm getting are saying that currentPosition and listOfNumbers are undeclared (not defined) in l2g. I am running Windows 7 64-bit, using the MSVC++ 2008 Express Edition compiler. Thank you in advance to anyone who helps!

5
  • Your code is not compiling because it has massive typographical errors. - BTW, you want to avoid any library functions, and you are using new (nothrow)?
    – UncleBens
    Aug 18, 2010 at 21:44
  • Please learn how to use paragraphs. Your post is almost unreadable. Also, use standard libraries; they're fully portable. Also, floats can be negative. The only numeric types that can't be are unsigned integral types. Aug 18, 2010 at 21:45
  • Can you summarize some specific (shorter) questions? It's difficult to parse your questions from this long post
    – Jay
    Aug 18, 2010 at 21:46
  • I'm sorry about the unreadability of the post (this is my first question), but it looks like someone has edited it to fix that. UncleBens, could you tell me where the errors are? I couldn't find any. Thanks! Oh, and I will remove the use of nothrow once I learn how to handle exceptions.
    – masonsbro
    Aug 19, 2010 at 1:46
  • you can't handle exceptions without STL, because new itself uses it. To handle exception from new you need to catch std::bad_alloc. Otherwise you have to use malloc and free from C library.. if you don't use any libraries at all, you can't operate heap.. Literally, if you do not want to use ANY libraries, you're limited to C with classes, and to implementing every little thing down to assembler-like level, the language itself doesn't define those things. I'd like to think it's misconception could be caused by python, but actually, most of python's tools are part of "standard" library Dec 3, 2016 at 2:12

2 Answers 2

6

a) learn to use STL containers, algrithms etc they will make life much easier for you

b) c func atof will do what the question title asks

1

If you lift your restriction on using libraries, both tasks are very simple.

Converting char* to float is easy with Boost.lexical_cast (See http://www.boost.org/doc/libs/1_43_0/libs/conversion/lexical_cast.htm). It provides type-safe conversions between any two types, so long as they define the "stream" operators (<< and >>).

#include "boost/lexical_cast.hpp"
...
const char* string_value = "1.5";
float float_value = boost::lexical_cast<float>(string_value);

The standard library's sort method is the simplest way to order your array. Using your example above:

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<float>()); //Sort highest to lowest

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 http://en.wikipedia.org/wiki/Sorting_algorithm for lots of options. Quicksort and Merge-sort are fairly commonly seen ones that give pretty good performance.

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.

1
  • Thanks! This answer helped a lot. I saw somewhere else on the web a way to convert a character to its respective integer. It said to just subtract the character '0' from the character to convert. Example: c-'0' would return however much higher the ASCII value of c is than the ASCII value of zero. Again, thank you very much for the answer. It solved my problems.
    – masonsbro
    Aug 19, 2010 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.