Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 37

Thread: Program to do matrices

  1. #11
    Join Date
    Sep 2003
    Posts
    603
    Rep Power
    0

    Default

    Quote Originally Posted by akoo
    at this moment im having problems writing the code for the multiplication section does anyone care to help???? also i dont know if anyone tried to run the program but the addition, subtraction and scalar fucntions do not work so plz help me out!
    Does this have to be done in C? if would be a little easier if it was done in C++

    here is a little something I whipped up in C++, substitute you C input output operators..

    Code:
    // matrixMulti.cpp : Defines the entry point for the console application.
    // say thanks to uncle CGPGroup
    
    #include <stdafx.h>
    #include <iostream>
    using namespace std;
    
    const int _M1ROWS = 2;
    const int _M1COLS = 3;
    const int _M2ROWS = 3;
    const int _M2COLS = 2;
    
    
    int main()
    {
    	//load the first Matrix
    	int Matrix1[_M1ROWS][_M1COLS] = {1,2,3,
    									 4,5,6};
    
    	//load the second matrix
    	int Matrix2[_M2ROWS][_M2COLS] = {1,2,
    									 3,4,
    									 5,6};
    
    	//Declare the resultant Matrix
    	//remember the result matris is always the 
    	//number of rows in the first X the number of cols in the second
    	int rMatrix [_M1ROWS][_M2COLS];
    
    	//declare our loop controls
    	int rowM1, rowColIndx, colM2;
    
    	//loop through all the rows in matrix 1
    	for(rowM1=0;rowM1<_M1ROWS;rowM1++)
    	{
    		//loop through all the cols in matrix 2
    		for(colM2=0;colM2<_M2COLS;colM2++)
    		{
    			//loop through all items in the row to col match and multiply
    			int calResult = 0;
    			for(rowColIndx=0;rowColIndx<_M1COLS;rowColIndx++)
    			{
    				calResult = calResult +(Matrix1[rowM1][rowColIndx] * Matrix2[rowColIndx][colM2]);
    			}
    			rMatrix[rowM1][colM2]=calResult;
    		}
    	}
    //I am just printing the result matrix here...
    cout<<rMatrix[0][0]<<",";
    cout<<rMatrix[0][1]<<endl;
    cout<<rMatrix[1][0]<<",";
    cout<<rMatrix[1][1];
    int x;
    cin>>x;
    }
    easiparcel.com Shop online and ship to Jamaica

  2. #12
    Join Date
    Sep 2003
    Posts
    603
    Rep Power
    0

    Default

    I know I am being an ***** but I have to brag the above code took me about 30 mins to write and I have not done C++ in about 4 years maybe even 4.5.


    damn I am good......
    easiparcel.com Shop online and ship to Jamaica

  3. #13
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    @CGPGroup, I sure want to take you on in a programming challenge.

    I either get beat silly and get better or
    beat u and see how good I am.

    Do not have the time now, maybe one day in something challeging within the design phase as well as coding. Completely original problem.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #14
    Join Date
    Dec 2004
    Posts
    476
    Rep Power
    0

    Default

    wow could you plz post what u have?
    WHY PAY WHEN YOU CAN GET IT FREE!!!!!

  5. #15
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    look up at CGPGroup?! That's for the multiply
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  6. #16
    Join Date
    Dec 2004
    Posts
    476
    Rep Power
    0

    Default

    plz help i know nothing in C++ can someone covert that to C plz
    WHY PAY WHEN YOU CAN GET IT FREE!!!!!

  7. #17
    Join Date
    Sep 2003
    Posts
    603
    Rep Power
    0

    Default

    Quote Originally Posted by crosswire
    @CGPGroup, I sure want to take you on in a programming challenge.

    I either get beat silly and get better or
    beat u and see how good I am.

    Do not have the time now, maybe one day in something challeging within the design phase as well as coding. Completely original problem.
    are you sure you want to do that?
    I am pretty experienced, Worked for the IAEA on software to conduct Nuclear inspections. currently working for the US Govt to put in place a Requirements Management System for a DOE lab. I also did a programming competition sponsored by IBM (my University didn't win anything though).

    if it is in C++ you would probably beat me though... I havent done it since 2002. This matrix multiplication makes me want to jump back into C++ though. It was very refreshing....

    Dont listen to me I am an a$s..
    easiparcel.com Shop online and ship to Jamaica

  8. #18
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    are you sure you want to do that?
    I am pretty experienced, Worked for the IAEA on software to conduct Nuclear inspections. currently working for the US Govt to put in place a Requirements Management System for a DOE lab. I also did a programming competition sponsored by IBM (my University didn't win anything though).
    Alright boss.

    Any language will suffice, except designing your own language. Mi no so skilled. Mi wi train fi you then

    PS nuclear is so last century, it has reached its pinnacle
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #19
    Join Date
    Sep 2003
    Posts
    603
    Rep Power
    0

    Default

    To Convert the Code to C is pretty simple, not much to do actually.

    C++
    Code:
    #include <stdafx.h> // dont know why I need this but .Net 2003 compiler complaining without it
    #include <iostream>
    using namespace std;
    
    const int _M1ROWS = 2;
    const int _M1COLS = 3;
    const int _M2ROWS = 3;
    const int _M2COLS = 2;
    
    
    int main()
    {
    	//load the first Matrix
    	int Matrix1[_M1ROWS][_M1COLS] = {1,2,3,
    									 4,5,6};
    
    	//load the second matrix
    	int Matrix2[_M2ROWS][_M2COLS] = {1,2,
    									 3,4,
    									 5,6};
    
    	//Declare the resultant Matrix
    	//remember the result matris is always the 
    	//number of rows in the first X the number of cols in the second
    	int rMatrix [_M1ROWS][_M2COLS];
    
    	//declare our loop controls
    	int rowM1, rowColIndx, colM2;
    
    	//loop through all the rows in matrix 1
    	for(rowM1=0;rowM1<_M1ROWS;rowM1++)
    	{
    		//loop through all the cols in matrix 2
    		for(colM2=0;colM2<_M2COLS;colM2++)
    		{
    			//loop through all items in the row to col match and multiply
    			int calResult = 0;
    			for(rowColIndx=0;rowColIndx<_M1COLS;rowColIndx++)
    			{
    				calResult = calResult +(Matrix1[rowM1][rowColIndx] * Matrix2[rowColIndx][colM2]);
    			}
    			rMatrix[rowM1][colM2]=calResult;
    		}
    	}
    //I am just printing the result matrix here...
    cout<<rMatrix[0][0]<<",";
    cout<<rMatrix[0][1]<<endl;
    cout<<rMatrix[1][0]<<",";
    cout<<rMatrix[1][1];
    
    int x;
    cin>>x; // this does nothing just pauses to see results
    }

    C Code
    Code:
    #include <iostream.h>
    
    const int _M1ROWS = 2;
    const int _M1COLS = 3;
    const int _M2ROWS = 3;
    const int _M2COLS = 2;
    
    int main()
    {
    	//load the first Matrix
    	int Matrix1[_M1ROWS][_M1COLS] = {1,2,3,
    					 4,5,6};
    
    	//load the second matrix
    	int Matrix2[_M2ROWS][_M2COLS] = {1,2,
    					 3,4,
    					 5,6};
    
    	//Declare the resultant Matrix
    	//remember the result matris is always the 
    	//number of rows in the first X the number of cols in the second
    	int rMatrix [_M1ROWS][_M2COLS];
    
    	//declare our loop controls
    	int rowM1, rowColIndx, colM2;
    
    	//loop through all the rows in matrix 1
    	for(rowM1=0;rowM1<_M1ROWS;rowM1++)
    	{
    		//loop through all the cols in matrix 2
    		for(colM2=0;colM2<_M2COLS;colM2++)
    		{
    			//loop through all items in the row to col match and multiply
    			int calResult = 0;
    			for(rowColIndx=0;rowColIndx<_M1COLS;rowColIndx++)
    			{
    				calResult = calResult +(Matrix1[rowM1][rowColIndx] * Matrix2[rowColIndx][colM2]);
    			}
    			rMatrix[rowM1][colM2]=calResult;
    		}
    	}
    //I am just printing the result matrix here...
    printf("%d,",rMatrix[0][0]);
    printf("%d\n",rMatrix[0][1]);
    printf("%d,",rMatrix[1][0]);
    printf("%d",rMatrix[1][1]);
    
    int x;
    scanf("%d",x);// this does nothing just pauses to see results
    }
    No difference really
    (1)
    #include <iostream>
    using namespace std;

    changes to

    #include <iostream.h>

    (2)
    removed #include <stdafx.h> because it is not a part of C (I think)

    (3)
    uses printf and scanf instead of cin>> and cout<<
    easiparcel.com Shop online and ship to Jamaica

  10. #20
    Join Date
    Sep 2003
    Posts
    603
    Rep Power
    0

    Default

    read the comments, I commented almost every single line. I compiled and executed the C++ and it works, I didnt for the C code.. good luck and cheers
    easiparcel.com Shop online and ship to Jamaica

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •