Thread: Help using arrays and loops for math

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

    Help using arrays and loops for math

    I am trying to construct a C++ script that will utilize an output based upon math equations. Could anyone provide a rough example of code in which I would construct a math equation with a looped array.

    For instance I would like to call 2 arrays with two different values, then I would like the program to take each value from the first array and divide by each value in the second array to see if it equals a variable, I call RATIO.

    Example:
    firstArray1 / secondArray1
    firstArray1/secondArray2
    firstArray1/secondArray3

    firstArray2/secondArray1
    firstArray2/secondArray2
    firstArray2/secondArray3 etc. etc.

    I think I will be using an IF statement -
    in that IF RATIO = firstArray/secondArray etc. then output this .... (not sure if that changes anything)

    As always, thanks for helping out someone who is clueless!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    for ( first = 0 ; first < nFirst ; first++ ) {
      for ( second = 0 ; second < nSecond ; second++ ) {
        // do stuff with firstArray[first] and secondArray[second]
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    16
    Thank you!

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by LLINE View Post
    I am trying to construct a C++ script
    Sorry to be pedantic, but C++ is not a scripting language. You *write* C++ *programs*. C++ is compiled, scripts are interpreted, there's a big difference.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. while loops with arrays
    By volk in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 07:22 PM
  3. Arrays and loops
    By Zewu in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 11:05 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. loops and arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-03-2001, 03:12 PM