Thread: Vectors

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Vectors

    Vectors

    Vectors are very important in mathematical computing and in computer graphics .Vector graphics is economical in its use of memory, as an entire line segment is specified simply by the coordinates of its endpoints. A vector can be represented by an arrow whose length represents the magnitude and the direction represents the vector direction.



    The above vector a can be represented as

    a = [a1, a2]

    The arithmetic operations can be performed in the following way:

    o Addition

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then the sum of a and b is

    a + b = [a1+b1 , a2+b2]

    o Subtraction

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then the difference of a and b is

    a - b = [a1-b1 , a2-b2]

    o Multiplication

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then dot product of a and b is

    a . b = (a1 * b1 ) + ( a2 * b2)

    o Length of vector

    If a is a vector

    a = [a1, a2]

    then the length of vector a is

    length =

    Assignment

    Write a C++ program that performs the above mentioned operations on vectors with the help of above mentioned formulae. The program should have four user defined functions for the above operations.



    1) Addition

    2) Subtraction

    3) Multiplication

    4) Length

    The following menu of commands should be displayed at the start of program execution.

    Press 'a' to add two vectors

    Press 's' to subtract two vectors

    Press 'm' to multiply two vectors

    Press 'l to calculate the length of vector

    Press 'q' to quit

    Please enter your choice: a

    After the user selects a choice, prompt the user to enter the vector components on which the selected mathematical operation is to be performed, and then display the result. For example,

    if the user enters ‘a’ then your output should be:

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The sum is [ 8 , 10 ]

    if the user enters ‘s’ then your output should be :

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The difference is [ -4 , 4]

    if the user enters ‘m’ then your output should be :

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The multiplication is 33

    After the menu if the user selects ‘l’ then your output should be

    Enter first component of vector : 1

    Enter second component of vector : 2

    The vector is : [ 1 , 2 ]

    The length is 2.23607

    After the menu if the user selects ‘q’ then your output should be

    Press any key to continue …..

    Conditions that must be checked and fulfilled:

    1) If a user enters choice other then choices given in menu, then a message should be displayed “You have entered wrong choice:” and main menu should be displayed again.

    2) If a used enters a ,s or m then you have to take components of two vectors then do calculation on them

    3) If a user enters l then you have to take components of one vector only then do calculation on it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Moved to the C++ forum.

    Please read concerning our homework policy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    When is this homework assignment due?

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    This is actually really easy. If you do it.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Do you actually have a question, or should I just close the thread?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If it were up to me, I'd just close it since it seems pointless and waste's people's time.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    6
    Vectors

    Vectors are very important in mathematical computing and in computer graphics .Vector graphics is economical in its use of memory, as an entire line segment is specified simply by the coordinates of its endpoints. A vector can be represented by an arrow whose length represents the magnitude and the direction represents the vector direction.



    The above vector a can be represented as

    a = [a1, a2]

    The arithmetic operations can be performed in the following way:

    o Addition

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then the sum of a and b is

    a + b = [a1+b1 , a2+b2]

    o Subtraction

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then the difference of a and b is

    a - b = [a1-b1 , a2-b2]

    o Multiplication

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then dot product of a and b is

    a . b = (a1 * b1 ) + ( a2 * b2)

    o Length of vector

    If a is a vector

    a = [a1, a2]

    then the length of vector a is

    length =

    Assignment

    Write a C++ program that performs the above mentioned operations on vectors with the help of above mentioned formulae. The program should have four user defined functions for the above operations.



    1) Addition

    2) Subtraction

    3) Multiplication

    4) Length

    The following menu of commands should be displayed at the start of program execution.

    Press 'a' to add two vectors

    Press 's' to subtract two vectors

    Press 'm' to multiply two vectors

    Press 'l to calculate the length of vector

    Press 'q' to quit

    Please enter your choice: a

    After the user selects a choice, prompt the user to enter the vector components on which the selected mathematical operation is to be performed, and then display the result. For example,

    if the user enters ‘a’ then your output should be:

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The sum is [ 8 , 10 ]

    if the user enters ‘s’ then your output should be :

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The difference is [ -4 , 4]

    if the user enters ‘m’ then your output should be :

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The multiplication is 33

    After the menu if the user selects ‘l’ then your output should be

    Enter first component of vector : 1

    Enter second component of vector : 2

    The vector is : [ 1 , 2 ]

    The length is 2.23607

    After the menu if the user selects ‘q’ then your output should be

    Press any key to continue …..

    Conditions that must be checked and fulfilled:

    1) If a user enters choice other then choices given in menu, then a message should be displayed “You have entered wrong choice:” and main menu should be displayed again.

    2) If a used enters a ,s or m then you have to take components of two vectors then do calculation on them

    3) If a user enters l then you have to take components of one vector only then do calculation on it.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    6
    Code:
    #include<iostream.h>
    #include <math.h>
    #include<process.h>
    #include<conio.h>
    // prototype for the the functions
    void getVect(int &s, int &e );
    void addition();
    void subtraction();
    void multiplication();
    void length();
    int main() // function main begins program execution
    {
    // let the user know about the program
    clrscr();
    cout << "\n\n\t program that describes something about vectors.";
    char choice;
    do{
    cout << "\n\n \t\t\t 'M E N U' ";
    cout << "\n\n\t\tAddition : A";
    cout << "\n\t\tSubtraction : S";
    cout << "\n\t\tMultiplication : M";
    cout << "\n\t\tLength : L";
    cout << "\n\t\tQuit : Q";
    cout << "\n\n\t\tEnter your choice ";
    cin >> choice;
    //ff convert the choice to lower case
    if ( choice < 97)
    choice +=32;
    switch ( choice )
    {
    case 'a' : addition();
    break;
    case 's' : subtraction();
    break;
    case 'm' : multiplication();
    break;
    case 'l' : length();
    break;
    case 'q' : //ff pause the screen
    cout << "\n\n\t";
    exit(0);
    break;
    default : cout << "\n\n\tYou have entered wrong choice";
    } //ff end switch
    } while (choice !='q'); // end do-while
    return 0; //ff indicate program executed successfully
    } //ff end of function, main
    
    void getVect( int &f, int &s )
    {
    //fix" prompt and read the values into the vector
    cout << "\n\tEnter the values for the vector:";
    cout << "\n\tFirst Component : ";
    cin >> f;
    cout << "\tSecond Component : ";
    cin >>s;
    cout << "'\tThe vector is [ " << f << " , " << s << " ]";;
    } // end function getVect
    
    void addition()
    {
    //ff declare the variables
    int a1, a2, b1, b2 ;
    //ff get the values for the first vector
    getVect( a1, a2 );
    //ff get the values for the second vector
    getVect( b1, b2 );
    //ff do the addition
    cout<< "\n\n\tThe sum is [ " << ( a1 + b1 )<< "," << ( a2 + b2 ) << "]" ;
    } //ff end function addition
    
    void subtraction()
    {
    // declare the variables
    int al, a2, bl, b2 ;
    //ff get the values for the first vector
    getVect( al, a2 );
    //ff get the values for the second vector
    getVect( bl, b2 );
    //ff do the subtraction
    cout << "\n\n\t‘I‘he difference is [ " << ( al - bl )<<", "<<(a2-b2)<<"]";
    } //fs'] end function subtraction
    
    
    
    void multiplication()
    {
    //fix" declare the variables
    int al, a2, bl, b2 ;
    //fra" get the values for the first vector
    getVect(al, a2) ;
    //fra" get the values for the second vector
    getVect( bl, b2 );
    //ff do the multiplication
    cout << "\n\n\tThe product is [ "  << ( al * bl )<< ", " << (a2 * b2) <<"]";
    }// ff end function multiplication
    void length()
    {
    //ff declare the variables
    int a1, a2;
    float len;
    
    //ff get the values for the vector
    getVect(a1,a2);
    //fir'] find the length
    a1=a1*a1;
    a2=a2*a2;
    len=a1+a2;
    len=sqrt(len);
    cout << "\n\n\tlength is "<<len;
    
    }
    // end function length

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    6
    Code:
    #include<iostream.h>
    #include <math.h>
    #include<process.h>
    #include<conio.h>
    // prototype for the the functions
    void getVect(int &s, int &e );
    void addition();
    void subtraction();
    void multiplication();
    void length();
    int main() // function main begins program execution
    {
    // let the user know about the program
    clrscr();
    cout << "\n\n\t program that describes something about vectors.";
    char choice;
    do{
    cout << "\n\n \t\t\t 'M E N U' ";
    cout << "\n\n\t\tAddition : A";
    cout << "\n\t\tSubtraction : S";
    cout << "\n\t\tMultiplication : M";
    cout << "\n\t\tLength : L";
    cout << "\n\t\tQuit : Q";
    cout << "\n\n\t\tEnter your choice ";
    cin >> choice;
    //ff convert the choice to lower case
    if ( choice < 97)
    choice +=32;
    switch ( choice )
    {
    case 'a' : addition();
    break;
    case 's' : subtraction();
    break;
    case 'm' : multiplication();
    break;
    case 'l' : length();
    break;
    case 'q' : //ff pause the screen
    cout << "\n\n\t";
    exit(0);
    break;
    default : cout << "\n\n\tYou have entered wrong choice";
    } //ff end switch
    } while (choice !='q'); // end do-while
    return 0; //ff indicate program executed successfully
    } //ff end of function, main
    
    void getVect( int &f, int &s )
    {
    //fix" prompt and read the values into the vector
    cout << "\n\tEnter the values for the vector:";
    cout << "\n\tFirst Component : ";
    cin >> f;
    cout << "\tSecond Component : ";
    cin >>s;
    cout << "'\tThe vector is [ " << f << " , " << s << " ]";;
    } // end function getVect
    
    void addition()
    {
    //ff declare the variables
    int a1, a2, b1, b2 ;
    //ff get the values for the first vector
    getVect( a1, a2 );
    //ff get the values for the second vector
    getVect( b1, b2 );
    //ff do the addition
    cout<< "\n\n\tThe sum is [ " << ( a1 + b1 )<< "," << ( a2 + b2 ) << "]" ;
    } //ff end function addition
    
    void subtraction()
    {
    // declare the variables
    int al, a2, bl, b2 ;
    //ff get the values for the first vector
    getVect( al, a2 );
    //ff get the values for the second vector
    getVect( bl, b2 );
    //ff do the subtraction
    cout << "\n\n\t‘I‘he difference is [ " << ( al - bl )<<", "<<(a2-b2)<<"]";
    } //fs'] end function subtraction
    
    
    
    void multiplication()
    {
    //fix" declare the variables
    int al, a2, bl, b2 ;
    //fra" get the values for the first vector
    getVect(al, a2) ;
    //fra" get the values for the second vector
    getVect( bl, b2 );
    //ff do the multiplication
    cout << "\n\n\tThe product is [ "  << ( al * bl )<< ", " << (a2 * b2) <<"]";
    }// ff end function multiplication
    void length()
    {
    //ff declare the variables
    int a1, a2;
    float len;
    
    //ff get the values for the vector
    getVect(a1,a2);
    //fir'] find the length
    a1=a1*a1;
    a2=a2*a2;
    len=a1+a2;
    len=sqrt(len);
    cout << "\n\n\tlength is "<<len;
    
    }// end function length
    Quote Originally Posted by naseerhaider View Post
    Vectors

    Vectors are very important in mathematical computing and in computer graphics .Vector graphics is economical in its use of memory, as an entire line segment is specified simply by the coordinates of its endpoints. A vector can be represented by an arrow whose length represents the magnitude and the direction represents the vector direction.



    The above vector a can be represented as

    a = [a1, a2]

    The arithmetic operations can be performed in the following way:

    o Addition

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then the sum of a and b is

    a + b = [a1+b1 , a2+b2]

    o Subtraction

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then the difference of a and b is

    a - b = [a1-b1 , a2-b2]

    o Multiplication

    If a and b are two vectors

    a = [a1, a2]

    b = [b1, b2]

    then dot product of a and b is

    a . b = (a1 * b1 ) + ( a2 * b2)

    o Length of vector

    If a is a vector

    a = [a1, a2]

    then the length of vector a is

    length =

    Assignment

    Write a C++ program that performs the above mentioned operations on vectors with the help of above mentioned formulae. The program should have four user defined functions for the above operations.



    1) Addition

    2) Subtraction

    3) Multiplication

    4) Length

    The following menu of commands should be displayed at the start of program execution.

    Press 'a' to add two vectors

    Press 's' to subtract two vectors

    Press 'm' to multiply two vectors

    Press 'l to calculate the length of vector

    Press 'q' to quit

    Please enter your choice: a

    After the user selects a choice, prompt the user to enter the vector components on which the selected mathematical operation is to be performed, and then display the result. For example,

    if the user enters ‘a’ then your output should be:

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The sum is [ 8 , 10 ]

    if the user enters ‘s’ then your output should be :

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The difference is [ -4 , 4]

    if the user enters ‘m’ then your output should be :

    Enter first component of vector : 2

    Enter second component of vector : 7

    The vector is : [ 2 , 7 ]

    Enter first component of vector : 6

    Enter second component of vector : 3

    The vector is : [ 6 , 3 ]

    The multiplication is 33

    After the menu if the user selects ‘l’ then your output should be

    Enter first component of vector : 1

    Enter second component of vector : 2

    The vector is : [ 1 , 2 ]

    The length is 2.23607

    After the menu if the user selects ‘q’ then your output should be

    Press any key to continue …..

    Conditions that must be checked and fulfilled:

    1) If a user enters choice other then choices given in menu, then a message should be displayed “You have entered wrong choice:” and main menu should be displayed again.

    2) If a used enters a ,s or m then you have to take components of two vectors then do calculation on them

    3) If a user enters l then you have to take components of one vector only then do calculation on it.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    6
    this problem is from dev cpp
    how to program c++

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Could a MOD close this?

    I really cannot see the point in it. No questions asked about the subject, and the code is non-standard in places anyway.
    Double Helix STL

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Indeed, basharat obviously ignored my instruction to read about our homework policy.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  4. Adding vectors ??
    By Hussain Hani in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2006, 03:03 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM