Thread: question about arrays...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    25

    question about arrays...

    is there a way to store numbers in arrays, then jst multiply them all somehow instead of typing
    cout << array[1]*array[2]*array[3] and so on...
    or are arrays not the right thing to do this if so can some tell me the right way of doing this
    the purpose of this is im making a math program the multiplies numbers that you type in
    thx a lot for any help...

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Assumption: The array is in scope and not dynamically allocated:
    Code:
    void somefunc() {
      int nums[15];
      int effsize = 0;
      // Do some stuff here that inputs values into nums and every time you do you increment effsize
      int total=nums[0];
      for (int i=1; i < effsize; i++)
        total *= nums[i];
      cout<<total;
    }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <numeric>     // For 'accumulate'
    #include <functional>  // For 'multiplies'
    #include <iostream> 
    using namespace std;
    
    int main()
    {
        int array[5] = { 1, 2, 3, 4, 5 };
        cout << "The product of all array values is: "
             << accumulate( array, array+5, 1, multiplies<int>() )
             << endl;
        return 0;
    }
    Should output:
    Code:
    The product of all array values is: 120
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM