Getting used to functions (tidying my code)

I have several long pieces of code, but never bother using functions in them as it is just another thing to learn. I just wanted a bit of help with creating my functions and so have shortened the code as much as I can just to get the gist of it. I have defined a function at the beginning of the code, but it doesn't work (defining a double from a double or something).

Could someone please explain to a complete beginner why this hasn't run, how to fix it, why it is better to use functions in your code and why (I have seen this all over the place) people define their variables or parameters only when they are required?

I have defined my entire scope at the beginning of my code (except for the few things I had to define in the function I just put in) and think it looks much neater to have everything defined at the top of page out of the way. Is this just convention or is there a better reason?

My code is as follows:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <stdlib.h>
using namespace std;

double Length (double x[101][4]){
  double L;
  int i,n;
  for(i=1;i<=n;i++){
    L=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
  }
  return(L);
}

int main()
{

  int a,f,g,n,m,i,j,k,r,s;
  double p,q,Energy,energy,y[101][4],x[101][4],L,Distance;

 clock_t t1,t2;
  t1=clock();

  /*  set the number of points */
  n=10;

  /* check that there are no more than 100 points */
  if(n>100){
    cout << n << " is too many points for me :-( \n";
    exit(0);
  }
  
  /* reset the random number generator */
  srand((unsigned)time(0));  
  
  for (i=1;i<=n;i++){
    x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
    x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
    x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
  }
    
  L=Length (x[i][1],x[i][2],x[i][3]);
    
  for(i=1;i<=n;i++){  
    for (k=1;k<=3;k++){
      x[i][k]=x[i][k]/L;
    }
  }

  /* calculate the energy */
  Energy=0.0;
  
  for(i=1;i<=n;i++){
    for(j=i+1;j<=n;j++){
      Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
		            +pow(x[i][3]-x[j][3],2));
      
      Energy=Energy+1.0/Distance;
    }
  }
  
  /* Save Original Points */
  for(i=1;i<=n;i++){
    y[i][1]=x[i][1];
    y[i][2]=x[i][2];
    y[i][3]=x[i][3];
  }

  t2=clock();
    float diff ((float)t2-(float)t1);
    float seconds = diff / CLOCKS_PER_SEC;
    cout << fixed << setprecision(2) << "Run time: " << seconds << "(s)" << "\n";
    return 0;

}


The function is for the length and I want to call upon it to get:

x[i][k]=x[i][k]/Length

I will later define functions for Distance and Energy Calculations as well, but wanted to get the hang of them first.

Thanks, A.

Edit: I know that I am not utilising my arrays properly by starting from 1 instead of 0. This shall sorted as well in due time!
Last edited on
I suggest you to look at std::valarray
it allows you to operate with arrays directly (as you'll do in `octave')

If the idea is to normalize the vectors it would be
1
2
3
4
5
6
7
8
9
10
11
12
13
double Length(double *vector){
   double sum=0;
   for(int K=0; K<3; ++K)
      sum += vector[K]*vector[K]; //pow is overkill
   return sqrt(sum);
}

//calling
for(int K=0; K<n; ++K){
   double L = Length(x[K]);
   for(int L=0; L<n; ++L)
      x[K][L] /= L;
}
or if you prefer valarray
1
2
3
4
5
6
7
double Length(const valarray<double> &vector){
   return sqrt( (vector*vector).sum() );
}

//calling
for(int K=0; K<n; ++K)
   x[K] /= Length(x[K]);
Topic archived. No new replies allowed.