C++ Matrix Multiplication

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bpurificacion
    New Member
    • Mar 2008
    • 9

    C++ Matrix Multiplication

    Hey can anybody help me find a way to input values from the keyboard into a 3 X 3 matrix. I'm having difficulty with even the simplist task.

    here's what i have so far.

    #include <iostream>
    #include <fstream>

    using namespace std;

    const int SIZE = 3;

    int row;
    int col;
    int PBUF[3][3];
    int QBUF[3][3];
    int RBUF[3][3];


    int main()
    {
    int choice;

    do
    {
    cout << endl
    << "Choose 1 to input two arrays from the keyboard.\n"
    << "Choose 2 to input two arrays from a file.\n"
    << "Choose 3 to display the input file data, buffer data, and output file data.\n"
    << "Choose 4 to write input file data, buffer data, and output file' on a file.\n"
    << "Choose 5 to exit the program";
    cin >> choice;

    switch (choice)
    {
    case 1:
    {

    for( col = 0; col < SIZE ; col ++ )
    for( row = 0; row < SIZE; row ++ )
    cin >> PBUF[col][row];
    }
    {
    for( col = 0; col < SIZE ; col ++ )
    for( row = 0; row < SIZE; row ++ )
    cin >> QBUF[col][row];
    }
    cout << endl;
    break;

    case 2:

    break;

    case 3:

    break;

    case 4:

    break;

    case 5:
    cout << "End of program.\n";
    break;
    default:
    cout << "Not a valid choice.\n"
    << "Choose again.\n";
    }
    } while (choice !=5);

    return 0;
    }
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    I don't know if your compiler might be different, but on my compiler, the only problem is that SIZE is already defined by some built-in library or other. I renamed SIZE to some other name and your program worked perfectly. Hope this helps.

    Comment

    • bpurificacion
      New Member
      • Mar 2008
      • 9

      #3
      i keep getting 2 errors its weird. im not sure if its my computer or my compiler. im using visual studio 2008 and i get these 2 errors

      1. MSVCRTD.lib(crt exew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStar tup
      2. fatal error LNK1120: 1 unresolved externals

      Anybody have any suggestions?

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        Builds find in VS 2005, but it sounds like you didn't start an empty project. Create a new project, select win32 console app. Then click on application settings and select the check box for empty project. Create a .cpp file and paste your code into there and it should work fine.

        Comment

        • bpurificacion
          New Member
          • Mar 2008
          • 9

          #5
          Thanks it actually does work now!

          ok sorry for being such a newbie. But i also have to input two matrices from a file. do i need to call the file as in

          char *inputfnameP = "inputPBUF.txt" ;
          ifstream in_data;

          in_data.open(in putfnameP);
          if (in_data.fail() )
          cout << "Unable to open 'inputPBUFF' for input." << endl;
          }
          {
          char *inputfnameQ = "inputQBUF.txt" ;
          ifstream in_data;

          in_data.open(in putfnameQ);
          if (in_data.fail() )
          cout << "Unable to open 'inputQBUF' for input." << endl;
          }

          or am i doing this completely wrong. I also don't understand where the file must be located at. does it have to be in a specific folder or anything?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            If you opne the file with a name like "input.txt" , the file has to be in the present working directory, which for Visual Studio.NET is the project folder (unless its .NET 2003 which has a bug here). Unless you have changed this folder in your project properties. Otherwise, you specify the complete path: "C:\\MyStuff\\T heDataFiles\\in put.txt".

            Comment

            • bpurificacion
              New Member
              • Mar 2008
              • 9

              #7
              Originally posted by weaknessforcats
              If you opne the file with a name like "input.txt" , the file has to be in the present working directory, which for Visual Studio.NET is the project folder (unless its .NET 2003 which has a bug here). Unless you have changed this folder in your project properties. Otherwise, you specify the complete path: "C:\\MyStuff\\T heDataFiles\\in put.txt".
              I am using Visual Studio 2008.. I still don't understand how this all works.. Sorry! :(

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                You've got to locate the folder where Visual Studio stores your project and make sure the file is in that folder. There's probably also a new file dialog you could use (just make an empty file, rather than one for a specific language, and save it as a .txt or whatever), if you can't find the folder.

                Comment

                • bpurificacion
                  New Member
                  • Mar 2008
                  • 9

                  #9
                  I was wondering how i would be able to multiply the values of the 2 matricies. The values were loaded into a buffer like PBUFF[3][3]. I was thinking about doing a for loop possibly, but i don't know how to multiply the first values of each array and then the second values.

                  for (row=0; row < 3; row ++)
                  for (col=0; col < 3; col ++)

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    You'll need to write a function to do that, since C++ doesn't have a builtin function for it. This is a great tutorial on how (multidimension al) arrays work. Even if you think you understand them, as you read this, you will learn things.

                    Comment

                    Working...