Errors with For-loop

Hi guys. I am new to C++ and I have trouble finishing this assignment

here is the assignment

Your task is to find the person in the class who is most similar to you.

The file neo-ffi.dat contains the NEO-FFI personality test response vectors for the entire class. To find the person who is most similar to you, you must take the absolute-value difference of your NEO-FFI vector from that of everyone else in the class, take the sum of those differences, and find the vector for which the sum is a minimum.

You will need to write functions int manhattan(int[] x, int[] y), which gives the Manhattan distance between your vector and that of a classmate's, and int argmin(int[] manhattan), which finds the index of the minimum Manhattan distance. Finding the data vector itself is sufficient to complete the assignment.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

#define N 69
#define M 10

int argmin(int x[]) {
 int i;
 int min = x[0], argmin=0;
 for (i=0; i<N; i++)
 if (min > x[i]) {
    min = x[i];
    argmin = i;
 }
 return argmin;
 }

int manhattan(int x[], int y[]) {
 int i, s = 0;
 for (i=0; i<M; i++)
     s += abs(x[i]-y[i]);
 return s;
}

int main() {
 int d[N];
 int p[N][M];
 int i, j;
 string initial;
 ifstream infile;
 infile.open("neo-ffi.dat");

 if (infile) {
  for (i=0; i<N; i++) {
   infile >> initial;
   for (j=0; j<M; j++)
    infile >> p[i][j];
   }} else {
  cout << "File could not be opened!";
  exit(1);
  }
  
  int me[M] = {5,3,4,3,1,4,1,3,4,2};
  int you[M];

 for (i=0; i<N; i++) {
  for (j=0; j<M; j++) {
   you[i] = p[i][j];
 } d[i] = manhattan(me, you);
 }

 int similar = argmin(d);
 for (j=0; j<M; j++) {
  cout << p[similar][j] << " ";
 } cout << endl;
 return 0;
 }


Update: For-loop error is fixed. Segmentation is fixed. But when I compile the code, it did not run as it supposed to find the person who is most similar to me. Would you please help me? Thanks
Last edited on
You appear to have an errant semicolon in your for() statements. Remember a for() statement looks like for(initialization; condition; increment). Your statements have an extra semicolon.


@jib: Thank you. The error is fixed. But when I run the code. It said segmentation fault. Would you please tell me where the fault is happened?
Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”

Look through your arrays. Make sure things are right and you're not trying to access an element that you dont have.
Segmentation is fixed. But when I compile the code, it did not run as it supposed to find the person who is most similar to me. Would you please help me? Thanks

Here is the file neo-ffi.dat

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
A 2 4 3 4 1 4 1 4 1 2
A 3 2 3 4 3 3 5 4 2 4
A 4 1 1 3 2 3 4 5 4 4
A 4 4 3 5 5 3 4 4 5 2
A 4 4 5 4 4 2 2 2 4 4
B 2 5 1 4 5 5 3 5 4 4
B 3 5 4 4 2 3 3 4 2 4
B 3 5 4 4 4 4 3 3 4 3
B 4 4 3 3 3 4 4 4 4 5
B 5 3 3 4 2 1 3 4 3 3
B 5 5 4 5 4 2 3 4 5 4
C 2 5 3 5 2 4 1 5 2 5
C 4 3 1 4 4 2 1 5 2 5
C 4 3 2 3 3 2 3 5 3 3
C 4 5 3 3 4 3 2 5 3 5
C 5 2 1 3 2 1 2 5 2 5
D 3 4 2 5 2 4 1 5 1 5
D 3 5 2 4 2 3 2 4 3 4
D 4 5 3 4 3 4 3 5 4 4
D 5 4 4 4 5 3 3 4 3 5
E 2 4 4 3 2 5 3 3 2 5
F 3 3 3 3 4 3 4 4 2 4
F 4 5 4 5 1 5 2 4 5 5
G 3 4 4 4 2 4 2 3 4 4
H 3 4 2 4 1 4 2 5 3 5
H 5 4 5 1 4 3 1 4 5 2
I 4 4 3 4 5 3 3 5 4 4
J 1 5 1 3 4 4 3 5 2 4
J 2 4 1 5 1 4 4 5 2 4
J 2 5 1 4 3 4 2 4 3 3
J 2 5 2 3 5 4 2 5 2 5
J 3 4 3 4 3 4 2 4 2 4
J 3 5 2 4 3 4 2 5 3 4
J 3 5 3 4 3 5 1 3 1 5
J 3 5 4 3 3 2 3 3 4 3
J 4 4 3 2 2 2 2 4 4 4
J 4 4 3 3 4 3 3 4 5 4
J 4 5 4 4 4 4 2 3 3 4
J 5 3 4 4 3 4 3 4 5 5
J 5 4 2 3 2 2 2 4 4 4
J 5 5 2 3 1 2 1 5 5 4
K 2 3 4 4 3 4 3 4 2 5
K 3 4 2 3 2 4 4 5 2 2
K 3 4 4 5 3 2 2 3 5 3
L 2 4 2 3 5 3 3 2 4 1
L 3 5 3 3 2 3 3 3 3 5
L 5 3 4 3 1 4 1 3 4 2
M 1 4 4 5 1 5 2 4 3 5
M 3 3 4 3 2 5 2 4 3 3
M 3 4 2 5 2 3 1 4 3 4
M 4 2 2 3 2 2 3 4 4 4
M 4 4 3 4 3 2 3 5 2 5
M 4 4 4 4 4 3 3 4 5 3
N 2 5 4 4 4 4 1 5 2 5
N 3 4 3 5 2 3 3 3 1 4
N 3 5 3 5 2 4 2 4 3 3
N 4 3 1 5 1 4 1 5 2 5
N 4 5 3 3 3 4 3 5 5 4
R 4 5 2 4 3 5 2 5 2 4
R 5 2 3 1 1 1 3 5 5 3
S 2 4 3 3 4 4 1 3 5 3
S 3 4 2 5 2 5 3 5 1 5
S 4 2 2 4 3 3 1 5 4 4
S 4 5 1 5 1 4 2 5 3 5
T 3 5 3 2 4 3 4 4 4 3
T 4 3 2 2 1 3 5 4 5 5
V 5 2 3 3 3 1 2 4 2 3
X 5 4 3 4 2 2 2 4 4 3
Z 4 3 3 3 2 3 2 4 3 4
Last edited on
when you use filestream, remember to close it. and on line 47, you have size of M= 10, but on line 51, it will go with the size of N = 69.
when you use filestream, remember to close it.

Why? Unless your going to reuse the stream why not just let the destructor close the file when the instance goes out of scope?

Topic archived. No new replies allowed.