Difficuty calculating numbers and putting them in a 2D array

For my assignment I need to create a code that calculates the distances between x coordinates and y coordinates for 25 people. I then need to compare these with a single person to see how similar the values are. When I calculate the distances and try to put them in an array all I get is a single number instead of 25.

Here is what the first input file (facedata.txt) looks like:

3.55 3.17 3.90 3.91 3.17
Eduardo Ravelo
2.41 3.65 2.92 1.92 1.14
3.55 3.15 3.92 3.93 3.15
Rita Crundwell
2.41 3.69 2.96 1.88 1.10
3.55 3.13 3.94 3.95 3.13
Yaser Said
2.41 3.73 3.00 1.84 1.06
3.55 3.11 3.96 3.97 3.11
Sun Kailiang
2.41 3.77 3.04 1.80 1.02
3.55 3.09 3.98 3.99 3.09
Alexsey Belan
2.41 3.81 3.08 1.76 0.98
3.55 3.07 4.00 4.01 3.07
Rebecca Adams
2.41 3.85 3.12 1.72 0.94
3.55 3.05 4.02 4.03 3.05
Evgeniy Bogachev
2.41 3.89 3.16 1.68 0.90
3.55 3.03 4.04 4.05 3.03
Lea Fastow
2.41 3.93 3.20 1.64 0.86
3.55 3.01 4.06 4.07 3.01
Victor Wolf
2.41 3.97 3.24 1.60 0.82
3.55 2.99 4.08 4.09 2.99
Julieanne Dimitrion
2.41 4.01 3.28 1.56 0.78
3.55 2.97 4.10 4.11 2.97
Aviv Mizrahi
2.41 4.05 3.32 1.52 0.74
3.55 2.95 4.12 4.13 2.95
Boniface Ozoemena
2.41 4.09 3.36 1.48 0.70
3.55 2.93 4.14 4.15 2.93
Patricia Saa
2.41 4.13 3.40 1.44 0.66
3.55 2.91 4.16 4.17 2.91
Amy Gilligan
2.41 4.17 3.44 1.40 0.62
3.55 2.89 4.18 4.19 2.89
Victor Gerena
2.41 4.21 3.48 1.36 0.58
3.55 2.87 4.20 4.21 2.87
Harriette Walters
2.41 4.25 3.52 1.32 0.54
3.55 2.85 4.22 4.23 2.85
Nicolae Popescu
2.41 4.29 3.56 1.28 0.50
3.55 2.83 4.24 4.25 2.83
Viet Nguyen
2.41 4.33 3.60 1.24 0.46
3.55 2.81 4.26 4.27 2.81
Robert Fisher
2.41 4.37 3.64 1.20 0.42
3.55 2.79 4.28 4.29 2.79


Here is what suspect_1.txt looks like:

2.41 4.14 3.42 1.40 0.58
3.55 2.87 4.20 4.21 2.87


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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204


#include <iostream>  /* Allows ouputes to the screen */
#include <fstream>  /* Allows files to be input and ouput */
#include <string>   /* Allows file strings to be used */
#include <iomanip>
#include <math.h>

 using namespace std; /* Allows certain parts of the code to be written without the prefix stating it is from the standard library */

double facelines(double array1[25][5], double array2[25][5]);
double facelines2(double array1[1][5], double array2[1][5]);
double ssd(double array1[25][10], double array2[1][10]);

 int main()  /* This signals the start of the main function */
 {
     
     ifstream facedata; 
     ofstream names, xcoor, ycoor, dist1, dist2, ssddata;
     int a;
     double xcor[25][5], ycor[25][5], distancearray[25][10], distancearraysuspect[1][10], ssdcalc[25][1];
     string name[25];

	facedata.open("facedata.txt");
	names.open("names.txt");
	xcoor.open("xcoor.txt");
	ycoor.open("ycoor.txt");
	dist1.open("dist1.txt");
	dist2.open("dist2.txt");
	ssddata.open("ssddata.txt");
	


	facedata >> a; 

     for (int c=0; c<a; c++)
     {
         
         for (int b=1; b<=2; b++)
         {
         facedata >> name[c];
         names << name[c];
         cout << name[c] << endl;
         }
         cout << endl;
         
         
         for (int w=1; w<=5; w++)
         {
             facedata >> xcor[c][w]; 
             xcoor << xcor[c][w];

         }


         for (int p=1; p<=5; p++)
         {
             facedata >> ycor[c][p];
             ycoor << ycor[c][p];

         }
         }
         
         distancearray[25][10]=facelines(xcor, ycor);   

     facedata.close();
     names.close();;
     xcoor.close();
     ycoor.close();
     
     
     
     string infile;
     ifstream suspect;
     double xsuspect[1][5], ysuspect[1][5];
     
     cout << "What is the name of the test file?  ";
     cin >> infile;
     
     suspect.open(infile.c_str());
     if(suspect.fail())
     {
                    cerr << "Error: File not found."<< endl;
                    system("pause");
                    exit(1);
     }
     
     
     for (int c=0; c<=1; c++)
     {
              for (int w=1; w<=5; w++)
         {
             suspect >> xsuspect[c][w];
         }


         for (int p=1; p<=5; p++)
         {
             suspect >> ysuspect[c][p];
         }
         }
         
         
         distancearraysuspect[1][10]=facelines2(xsuspect, ysuspect);
         dist1 << distancearray[25][10];
         dist2 << distancearraysuspect[1][10];
         
         dist1.close();
         dist2.close();
         
         
         ssdcalc[25][1]=ssd(distancearray, distancearraysuspect);
         ssddata << ssdcalc[25][1];
         
         ssddata.close();


     system("pause");
     return(0);
 } 
 
 
