adjacency matrix

This is a discussion on adjacency matrix within the C++ Programming forums, part of the General Programming Boards category; Any suggestions as to why this program only reads in the data and doesn't out out it. I have looked ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    17

    adjacency matrix

    Any suggestions as to why this program only reads in the data and doesn't out out it. I have looked at it till I can't see and I think that is my problem. Any suggestions as to where to look or how to fix would be awesome!!

    I think this program is self explained.
    Read in adjacency matrix, call up a power of it, print it out!!

    Code:
    //  Adjacency Matrix
    #include <iostream>
    using namespace std;
    
    void matin ( int G[][5], int P[][5], int r, int c ){
    	int i,j;
    	cout << "Enter an edge as 1 and no edge as 0 " << endl
    				 << "Please enter matrix by row. " << endl;
    	for ( i=0; i< r; i++ ){
    			
    		for ( j=0; j<c; j++ ){		
    			cin >> G[i][j];
    			 G[i][j] =  P[i][j] ;
    		};
    	};
    }
    
    void matout (int P[][5], int r, int c){
    	cout << endl;
    	int i,j;
    	for ( i=0; i<r; i++ ){
    		for ( j=0; j<c; j++ ){
    			cout << P[i][j] << "  ";
    		};
    		cout << endl;
    	};
    }
    
    void matmult ( int A[][5], int P[][5], int G[][5], int r, int c, int n){
    	int i,j,k;
    	for ( k=0; k<r; k++ ){
    			for( i=0; i<r; i++ ){
    				int sum =0;
    				for ( j=0; j<n; j++ ){					
    					sum = sum +  P[k][j]* A[j][i] ;	
    				G[k][i] = sum;	
    				};			
    			};
    	};matout (G,r,c);
    }
    
    int matpower ( int A[][5], int P[][5],int r, int c, int n ){
    	int G[5][5];
    	if ( n == 0 ){               //base case
    		cout << "Matrix to the 0 power has ended program " << endl;		
    		return 1;
    	}	
    	else if ( n > 0,--n ){	
    			matmult( A,P,G,r,c, n );
    			matpower ( A,G,r,c,n );//p to g
    	};
    return 1;
    }
    
    int main (){
    	int r, c;
    	cout << "This matrix program will handle any matrix up to 20 rows and 20 columns" << endl
    		 << "Please enter size of matrix rows and columns "  << endl;
    	cin >> r >> c;	
    	int P[5][5];
    	int A[5][5];
    	if (r!= 0 && c!= 0){
    	matin (A,P, r, c);
    	cout << endl;
    	matout(P, r,c);
    	int n;
    	cout << "Please enter the power multiple for matrix. " << endl
    		 <<  "(note: every power up to the one entered will be printed) ";
    	cin >> n;
    	cout  << endl<< "Matrix from power of 1 through " << n << " power are:"
    		 << endl;
    	matout (P, r, c);
    	matpower(P, A, r, c, n);
    	}
    	return 0;
    }
    /*
    Matrix from power of 1 through 3 power are:
    
    0  1  0  0  1
    0  0  1  0  0
    0  1  0  0  1
    0  0  1  0  0
    1  0  0  1  0
    
    1  0  1  1  0
    0  1  0  0  1
    1  0  1  1  0
    0  1  0  0  1
    0  1  1  0  1
    
    0  2  1  0  2
    1  0  1  1  0
    0  2  1  0  2
    1  0  1  1  0
    1  1  1  1  1
    Press any key to continue
    */

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Code:
       ...
       for ( j=0; j<c; j++ ){		
          cin >> G[i][j];
          G[i][j] =  P[i][j] ;
       };
       ...
    You are overwriting what you just input. You've been staring at the monitor for too long .

    gg

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    17
    I guess that is what I get for spending Spring Break at home =)
    So I am thinking with what little brain I have left that in my matin function I need some sort of loop that will keep advancing me through the matrix?? Doesn't the for loops advance me to the "next space" within the matrix??

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >Doesn't the for loops advance me to the "next space" within the matrix??

    yes, but you overwrite what you read in...

    Code:
       ...
       for ( j=0; j<c; j++ ){		
          cin >> G[i][j] ;
           G[i][j]  =  P[i][j] ; // should be P[i][j] = G[i][j] ?
       };
       ...
    Last edited by Perspective; 03-12-2003 at 08:09 PM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    17
    Ah ha!! You are right I must get away from this darn computer. Thanks a bunch!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Replies: 1
    Last Post: 11-06-2008, 10:50 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21