Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,673 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,177 people online right now. Registration is fast and FREE... Join Now!




inner_product() in VECTORS

 
Reply to this topicStart new topic

inner_product() in VECTORS, product will not give correct result

nirvanarupali
post 4 Sep, 2007 - 08:48 PM
Post #1


D.I.C Foot

Group Icon
Joined: 1 Aug, 2007
Posts: 983



Thanked 2 times

Dream Kudos: 375
My Contributions


My goal is to produce the sum and product to all elements but

I have a problem how to get product to all elements.

CODE
inner_product()
will not work.

CODE

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

int main()
{
  
   vector <double> number1, number2(1);
   double x;
   char answer;

   cout << "Do you want enter number (y/n)?";
   cin >> answer;

  
   while (answer == 'y' || answer == 'Y')
   {
      
      cout << "Enter number:";
      cin >> x;

      number1.push_back(x);
  
    cout << "Do you want enter more numbers (y/n)?"<< endl;
    cin >> answer;
   }

    for (int i=0; i<number1.size(); i++)
     {
        cout <<"The numbers are:" << " " << number1[i] <<"\n";
     }

   cout << "\n\n";


   cout << "Total is:"
        << accumulate(number1.begin(), number1.end(),  0.0000) <<"\n\n";
        
   cout << "Product is:"
        << inner_product(number1.begin(), number1.end(), number2.begin(), 0.0000) <<"\n\n";
  
  

  return 0;

}

User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 4 Sep, 2007 - 09:18 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well that is because you don't load any numbers into your second vector (number2). So your inner_product() is attempting to multiple your first number against null in number2 and then add it to your second number times null. All in all, it ends in null! (I am a poet and didn't even know it!)

Just to show you what I mean I modified your program in the following way...

CODE

vector <double> number1, number2;
...
...
number1.push_back(x);
number2.push_back(x); // Add the same value to second vector


So lets say I enter 1 as a first number and 3 as the second number. 1 * 1 = 1 added to 3 * 3 = 9 so your total will be "10".

Enjoy!

decap.gif
User is offlineProfile CardPM

Go to the top of the page

nirvanarupali
post 4 Sep, 2007 - 10:32 PM
Post #3


D.I.C Foot

Group Icon
Joined: 1 Aug, 2007
Posts: 983



Thanked 2 times

Dream Kudos: 375
My Contributions


I got your point but my goal is just to get the product on all elements; let's say I entered 3,6,5 then obviously the product is
CODE
3*6*5=90
. I put
CODE
number2(1)
because when I eliminate the 3rd parameter, I got error so I put it with value 1 so much so that the product would still be 90. Can you give me an example just to get the product with the use of
CODE
inner_product()
in vectors ?
User is offlineProfile CardPM

Go to the top of the page

Bench
post 5 Sep, 2007 - 03:06 AM
Post #4


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


Why do you need to use inner_product? Can't you just use accumulate?

CODE
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>

int main()
{
    int arr[] = { 1,2,3,4,5 };
    std::vector<int> vec1( arr, arr+sizeof(arr)/sizeof(*arr) );
    std::cout << std::accumulate( vec1.begin(), vec1.end(),
                                  1,
                                  std::multiplies<int>() )
              << std::endl;
}


This post has been edited by Bench: 5 Sep, 2007 - 03:07 AM
User is offlineProfile CardPM

Go to the top of the page

nirvanarupali
post 5 Sep, 2007 - 04:39 PM
Post #5


D.I.C Foot

Group Icon
Joined: 1 Aug, 2007
Posts: 983



Thanked 2 times

Dream Kudos: 375
My Contributions


I got it.....Thank u Bench
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month