Hey guys, I'm new here, I looked through the help also searched and couldn't find anything that helped. Although I will be using the Algorithms you guys have on this site.
anyways I only took a intro course to C++ about four years ago and don't remember much I wrote a program for a class and I can't get it to work I feel as though I am missing something.
What I'm trying to do is have the programs prompt the user for a node. Then the program refrence a text file with four coloums and x amount of nodes (I'll post the numbers i have). and it post all the row's that match that number.
Here's what I have so far
#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"<<endl;
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];
}
// enter node
cout<<"enter a node"<<endl;
cin>>a;
// output node info regarding what node was entered
for (i=0;i<15;i++)
if (tail[i]==a)
{
cout<<tail<<head<<cost<<capacity<<endl;
}
return 0;
}
The text file is this
1 2 2 4
1 3 3 7
2 4 2 6
2 5 6 8
2 6 8 8
3 2 5 5
3 5 3 8
3 8 5 9
4 3 2 4
4 5 3 4
4 6 3 9
4 7 1 5
5 7 6 4
5 8 1 2
7 6 1 5
8 7 2 9
So for example what I'm trying to get it to do is if user enters 3 as the node in the prompt, it will display
3 2 5 5
3 5 3 8
3 8 5 9
If possible I would like to try and figure out how to have it display the titles on top of the numbers... for example
Tail Head Cost Capacity
3 2 5 5
3 5 3 8
3 8 5 9
Thanks for your guy's help!!
>>cout<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)
>>cout<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();
}