Thread: comparing two arrays of floats

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    comparing two arrays of floats

    I have two array of floats, each could have an undetermined amount of information. strcmp() didn't work and I ma guessing because it is not a string. I got memcmp() to work but I had to hard code the length I wanted to compare. How can I determine the length of each of the array of floats? Is there a better way of doing this then using memcmp()?

    Thanks,

  2. #2
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>How can I determine the length of each of the array of floats?
    Code:
    #define LEN(x) sizeof (x) / sizeof (x[0])
    ...
    memcmp(a, b, LEN(a));

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    I apoligize I am new and my terminology is a little vague.

    Let's say the length is 10 I need to know many floats are actually in this array? (i.e. Maybe 5 or 7 or even 10)

    Thanks again,

  4. #4
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Could you try initializing all the elements in the array to a value you know that they'll never have? (I like to use the constants DBL_MIN and DBL_MAX if it is possible they can contain 0 or -1 or any other constant commonly used for errors.) Then you can see how many elements are not equal to the default value. A little time consuming, but it works....

    Alternatively you can have a separate variable that stores the number of floats in the array. But everytime you add or delete elements you'll have to change the variable.

    Is this what you're looking for?
    My programs don't have bugs, they just develop random features.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    if you use a storage class like vector you can just call the size() method. casey's idea works for fixed length arrays. For an unknown length dynamic array though, you're pretty much out of luck I think. Since this is the C++ forum though I'm going to suggest vector.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wait, you've allocated it to 10? well whatever you left in there could be seen as a float. If you initialize to zero for instance, there is a float in there. you need to keep a counter of some kind or use vector and let it do it for you.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Unless I find something else I will probably try initializing the elements to a number I know they won't have.

    Thanks for your help.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What's wrong with vector?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. efficiency comparing ints and floats
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-28-2009, 03:56 PM
  2. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  3. Comparing 2 elements from 2 different arrays
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 11-15-2006, 02:43 PM
  4. Comparing Arrays
    By Raison in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2004, 02:21 PM
  5. comparing arrays
    By dustyrain84 in forum C Programming
    Replies: 1
    Last Post: 01-21-2004, 05:52 PM