Print things out in a table problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hypnotik
    New Member
    • Jun 2007
    • 87

    Print things out in a table problem

    Hello everyone. This is a relatively simple program that is driving me crazy. The user enters how many verticies and how many edges. The program then asks where the edges are...in a 2D array. If the user enters a 1 (indicated there is an edge) that array element is set to 1. For some reason my print out is all wrong. It prints out to many elements, and incorrect values.

    Any help is appreciated, I think I'm just missing some minor thing....but I cannot find it.

    Code:
    #include <iostream>
    using namespace std;
    void main ()
    {
    	int graph[10][10]={0,0}, num_vert=0, num_edge=0,r=0,c=0,cnt=0,i=0, j=0;
    	cout<<"Enter the number of verticies: "<<endl;
    	cin>>num_vert;
    	cout<<"Enter the number of edges: "<<endl;
    	cin>>num_edge;
    	for (i=0;i<num_edge;++i)
    		for (j=0;j<num_edge;++j)
    	{
    		cout<<"Is there an edge between "<<i<<" "<<j<<endl;
    		cin>>cnt;
    		if (cnt==1)
    			graph[i][j]=1;
    		else 
    			graph[i][j]=0;
    	}
    	i=0;
    	for (i=0;i<num_vert;++i)
    		for (j=0;j<num_vert;++j)
    			cout<<"GRAPH "<<i<<" "<<j<<" "<<graph[i][j]<<endl;
    	i=0;
    	do
    	{
    		cout<<"     "<<i;			// top header
    			++i;
    	}
    	while(i<num_vert);
    	int t=0;
    	do										//output
    	{
    		cout<<endl<<endl<<t<<"|";
    		for (int r=0;r<num_edge;++r)
    			for (int c=0;c<num_edge+1;++c)
    				cout<<"    "<<graph[r][c];
    		++t;
    	}
    	while ( t<num_vert);
    	cout<<endl<<"T "<<t<<" NUM_VERT "<<num_vert<<" NUM_EDGE "<<num_edge;
    }
    Thanks,
    J
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    void main() is nonstandard and can lead to all sorts of crazy hijinx that you really don't want. Use int main() instead.

    You're using ++i and ++j rather than i++ and j++. This means that at each iteration of the loop, rather than using the current value and then incrementing, it increments and uses that value. This may give you the behavior you seek, but generally the postfix (i++/j++) is preferred.

    Comment

    • Hypnotik
      New Member
      • Jun 2007
      • 87

      #3
      Made those 2 changes however the problem is still there.

      For instance when I select graph[0][0] as a yes, so graph[0][0]=1 it prints out a 1 in all columns of 0 instead of just at 0,0.


      J

      Comment

      • Hypnotik
        New Member
        • Jun 2007
        • 87

        #4
        Problem is fixed, i started from scratch and had it done in about 10 minutes.

        Thanks to those who took a look.

        J

        Comment

        Working...