A better solution to my problem

This is a discussion on A better solution to my problem within the C Programming forums, part of the General Programming Boards category; Hi All, First post here Would like some help for a short problem. I have two arrays of the same ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    A better solution to my problem

    Hi All,

    First post here Would like some help for a short problem.

    I have two arrays of the same size. I need to find if the difference of n consecutive elements of the two arrays are bigger than a certain threshold.

    I have a rudimentary solution to this but was wondering if someone has a better and smarter solution. In the case of n=3, I am doing:

    Code:
    for (int i = 0; i <= array_size; i++ ){
        if ((a[i]-b[i] >= thresh ) && (a[i+1]-b[i+1] >= thresh) && (a[i+2]-b[i+2] >= thresh))
           // do something
    }
    (Please ignore the part where I reach the end of the array. I have presented my problem to focus on the part above only)

    As you can see, the conditional statement for the if is very poor design. If n = 10, I would have to write a very long expression.

    Does someone have a better solution than mine?

    Would appreciate your help.
    Last edited by renegade007; 05-16-2013 at 01:50 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,282
    I would use the "for()" loop simply to check for the desired condition, with a flag to indicate this condition. Then after the loop, have code to perform certain actions based on the status of this flag.

    Code:
    for i = 0 until i < array_size
        if array1[i] - array2[i] >= threshold
            increment counter
        else
            clear counter
    
        if counter >= n
            set flag
    end for
    
    
    if flag == 1
        // do something
    Note that on line 2 in my example, which was borrowed from your example, you should compare with the absolute value of the difference, in case array 2 happen to contain a larger value than array 1.

    Also note that if "array_size" is the size of your array, you're overruning the bounds of that array:

    Code:
    for (int i = 0; i <= array_size; i++ )
    Array indices start at zero and go to N-1. The standard way of representing this would be:

    Code:
    for (int i = 0; i < array_size; i++ )

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    I'd be inclined to use logical statements that could break away from the "for" loop as soon as a difference was detected.

    Code:
        for (i = 0, flag = 1; (i < arr_size) && (flag != 0); i++)
        {
            flag &= (arr1[i] == arr2[i]);
        }
    arr1[i] == arr2[i] will equate to 1 if true, or 0 if false
    flag &= result -> flag will be 1 if result is 1, 0 if false
    If you look at the condition in the "for" loop (flag != 0), this will exit the for loop at first sign of a difference. You could put (arr1[i]==arr2[i]) in the condition for the for loop, but then you can't use the flag variable later as an indication of whether the two arrays were the same.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Eight Queen Problem Solution!
    By chottachatri in forum C# Programming
    Replies: 2
    Last Post: 06-29-2009, 08:37 PM
  2. solution to the problem
    By vapanchamukhi in forum C Programming
    Replies: 14
    Last Post: 09-08-2008, 10:41 PM
  3. Problem and Solution
    By DavidP in forum Tech Board
    Replies: 3
    Last Post: 08-18-2003, 02:23 AM
  4. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM
  5. Odd solution to an odd problem
    By Zeeshan in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-20-2002, 05:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21