Thread: Matrix c program help!!!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    Matrix c program help!!!

    alright so i'm trying to make a matrix program that replaces, adds, multiplies and transposes matrices
    as of right now i'm having trouble printing it out after i run a function

    it goes something like this

    input rows:
    input columns:
    then enter integer for each matrix position:

    then it prints it out
    (example)
    1 2
    3 4

    then i run my replace function
    input rows:
    input columns:
    then the integers for each matrix position
    then it prints out
    1 2
    -435262 1
    when it should be
    4 3
    2 1

    please help!!
    also i didn't know how to copy the code from the server, so if someone could tell me how to do that it would prob help you see what my code looks like thanks.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Since you are getting an apparently garbage value, my guess is you are using a variable uninitialized or accessing an array out of bounds.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i didn't know how to copy the code from the server
    I suppose it depends on a little details you forgot to mention: what server, what client, how do you connect to the server etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    i'm using this unix server for my college class, i kno its accessed through a ssh and then a user name then url
    by the way i got the replace one to work, now it one to the addition, which is doing the same thing, shooting out crap values

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If you are running Linux at home, you can use sshfs, otherwise sftp.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    could u further explain

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Google is your friend.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    i dont think i can use those because they're all windows based, and im using a mac

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    alright heres my code
    Code:
    #include<stdio.h>
    
    
    void showmatrix(int matrix[][], int row, int column)
    {	
    	int i, j ;
    	int A[row][column];
    	printf("Current Matrix is : \n");
    	for(i=0; i<row; i++)
    	{
    		for(j =0; j<column; j++)
    		{ printf("%d\t", A[i][j]);
    		} printf("\n");
    	}
    	printf("\n");
    }
    
    void setMatrix(int matrix[][], int row, int column)
    {
    	int i, j;
    	int A[row][column];
    
    	
    	for(i=0; i<row; i++)
    	{
    		for(j =0; j<column; j++)
    		{
    			printf("please input element (%d, %d) of the matrix:", i, j); scanf("%d", &A[i][j]);
    		}
    	}
    }
    
    void replaceA(int matrix[][4],int row,int column)
    {
    	int i, j;
    	int A[row][column];
    
    	printf("Please enter the number of rows for matrix:"); scanf("%d", &row);
        	printf("Please enter the number of columns for matrix:"); scanf("%d", &column);
    
    	for(i=0; i<row; i++)
    	{
    		for(j =0; j<column; j++)
    		{
    			printf("please input element (%d, %d) of the matrix:", i, j); scanf("%d", &A[i][j]);
    		}
    	}
    }		
    int showmenu(int matrix[][], int row, int column)
    {
    	int choice = 0;
        printf("******MENU******\n");
    /*	showmatrix(matrix, row, column);*/
    	
    	printf("(1)	Set matrix A\n");
    	printf("(2)	Add a matrix B (matrix addition) \n");
    	printf("(3)	Multiply with a matrix B (matrix multiplication) \n");
    	printf("(4)	Transpose matrix A\n");
    	printf("(5)	Exit \n");
    	printf("Please give your choice: "); scanf("%d", &choice);
    
    	return choice;
    }
    
    void replaceB(int matrixB[][],int row, int column)
    {	int i, j;
    	int B[row][column];
    	
    	for(i=0; i<row; i++)
    	{ 
    		for(j =0; j<column; j++)
    		{	
    		printf("please input element (%d, %d) of the matrix:", i, j); scanf("%d", &B[i][j]);
    		}
    	}
    }
    
    void add(int matrixA[][], int matrixB[][], int matrixC[][], int row, int column)
    {	int i,j;
    	int A[row][column];
    	int B[row][column];
    	int C[row][column];
    
    	for(i=0; i<row; i++)
    	{
    		for(j =0; j<column; j++)
    		{ C[i][j] = A[i][j] +B[i][j];
    		  A[i][j] = C[i][j];	 }
    		
    	}
    }
    /*
    void add(int matrixA[][], int matrixB[][], int row, int column)
    {
    	int i, j;
    	int A[row][column];
    	int B[row][column];
    	int C[row][column];
    	int value;
    {
    	for(i=0; i<row; i++)
    	{
    		for(j =0; j<column; j++)
    		{
    			printf("please input element (%d, %d) of the matrix:", i, j); scanf("%d", &value);
    			B[i][j] = value;			
     		}
    	}
    }
    	for(i=0; i<row; i++)
    	{
    		for(j =0; j<column; j++)
    		{ C[i][j] = A[i][j] + B[i][j];
    		A[i][j] = C[i][j];		  
    		}
    	}	
    }*/
    /*
    void multiply(int matrixA[][4], int rowA, int columnA, int matrixB[][4], int rowB, int columnB)
    {
    	int i,j, k;
    	int A[i][j],B[i][k];
    	int column, row, rowA, columnA, rowB, columnB, value, multi;
    	
    	rowA = row;
    	columnA = column;
    	rowB = columnA;
    	printf("Please enter the number of columns for matrix A: ")
    	scanf("%d", &columnB);
    	for(i=0; i<rowB; i++)
    	{		 
    		for(k=0; k<columnB; k++)
    		{
    		printf("please input element (%d, %d) of the matrix:", i, j); scanf("%d", &value);
    		B[i][k] = value
    		}	
    	}
    	
    	for(i=0; i<rowA; i++)
    	{
    		for(j=0; j<columnB;j++)
    		{ multi = (A[i][j] * B[i][k]) + (A[i][j++] * B[i++][k];
    		 A[i][j] = multi;
    	
    
    
    
    
    
    
    }
    void transpose(int matrix[][4], int row, int column)
    {
    }
    
    */
    
    int main()
    {
    	int matrixA[4][4];   
    	int matrixB[4][4];
    	int matrixC[4][4];
        	int row=0, column =0;
    	int rowB=0, columnB = 0;
    	/* this variable is used to store the user's choice for the menu system */
    	int choice = -1;
    
    	printf("Please enter the number of rows for matrix:"); scanf("%d", &row);
        	printf("Please enter the number of columns for matrix:"); scanf("%d", &column);
        
    	
    	setMatrix(matrixA, row, column);
    	showmatrix(matrixA,row,column);	
    	
    	choice = showmenu(matrixA,row,column);
    
    	while(1)
    	{	
    	if(choice == 1)
    	{ replaceA(matrixA,row,column) ;
    	showmatrix(matrixA,row,column);	
    	}
    	
    	if(choice == 2)
    	{  replaceB(matrixB,row,column);
    	  add(matrixA, matrixB, matrixC,row, column);
    	showmatrix(matrixA,row,column);  }	
    	else if(choice ==5)
    	{ break;}
    
    	choice = showmenu(matrixA,row,column);
    
    	}
    
    	
    
    
    
    	
    	return 0;
    }
    i figured out the first problem
    now it does the same thing when i run the addition program
    i have yet to start on the multiplication and transpose

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kclew View Post
    i dont think i can use those because they're all windows based, and im using a mac
    There should be very little, if anything, that is windows related in a matrix calculation program.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Quote Originally Posted by matsp View Post
    There should be very little, if anything, that is windows related in a matrix calculation program.

    --
    Mats
    sorry bout that, but i figured out how to do it

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    void showmatrix(int matrix[][], int row, int column)
    this could not be compiled - you can leave out only one dimention
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    no i was able to compile it and it worked for the first function

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by kclew View Post
    no i was able to compile it and it worked for the first function
    Do you sometimes read the warning and error messages of the compiler?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    so my biggest problem right now is that it wont output the right numbers,
    i ran the add function and the output was
    4 -45685678
    2 -46893778

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program 2D matrix multiplication using malloc
    By college_kid in forum C Programming
    Replies: 5
    Last Post: 04-03-2009, 10:04 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. Replies: 1
    Last Post: 03-06-2006, 07:57 PM