double facelines(double array1[25][5], double array2[25][5])
 {
      double distance1a2, distance1a3, distance1a4, distance1a5, distance2a3, distance2a4, distance2a5, distance3a4, distance3a5, distance4a5, distancearray[25][10];
      
          for (int c=0; c<=25; c++)
          {

              distance1a2=sqrt(pow((array1[c][0]-array1[c][1]),2)+pow((array2[c][0]-array2[c][1]),2));
 
              distance1a3=sqrt(pow((array1[c][0]-array1[c][2]),2)+pow((array2[c][0]-array2[c][2]),2));
  
              distance1a4=sqrt(pow((array1[c][0]-array1[c][3]),2)+pow((array2[c][0]-array2[c][3]),2));
  
              distance1a5=sqrt(pow((array1[c][0]-array1[c][4]),2)+pow((array2[c][0]-array2[c][4]),2));
    
              distance2a3=sqrt(pow((array1[c][1]-array1[c][2]),2)+pow((array2[c][1]-array2[c][2]),2));

              distance2a4=sqrt(pow((array1[c][1]-array1[c][3]),2)+pow((array2[c][1]-array2[c][3]),2));
  
              distance2a5=sqrt(pow((array1[c][1]-array1[c][4]),2)+pow((array2[c][1]-array2[c][4]),2));

              distance3a4=sqrt(pow((array1[c][2]-array1[c][3]),2)+pow((array2[c][2]-array2[c][3]),2));

              distance3a5=sqrt(pow((array1[c][2]-array1[c][4]),2)+pow((array2[c][2]-array2[c][4]),2));

              distance4a5=sqrt(pow((array1[c][3]-array1[c][4]),2)+pow((array2[c][3]-array2[c][4]),2));
              
              distancearray[c][10]=(distance1a2, distance1a3, distance1a4, distance1a5, distance2a3, distance2a4, distance2a5, distance3a4, distance3a5, distance4a5);
              }
              
              return(distancearray[25][10]);
              
}

double facelines2(double array1[1][5], double array2[1][5])
 {
      double distance1a2, distance1a3, distance1a4, distance1a5, distance2a3, distance2a4, distance2a5, distance3a4, distance3a5, distance4a5, distancearray[25][10];
      
          for (int c=0; c<=1; c++)
          {

              distance1a2=sqrt(pow((array1[c][0]-array1[c][1]),2)+pow((array2[c][0]-array2[c][1]),2));
 
              distance1a3=sqrt(pow((array1[c][0]-array1[c][2]),2)+pow((array2[c][0]-array2[c][2]),2));
  
              distance1a4=sqrt(pow((array1[c][0]-array1[c][3]),2)+pow((array2[c][0]-array2[c][3]),2));
  
              distance1a5=sqrt(pow((array1[c][0]-array1[c][4]),2)+pow((array2[c][0]-array2[c][4]),2));
    
              distance2a3=sqrt(pow((array1[c][1]-array1[c][2]),2)+pow((array2[c][1]-array2[c][2]),2));

              distance2a4=sqrt(pow((array1[c][1]-array1[c][3]),2)+pow((array2[c][1]-array2[c][3]),2));
  
              distance2a5=sqrt(pow((array1[c][1]-array1[c][4]),2)+pow((array2[c][1]-array2[c][4]),2));

              distance3a4=sqrt(pow((array1[c][2]-array1[c][3]),2)+pow((array2[c][2]-array2[c][3]),2));

              distance3a5=sqrt(pow((array1[c][2]-array1[c][4]),2)+pow((array2[c][2]-array2[c][4]),2));

              distance4a5=sqrt(pow((array1[c][3]-array1[c][4]),2)+pow((array2[c][3]-array2[c][4]),2));
              
              distancearray[c][10]=(distance1a2, distance1a3, distance1a4, distance1a5, distance2a3, distance2a4, distance2a5, distance3a4, distance3a5, distance4a5);
              }
              
              return(distancearray[1][10]);
              
}


