How would I write the output to a txt file? Language is C++. #include<iostream>
ID: 3919517 • Letter: H
Question
How would I write the output to a txt file? Language is C++.
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int vi = 0;
char vertex[100]; //Set array vertex to allow 100 items
struct nod{ //Struct a node class called nod
char x;
struct nod *rep;
struct nod *next;
};
struct list{ //Struct a list class called list that consists of two nodes (the head and the tail)
nod node;
struct nod *head;
struct nod *tail;
};
class dsets{
list *s[100]; //Struct a referenced array called s with 100 items
int nlist; //Create an int variable called nlist to be used to reference an item in array s later in the program
public:
dsets(){
nlist = 0;
}
void makeset(char a){
s[nlist] = new list;
s[nlist]->node.x = a;
s[nlist]->head = s[nlist]->tail = s[nlist]->node.rep = &(s[nlist]->node); //Start at nlist item in array s and then look at the head, tail, and node.rep and create the node in the correct position
s[nlist++]->node.next = NULL; //Set the next node = NULL
}
list* findset(char a){ //Find the set in list
int i;
nod *trav;
for (i = 0; i < nlist; i++){
trav = s[i]->head;
while (trav != NULL){
if (trav->x == a)
return s[i];
trav = trav->next;
}
}
return NULL;
}
list* unin(list *a, list *b){ //Uninitialize node in the list
nod* trav = b->head;
a->tail->next = b->head;
a->tail = b->tail;
while (trav != NULL){
trav->rep = a->head;
trav = trav->next;
}
b->head = b->tail = NULL;
return a;
}
void disp(){ //Display array s
nod *trav;
for (int i = 0; i < nlist; i++){
trav = s[i]->head;
while (trav != NULL){
cout << trav->x << " ";
trav = trav->next;
}
cout << endl;
}
}
};
int main(){
char b, a;
int forest[100];
char edge[100][2];
int cost[100];
string line;
int ei = 0;
dsets disset;
int c, d, i, fi = 0, j, t, weight;
ifstream input("C:\Temp\graph.txt"); //Open the file graph.txt located in the Temp folder in the C drive
while (getline(input, line)){
string token;
int i = 0;
istringstream ss(line);
while (getline(ss, token, ',')){ //While reading graph.txt, mark certain integers in certain areas
if (i == 0)
a = token[0]; //If the first integer, make a = token[0]
if (i == 1)
b = token[0]; //If the second integer, make b = token[0]
if (i == 2)
weight = stoi(token); //If the third integer, make weight = stoi(token)
i++; //Move to the next integer until the file is finished
}
edge[ei][0] = a; //Set the first edge = a
edge[ei][1] = b; //Set the second edge = b
cost[ei++] = weight; //Set the weight
c = 0, d = 0; //Initialize int c and d
for (i = 0; i < vi; i++){
if (a == vertex[i]){
c++; //If a, increase c by 1
}
if (b == vertex[i]){
d++; //If b, increase d by 1
}
}
if (c == 0){
vertex[vi++] = a; //If c, set the next vertex = a
}
if (d == 0){
vertex[vi++] = b; //If d, set the next vertex to b
}
}
for (i = 0; i < ei; i++)
for (j = 0; j < ei - i - 1; j++)
if (cost[j] > cost[j + 1]){ //If cost of the first weight is greater than the cost of the next weight, swap the values
t = cost[j];
cost[j] = cost[j + 1];
cost[j + 1] = t;
for (int k = 0; k < 2; k++){ //For these conditions, swap edge[j][k] and edge[j+1][k]
t = edge[j][k];
edge[j][k] = edge[j + 1][k];
edge[j + 1][k] = t;
}
}
for (i = 0; i < vi; i++) //For these conditions, create a new set
disset.makeset(vertex[i]);
for (i = 0; i < ei; i++){ //For these conditions, if edge[i][0] does not equal edge[i][1], create a new set
if (disset.findset(edge[i][0]) != disset.findset(edge[i][1])){
forest[fi++] = i;
disset.unin(disset.findset(edge[i][0]), disset.findset(edge[i][1]));
}
}
int count = 0;
cout << "The minimum spanning tree of the entered graph is the one through the following edges ";
for (i = 0; i < fi; i++){ //For all nodes, print them in a tree
cout << edge[forest[i]][0] << " " << edge[forest[i]][1] << " " << cost[i] << endl;
count += cost[i];
}
cout << endl << "Total weight = " << count << endl;
}
Explanation / Answer
To write the output to a text file use the code freopen("output.txt", "w", stdout); in the program or redirect the output from the command line as given below,
programname > output.txt
The output of program is sent to output.txt file.
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int vi = 0;
char vertex[100]; //Set array vertex to allow 100 items
struct nod{ //Struct a node class called nod
char x;
struct nod *rep;
struct nod *next;
};
struct list{ //Struct a list class called list that consists of two nodes (the head and the tail)
nod node;
struct nod *head;
struct nod *tail;
};
class dsets{
list *s[100]; //Struct a referenced array called s with 100 items
int nlist; //Create an int variable called nlist to be used to reference an item in array s later in the program
public:
dsets(){
nlist = 0;
}
void makeset(char a){
s[nlist] = new list;
s[nlist]->node.x = a;
s[nlist]->head = s[nlist]->tail = s[nlist]->node.rep = &(s[nlist]->node); //Start at nlist item in array s and then look at the head, tail, and node.rep and create the node in the correct position
s[nlist++]->node.next = NULL; //Set the next node = NULL
}
list* findset(char a){ //Find the set in list
int i;
nod *trav;
for (i = 0; i < nlist; i++){
trav = s[i]->head;
while (trav != NULL){
if (trav->x == a)
return s[i];
trav = trav->next;
}
}
return NULL;
}
list* unin(list *a, list *b){ //Uninitialize node in the list
nod* trav = b->head;
a->tail->next = b->head;
a->tail = b->tail;
while (trav != NULL){
trav->rep = a->head;
trav = trav->next;
}
b->head = b->tail = NULL;
return a;
}
void disp(){ //Display array s
nod *trav;
for (int i = 0; i < nlist; i++){
trav = s[i]->head;
while (trav != NULL){
cout << trav->x << " ";
trav = trav->next;
}
cout << endl;
}
}
};
int main(){
char b, a;
int forest[100];
char edge[100][2];
int cost[100];
string line;
int ei = 0;
dsets disset;
int c, d, i, fi = 0, j, t, weight;
freopen("output.txt", "w", stdout); // to write the output to the output.txt file
ifstream input("C:\Temp\graph.txt"); //Open the file graph.txt located in the Temp folder in the C drive
while (getline(input, line)){
string token;
int i = 0;
istringstream ss(line);
while (getline(ss, token, ',')){ //While reading graph.txt, mark certain integers in certain areas
if (i == 0)
a = token[0]; //If the first integer, make a = token[0]
if (i == 1)
b = token[0]; //If the second integer, make b = token[0]
if (i == 2)
weight = stoi(token); //If the third integer, make weight = stoi(token)
i++; //Move to the next integer until the file is finished
}
edge[ei][0] = a; //Set the first edge = a
edge[ei][1] = b; //Set the second edge = b
cost[ei++] = weight; //Set the weight
c = 0, d = 0; //Initialize int c and d
for (i = 0; i < vi; i++){
if (a == vertex[i]){
c++; //If a, increase c by 1
}
if (b == vertex[i]){
d++; //If b, increase d by 1
}
}
if (c == 0){
vertex[vi++] = a; //If c, set the next vertex = a
}
if (d == 0){
vertex[vi++] = b; //If d, set the next vertex to b
}
}
for (i = 0; i < ei; i++)
for (j = 0; j < ei - i - 1; j++)
if (cost[j] > cost[j + 1]){ //If cost of the first weight is greater than the cost of the next weight, swap the values
t = cost[j];
cost[j] = cost[j + 1];
cost[j + 1] = t;
for (int k = 0; k < 2; k++){ //For these conditions, swap edge[j][k] and edge[j+1][k]
t = edge[j][k];
edge[j][k] = edge[j + 1][k];
edge[j + 1][k] = t;
}
}
for (i = 0; i < vi; i++) //For these conditions, create a new set
disset.makeset(vertex[i]);
for (i = 0; i < ei; i++){ //For these conditions, if edge[i][0] does not equal edge[i][1], create a new set
if (disset.findset(edge[i][0]) != disset.findset(edge[i][1])){
forest[fi++] = i;
disset.unin(disset.findset(edge[i][0]), disset.findset(edge[i][1]));
}
}
int count = 0;
cout << "The minimum spanning tree of the entered graph is the one through the following edges ";
for (i = 0; i < fi; i++){ //For all nodes, print them in a tree
cout << edge[forest[i]][0] << " " << edge[forest[i]][1] << " " << cost[i] << endl;
count += cost[i];
}
cout << endl << "Total weight = " << count << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.