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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <fstream>
#include <iostream>
#include <vector>
 
void read_vector( const std::vector<double> &v, std::ifstream &infile ) {
    // TODO : write function
}
 
double norm( const std::vector<double> &v ) {
    // TODO : implement norm
}
 
double integrate_prod( const std::vector<double> &f,
                       const std::vector<double> &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<double> 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>

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.