double ssd(double array1[25][10], double array2[1][10])
{
       double ssd[25][1];
       
       for (int c=0; c<=25; c++)
       {
           ssd[c][1]=(pow((array1[c][0]-array2[c][0]),2)+pow((array1[c][1]-array2[c][1]),2)+pow((array1[c][2]-array2[c][2]),2)+pow((array1[c][3]-array2[c][3]),2)+pow((array1[c][4]-array2[c][4]),2)+pow((array1[c][5]-array2[c][5]),2)+
+pow((array1[c][6]-array2[c][6]),2)+pow((array1[c][7]-array2[c][7]),2)+pow((array1[c][8]-array2[c][8]),2)+pow((array1[c][9]-array2[c][9]),2));
       }
       
       return(ssd[25][1]);
}
1
2
3
4
5
6
double ssd(double array1[25][10], double array2[1][10])
{
       double ssd[25][1];
       //...
       return(ssd[25][1]);
}


As mentioned in the other thread you posted:
http://www.cplusplus.com/forum/beginner/163139/

Your return line is attempting to return an out of bounds element in the array, and not the array as a whole.

Furthermore, the return type of your function is a double .. that is... a singular value. Not an array.

But that kind of doesn't matter, because as mentioned in that other thread -- you cannot return arrays from functions -- the language simply does not allow it.


Alternatively, you can pass the 'out' array to the function, and that function can modify it, so you don't have to return anything.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void func( int out[5], const int in[5] )
{
    for(int i = 0; i < 5; ++i)
        out[i] = in[i] * 2;
}

int main()
{
    int foo[5] = {1,2,3,4,5};
    int bar[5];

    func(bar, foo);

    // at this point, bar contains:  {2,4,6,8,10}
}




EDIT:

Actually, now that I look at your code... you are going out of bounds all over the place. That is very, very bad.

Remember that when you make an array of size N, valid indexes are between 0 and N-1.

So with an array of size 5... 0,1,2,3,4 are valid indexes, but anything else is out of bounds.


Your for loops are ill-formed for this, too. You seem to be starting your indexes at 1 instead of 0... and doing <= max rather than < max:

1
2
3
4
5
         for (int w=1; w<=5; w++)  // <- BAD BAD BAD, w=5 is out of bounds for these arrays!
         {
             facedata >> xcor[c][w]; 
             xcoor << xcor[c][w];
         }


This loop:
 
for (int w=1; w<=5; w++)


really should be this:

 
for( int w = 0; w < 5; ++w )


Notice you start at zero and you don't allow it to reach the size, that is, you use < and not <=.

Most/all of your for loops should be structured this way.
Last edited on
I understand what you're saying, but my professor has asked us to so unless I am completely misunderstanding him, there must be a way.
I understand what you're saying, but my professor has asked us to so unless I am completely misunderstanding him, there must be a way.


You must be misunderstanding.

Returning an array from a function is quite literally impossible in C++.


EDIT:

When I say "impossible", I'm referring to proper arrays. There are ways you can cheat like using container classes, or putting the array in a struct, but I'm sure that's not what your professor is intending you to do.
Last edited on
Is it possible to save information from a function into a text file and then use the information in the text file in order to do further calculations in the main function?
Here is what my professor has said: "Call a function facelines() written in your code that calculates and stores the 10 facial distances of each person in the database and for the test subject."
So I have put the calculations from the separate functions into text files and I am getting out the correct information now! Thanks for the advice that was really helpful! Stay tuned because I will probably need more help soon haha
Is it possible to save information from a function into a text file and then use the information in the text file in order to do further calculations in the main function?


Yes you could do that, but it's a terrible idea.

Just pass the array and use it as output as I illustrated above.
Topic archived. No new replies allowed.