### computational physics

Write your question here.
this is an incomplete code , what it should do is ask to input 2 .TXT files which are arrays, normalize them(take their norm) then integrate their product.
i am not sure how to write code asking to take the norm of the 2 .txt files , and i also keep getting too many errors.

Any help would greatly be appreciated.

Thanks!.
Ash

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` #include #include #include void read_vector( const std::vector &v, std::ifstream &infile ) { // TODO : write function } double norm( const std::vector &v ) { // TODO : implement norm } double integrate_prod( const std::vector &f, const std::vector &g, int a, int b ) { int area = 0; for ( auto f_ndx = begin(f) + a, g_ndx = begin(g) + a; f_ndx < begin(f) + b && g_ndx < begin(g) + b; ++f_ndx, ++g_ndx ){ area += (*f_ndx) * (*g_ndx); } return area; } int main() { std::vector f, g; std::ifstream proton, newtron; proton.open("proton.txt"); newtron.open("newtron.txt"); read_vector(f, proton); read_vector(g, newtron); integrate_prod(f, g, 0, f.size()); return 0; } Put the code you need help with here.``````
First things first, after your include statements, put 'using namespace std;'
It will make it easier, so you don't have to use std:: all the time.

For example:

 ``123456789101112131415161718`` ``````#include #include using namespace std; // This will stop the 'std::' int main() { // code here // Instead of this std::cout << "Hello World!" << endl; // You can just do this cout << "Hello World!" << endl; return 0; }``````

Less keystrokes makes for happier programmers.
 First things first, after your include statements, put 'using namespace std;' It will make it easier, so you don't have to use std:: all the time.

 Less keystrokes makes for happier programmers.

No, don't do that !

What that does is bring in the entire std namespace to the global namespace, defeating the purpose of namespaces - that is causing clashes with variable & function names. There are all kinds of variables that will clash like count, distance, left, right, merge, ratio plus many others. Instead of worrying about whether a name will clash, just put `std::` before each std thing, as the OP has done.

Ideally one should put their own stuff in it's own namespace.

@ashash

What do you mean by normalize? I can think of a few different meanings for that.
 First things first, after your include statements, put 'using namespace std;' It will make it easier, so you don't have to use std:: all the time.

 Less keystrokes makes for happier programmers.

DON'T DO THIS ( SEE ABOVE )

 No, don't do that !
Listen to this guy, just like in real life, STD's can be your friend or your foe. Wait... yeah, foe.
Last edited on
Topic archived. No new replies allowed.