Thread: Code Flow (arrays and loops)

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    16

    Code Flow (arrays and loops)

    Hi: Thank you for taking time to help me with my problem. I am having problems understanding where this code is wrong as I believe that I am not receiving the correct outputs. I have truncated the code to the most pertinent.

    The algorithm asks that every value in array1 be divided by array2
    and if that value is less than the ratio * array3 (percentage filter) then output.

    I have no problem with the code with the exception of
    abs(ratio*array3[m]

    Assuming that the ratio = 1 then all values get outputted, I want the array1 (first value) to have the first value in array3 assigned to it, the second value in array1 to have the second value in array3 to be assigned.

    So:
    array1 (1.0) with array3 (0.05)
    array1 (2.0) with array3 (0.06)
    etc.



    Code:
    array1[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
    array2[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
    array3[5] = {0.05, 0.06, 0.07, 0.08, 0.09};
    
    ratio = abc/def;
    
     for(int m=0; m<5; m++)
                  {
                   for(int n=0; n<5; n++)
                     {
                      if(abs(ratio-array1[m]/array2[n])<abs(ratio*array3[m]))
    {
    >>then print out values paired matches of array1[] and array2[]
    }
    Thank you again for pointing out any problem you see.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    1. Your arrays and variables (ratio, abc, def) must be floating-point type (float or double) to return requested value.
    2. To use abs() function remember to include appropriate header file.
    Last edited by DRK; 02-15-2013 at 01:33 AM.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    your code is not code
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    16
    Thank you - I should have stated that it is pseudo code - I have the problem solved. Thanks for your help.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    ok, i did think that - but it is best to indicate, we never know when someone is really far off the mark with their code.

    Another thing i should say though is that the indentation style is really rather strange, and not very nice - i dont know if that is just the browser formatting but if not, sort it out.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about Arrays and program flow
    By David.Mary in forum C Programming
    Replies: 12
    Last Post: 06-27-2011, 05:23 PM
  2. explain Flow of C code
    By emb_begin in forum C Programming
    Replies: 2
    Last Post: 02-18-2011, 07:50 AM
  3. Code flow analyzer
    By CodeMonkey62 in forum C++ Programming
    Replies: 7
    Last Post: 05-26-2010, 02:54 AM
  4. Buffer Over Flow with string Arrays
    By vlrk in forum C Programming
    Replies: 1
    Last Post: 06-24-2008, 07:16 AM