How to choose one element from each array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nurulshidanoni
    New Member
    • Sep 2007
    • 40

    How to choose one element from each array

    How to choose one element from each array., Which A(i)+B(i)+C(i)+ D(i)+E(i)+F(i) must equal to 10.

    _______________ _______________ _______________ _____
    i A B C D E F
    _______________ _______________ _______________ _____
    1 3 2 3 3 2 4
    _______________ _______________ _______________ _____

    2 6 9 4 24 5 7
    _______________ _______________ _______________ _____

    3 11 28 5 81 10 12
    _______________ _______________ _______________ _____

    4 18 65 6 192 17 19
    _______________ _______________ _______________ _____

    5 27 126 7 375 26 28
    _______________ _______________ _______________ _____

    6 38 217 8 648 37 39
    _______________ __________
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by nurulshidanoni
    How to choose one element from each array., Which A(i)+B(i)+C(i)+ D(i)+E(i)+F(i) must equal to 10.

    _______________ _______________ _______________ _____
    i A B C D E F
    _______________ _______________ _______________ _____
    1 3 2 3 3 2 4
    _______________ _______________ _______________ _____

    2 6 9 4 24 5 7
    _______________ _______________ _______________ _____

    3 11 28 5 81 10 12
    _______________ _______________ _______________ _____

    4 18 65 6 192 17 19
    _______________ _______________ _______________ _____

    5 27 126 7 375 26 28
    _______________ _______________ _______________ _____

    6 38 217 8 648 37 39
    _______________ __________
    Please POST what you have tried to achieve this?

    Comment

    • nurulshidanoni
      New Member
      • Sep 2007
      • 40

      #3
      Code:
      //total up the array
      //total + = total + a[i]
      total += a[i]
      but, tehre is b,c,d,e,f ..i diont know ho w to do

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by nurulshidanoni
        Code:
        //total up the array
        //total + = total + a[i]
        total += a[i]
        but, tehre is b,c,d,e,f ..i diont know ho w to do
        You want a(i) + b(i) + c(i) + d(i) + e(i) = 10 right?

        You try something like this:

        [code=c]
        int total = 0;
        for(int i = 0;i<10;i++)
        {
        total = a[i] + b[i] + c[i] + d[i] + e[i];
        if(total==10)
        {
        printf("total is 10");
        }
        total=0;
        }
        [/code]

        Comment

        • nurulshidanoni
          New Member
          • Sep 2007
          • 40

          #5
          Why must total=0;?

          and why == ?

          Code:
          printf("total is 10");
          }
          total=0;
          }

          Comment

          • amitpatel66
            Recognized Expert Top Contributor
            • Mar 2007
            • 2358

            #6
            Originally posted by nurulshidanoni
            Why must total=0;?

            and why == ?

            Code:
            printf("total is 10");
            }
            total=0;
            }
            == is a relational operator used in IF for comparison. You can learn about fundamentals of C here

            And regarding total = 0, I have reinitialized the total variable so that next time it will store the sum for second value ie for i = 1 and so on.
            Its always good to reinitialize/clear a variable value before using them again

            Comment

            Working...