Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Structures (http://www.go4expert.com/forums/structures-t16825/)

chemr2 7Apr2009 05:55

Structures
 
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);
}


xpi0t0s 8Apr2009 04:30

Re: Structures
 
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()?

xpi0t0s 8Apr2009 04:32

Re: Structures
 
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.


All times are GMT +5.5. The time now is 08:30.