for loop and do while for vector addition. Please help

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

for loop and do while for vector addition. Please help

Postby Ares » Wed Mar 14, 2012 8:35 am

I have to make two programs for vector addition, subtraction, multiplication and length using
1) for loop
2) do while
Basically it is my assignment. I just need an idea, I don't want to tell me all.
So if you can help me just with addition of vectors, others for multiplication, subtraction and length i can make myself.
Program requirement is that program user will insert the vector components, I can do that, for example if [1,1] and [2,3] are two vectors, then please tell me two way to add them.
1)using for loop
Code: Select all
int main()
{
int x1, y1, x2, y2;
          cout << "\nPlease enter the first componet of 1 vector:";
      cin  >> x1;

      cout << "Please enter the second component of 1 vector:";
      cin >> y1;
               
                cout << "\nPlease enter the first component of 2  vetor:";
      cin  >> x2;

      cout << "Please enter the second component of 2 vector:";
      cin >> y2;
           
         for (initialization; condition; increment)
cout << "Addition of two vector is " << ((x1+x2) +(y1+y2)) << endl;
return 0;
}


2) using do while
Code: Select all
int main()
{
int x1, y1, x2, y2;
           cout << "\nPlease enter the first componet of 1 vector:";
      cin  >> x1;

      cout << "Please enter the second component of 1 vector:";
      cin >> y1;
               
                cout << "\nPlease enter the first component of 2  vetor:";
      cin  >> x2;

      cout << "Please enter the second component of 2 vector:";
      cin >> y2;

         do
           {
               cout << "Addition of two vector is " << ((x1+x2) +(y1+y2)) << endl;
            }
          while (condition);
return 0;
}

I'll be very thankful to you. :newbie:
Ares
 
Posts: 1
Joined: Wed Mar 14, 2012 7:59 am

Re: for loop and do while for vector addition. Please help

Postby exomo » Fri Mar 16, 2012 1:54 am

First of all I think you should use something like std::vector<double> to represent your vector. Or even better make your own class that has overloaded operators for +-*/.
You can not use a loop in a useful way on your x1,y1,.. variables.
For your addition:
Code: Select all
int x3 = x1+x2;
int y3 = y1+y2;
cout << "Addition of two vector is (" << x3 << "," << y3 << ")" << endl;
This is how you add vectors, no way to use a loop here. Pretty much the same for subtraction. Vector multiplication is either the cross product of three dimensional vectors or the dot product. And there is no way to divide a vector by another vector. Of course you can do elment wise multiplication and division of vectors just like the addition, but that would not be very useful.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 2 guests