I am writing a c++ program that reads in a text file that looks like this: 9 14
ID: 3818905 • Letter: I
Question
I am writing a c++ program that reads in a text file that looks like this:
9 14
1 2 4
1 8 8
2 3 8
2 8 11
3 4 7
3 9 2
3 6 4
4 5 9
4 6 14
5 6 10
6 7 2
7 8 1
7 9 6
8 9 7
this file represents a directed graph. the first line indicates the number of vertices (9) and edges (14) , respectively. All lines below line one contain 3 integers that indicate information about an edge. There is an edge pointing from u to v with weight w (u v w).
The program reads in an edge weighted directed graph from file to build the adjacency lists.
I want to display the graph on the screen like this:
it displays information for each vertex, what other vertex it is connected to and the weight..
the program that i have written so far only prints 9 and 14...
int readFile () {
string line;
ifstream myfile ("GRAPH.txt");
if (myfile.is_open()) {
for (int lineno = 0; getline (myfile,line) && lineno < 1; lineno++)
if (lineno == 0)
cout << line << endl;
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
How do I save 9 and 14 into variables numberofvertices and numberofedges? How do I print the rest of the graph in the sample format above? I would appreciate any help.The program should work for other text file examples not just the one above.
914 1:(88)(24) 2 : (811) (38) 3 : (64) (92) (47) 4 : (614) (59) 5 : (610) 6:(72) 7 : (96) (81) 8 : (97) 42 35 81411267 886667 9123456789Explanation / Answer
// added all values in structure and then comparing and writing to the console
// ask if something not clear.
#include<iostream>
#include<fstream>
using namespace std;
class GraphRead
{
private:
int vert, edge;
struct graph
{
int u, v, w;
};
struct graph *mystruct ;
public:
GraphRead()
{
}
~GraphRead()
{
}
int readFile ()
{
// string line;
// int line = 0;
ifstream myfile ("E:\Graph.txt");
if (myfile.is_open())
{
// getline (myfile,line);
// cout << line << ' ';
myfile>>vert>>edge;
mystruct = new graph[edge];
cout<<vert<<" "<<edge<<endl;
int i = 0;
int test = 0;
// while ( getline (myfile,line) )
while(myfile>>test)
{
mystruct[i].u = test;
myfile>>mystruct[i].v;
myfile>>mystruct[i].w;
// parse and store 3 space seperated integers
// cout << mystruct[i].u<<" : " <<mystruct[i].v<<" : "<<mystruct[i].w<<endl;
i++;
}
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}
void showFile()
{
int i = 0;
int j = 0;
int count = 0;
int countprev = 0;
while(j<vert)
{
int temp = mystruct[i].u;
while(mystruct[i].u == temp)
{
count++;
i++;
}
cout<<mystruct[j].u<<" : ";
for(int k=count-1; k>=countprev; k--)
{
cout<<" "<<"("<<mystruct[k].v <<" "<<mystruct[k].w<<")";
}
cout<<endl;
j= j+count;
countprev = count;
// cout<<"values are: "<< j << count<< countprev<<endl;
}
}
};
int main()
{
GraphRead obj;
obj.readFile();
obj.showFile();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.