Quote:
Originally Posted by Ancient Dragon (Post 554946) >>cout<<tail<<head<<cost<<capacity<<endl;
use the loop counter in this line
cout<<tai[i]l<<head[i]<<cost[i]<<capacity[i]<<endl;
>>If possible I would like to try and figure out how to have it display the titles on top of the numbers...
Simple -- print the titles just before line 36 (before the loop starts) |
Thanks,
Ok now I have another problem I'm trying to solve...
I have two cpp files, one was written by "DJ" it's dijsktras algorithm, and I want to utilize my program into it.
I was trying to merge the two files together but couldn't get it to work... here is each file separate, and then what I tried to go to get it to work.
Any and all help would be amazing!
Here is what I tried
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream>
#define infi 999
int i = 0;
int a;
int head[16];
int tail[16];
int capacity[16];
int cost[16];
int count;
ifstream inFile;
class queue
{
private:
int q[100];
int front, rear;
protected:
queue()
{
front=rear=-1;
}
int isempty()
{
if((front==-1 && rear==-1) || (front>rear))
{
front=rear=-1;
return 1;
}
return 0;
}
void push(int a)
{
if(isempty())
front++;
q[++rear]=a;
};
int del()
{
return q[front++];
}
};
class dj :public queue
{
private:
int mat[10][10], dist[10], path[10];
public:
int n;
void input()
{
int i = 0;
int a;
int head[16];
int tail[16];
int capacity[16];
int cost[16];
int count;
ifstream inFile;
// initalize the text file, and make sure it works
inFile.open("fstarin.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
for (count= 0; count< 16; count = count++)
{
// input so it knows what each collum stands for
inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count];
}
cout<<tail[count];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>mat[i][j];
}
void init(int m)
{
push(m);
dist[m]=0;
for(int i=1;i<=n;i++)
{
if(i!=m)
{
push(i);
dist[i]=infi;
}
path[i]=0;
}
}
void min_dist(int m)
{
int v, w;
init(m);
while(!(isempty()))
{
v=del();
for(int i=1;i<=n;i++)
{
if(mat[v][i]!=0)
{
w=i;
if((dist[v]+mat[v][w])<(dist[w]))
{
dist[w]=dist[v]+mat[v][w];
path[w]=v;
}
}
}
}
}
void disp_path(int m)
{
int p=path[m];
if(p==0)
return;
disp_path(p);
cout<<" "<<m;
}
void disp_dist(int m)
{
cout<<"cost: "<<dist[m]<<endl<<endl;
}
};
void main()
{
clrscr();
int c=0;
dj a;
a.input();
cout<<"\n\nPress any key to continue"<<endl;
getch();
clrscr();
for(int i=1;i<=a.n;i++)
{
a.min_dist(i);
for(int j=1;j<=a.n;j++)
{
if(i!=j)
{
if(++c==10)
{
cout<<"\n\nPress any key to continue"<<endl;
getch();
clrscr();
c=0;
}
cout<<"From "<<i<<" to "<<j<<":"<<endl;
cout<<"------------"<<endl;
cout<<"Minimum distance route: ("<<i;
a.disp_path(j);
cout<<")"<<endl;
a.disp_dist(j);
}
}
}
cout<<"\n\nPress any key to exit"<<endl;
getch();
}
Here is the files for each, first is my file to refrence the nodes in the text file earlier in post and second is dijstras
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int i = 0;
int a;
int head[16];
int tail[16];
int capacity[16];
int cost[16];
int count;
ifstream inFile;
// initalize the text file, and make sure it works
inFile.open("fstarin.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
for (count= 0; count< 16; count = count++)
{
// input so it knows what each collum stands for
inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count];
}
cout<<"enter a node"<<endl;
cin>>a;
for (i=0;i<15;i++)
if (tail[i]==a)
{
cout<<tail[i]<<head[i]<<cost[i]<<capacity[i]<<endl;
}
return 0;
}
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define infi 999
class queue
{
private:
int q[100];
int front, rear;
protected:
queue()
{
front=rear=-1;
}
int isempty()
{
if((front==-1 && rear==-1) || (front>rear))
{
front=rear=-1;
return 1;
}
return 0;
}
void push(int a)
{
if(isempty())
front++;
q[++rear]=a;
};
int del()
{
return q[front++];
}
};
class dj :public queue
{
private:
int mat[10][10], dist[10], path[10];
public:
int n;
void input()
{
cout<<"Enter number of nodes:\n";
cin>>n;
cout<<"\n\nEnter adjacency matrix\n"<<endl;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>mat[i][j];
}
void init(int m)
{
push(m);
dist[m]=0;
for(int i=1;i<=n;i++)
{
if(i!=m)
{
push(i);
dist[i]=infi;
}
path[i]=0;
}
}
void min_dist(int m)
{
int v, w;
init(m);
while(!(isempty()))
{
v=del();
for(int i=1;i<=n;i++)
{
if(mat[v][i]!=0)
{
w=i;
if((dist[v]+mat[v][w])<(dist[w]))
{
dist[w]=dist[v]+mat[v][w];
path[w]=v;
}
}
}
}
}
void disp_path(int m)
{
int p=path[m];
if(p==0)
return;
disp_path(p);
cout<<" "<<m;
}
void disp_dist(int m)
{
cout<<"Cost: "<<dist[m]<<endl<<endl;
}
};
void main()
{
clrscr();
int c=0;
dj a;
a.input();
cout<<"\n\nPress any key to continue"<<endl;
getch();
clrscr();
for(int i=1;i<=a.n;i++)
{
a.min_dist(i);
for(int j=1;j<=a.n;j++)
{
if(i!=j)
{
if(++c==10)
{
cout<<"\n\nPress any key to continue"<<endl;
getch();
clrscr();
c=0;
}
cout<<"From "<<i<<" to "<<j<<":"<<endl;
cout<<"------------"<<endl;
cout<<"Minimum distance route: ("<<i;
a.disp_path(j);
cout<<")"<<endl;
a.disp_dist(j);
}
}
}
cout<<"\n\nPress any key to exit"<<endl;
getch();
}