read a specific line from the file

hi, well first I sorry if my english is not very good .

In this homework from my university is about that a family what want visit the cities starting in a city and visiting others in a order specific . I have to read from a file that the first line is the cases (the amount of cases that the program will prove) and the second is the number of vertex because is a graph so later is the Adjacency matrix, example:

2
5
0 4 0 1 0
4 0 1 3 5
0 1 0 0 2
1 3 0 0 8
0 5 2 8 0
1 3 4
8
0 0 0 0 0 0 1 1
0 0 0 7 0 0 0 1
0 0 0 0 3 2 0 0
0 7 0 0 3 2 0 0
0 0 3 3 0 0 1 4
0 0 2 2 0 0 0 0
1 0 0 0 1 0 0 0
1 1 0 0 4 0 0 0
4 1 5 7

but later of the matrix is a line that represent the order or sequence of the vertex of start to final vertex

and well I don't know how to make that read that specific line and later put it in a array, the funtion strtok can use it for it later , but I tried with getline but it put it all in a string including the second matrix .

I use a "for" for what repeat the cycle to the number of cases and later I read the matrix and fill it and later read that other line doing then the cycle will do it the same for the other case.

please can someone help me with this? I try with a while but did not work.

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

int main () {
	ifstream fin ("tarea1.in");
	ofstream fout ("jose_silva.out");
	
	int c,v, w, contador;
	contador= 0;
	int p;
	int sum=0;
	string path ;
	char city;
	
	
	if (fin.fail() )
		cout<< "error al abrir el archivo" << endl;
	
	fin >> c ;
	
	
	for (int cases =0; cases < c; cases++) { 		
		fin >> v ;
		
		int mat [v][v];
		
			for (int a=0; a<v; a++){
				for (int b=0; b<v;b++){
					fin>> p;
					mat [a][b]= p;
					sum = sum + p;
	
					}
				}
				
				
		/*fin>> city;
		
		while (city != "\n"){
			if (city != " "){
				contador++;
				}
			fin>> city;	
			}
		*/		
		
		cout << "casos de prueba: " << c << " " << "vertices: " << v << endl; 
		cout << sum << endl;
		
		
		for (int i=0;i<v;i++){		
			for (int j=0 ; j<v ;j++){
				cout<< mat [i][j];
			}
			cout<<"\n";
		}
		
	
	}
	
	
	return 0;
}
Please be more specific to help me understand.
but later of the matrix


2 //the first line is the cases
5 // the second is the number of vertex. I see the below line is 5 values
0 4 0 1 0 // is this the matrix or is it below ?
4 0 1 3 5
0 1 0 0 2
1 3 0 0 8
0 5 2 8 0
1 3 4 // I have no idea what this is
8 // I see the below line is 8 values
0 0 0 0 0 0 1 1
0 0 0 7 0 0 0 1
0 0 0 0 3 2 0 0
0 7 0 0 3 2 0 0
0 0 3 3 0 0 1 4
0 0 2 2 0 0 0 0
1 0 0 0 1 0 0 0
1 1 0 0 4 0 0 0
4 1 5 7 // I have no idea what this is

I think line 28 should be "maybe"
int mat [c][v];

I have no idea what your doing in lines after 30, maybe add some comments.
ok, in that part (in that part that you have no idea) I mean what the line next to the matrix is the sequence of cities that the family going to visit, and in that order is how visit the cities, example: 1 3 4 , so I can't visit first the city 1 and later the 4 , I can visit example: 1 2 3 4 while the order not be break it.

and the matrix is [V][V] because the matrix is square and work fine, if I want show the matrix with the variable show :


48
04010
40135
01002
13008
05280

and yeah, later the number of vetex is the matrix
Last edited on
Topic archived. No new replies allowed.