Structures

Discussion in 'C++' started by chemr2, Apr 7, 2009.

  1. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    I have a problem at hand that I have a few questions about and possibly could use some advice. The problem states:

    Write a program that defines an array of two vector records, where each record consists of two double precision components a and b. The program should permit the user to enter two vectors, call two funtions that return the sum and difference of the entered vectors, and display the results calculated by these functions.

    What is an example of an array of two vector records? I have to use structures to write this program.

    after the user enters the components how do you pass a structure to a function? If you even would have to.

    finally is there an example of a program like this that someone might know about I could glance at to get a few pointers?

    here is my code in progress(which doesnt compile)

    Code:
    #include<iostream>
    using namespace std;
    struct Vector
    {
       double a;
       double b;
    };
    double sum(Vector a,Vector b);
    
    int main()
    {
        double a1,a2,b1,b2;
        
        Vector a={a1,a2};
        Vector b={b1,b2};
        double sum1;
        
        cout<<"Please enter the components of the first vector"<<endl;
        cin>>a1;
        cin>>a2;
        cout<<"Please enter the components of the second vector"<<endl;
        cin>>b1;
        cin>>b2;
        
        sum(Vector a,Vector b);
        cout<<sum1<<endl;
        
        system("pause");
        return 0;
    }
    double sum(Vector a,Vector b)
    {
           double sum1;
           sum1 = a + b
           return(sum1);
    }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Let me guess, you're a mathematician, right?
    a=b;
    b=2;
    does NOT set a to 2, and if b was, say, 5 before all this, a would be set to 5.

    So you need to get values for a1,a2,b1,b2 BEFORE you do
    Vector a={a1,a2};
    Vector b={b1,b2};

    because all the above two lines do is to set a and b to uninitialised data; the later reading in of values into a1 et al has no effect on vectors a and b.

    You say this doesn't compile; could this be anything to do with the missing semicolon in sum()?
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The other reason it might not compile of course is that a sum of two vectors would be a vector, correct? Not a scalar, which is what your code is trying to do. sum1 also needs to be a Vector.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice