Thread: [C++] Matrix and vector product

  1. #1
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15

    [C++] Matrix and vector product

    Hello, again, guys!
    I have another problem... I'm trying to compute the product of a matrix and a vector.
    I could ask for the values of the matrix/vector but I wasn't asked to do so... It's easier to set the values from the beginning
    The problem I have is that I get 2 errors when I try to run the program, they occur on lines 20 and 23. MVS says "printf indentifier not found" on both lines... any help ? Thanks!

    LE: Ohh wait... C shouldn't be a matrix at the end ? Or am I too tired today? Because I set "int C[3]"...

    Code:
    #include <cstdlib>
    #include <math.h>
    #include <conio.h>
    
    
    int main()
    {
        int A[3][3] = {{2, 4, 3}, {1, 5, 6}, {7, 3, 7}};
        int B[3] = {2, 5, 6};
        int C[3];
        int i, j;
    
    
        for ( i = 0; i < 3; i++)
        {
            for ( j = 0; j < 3; j++)
            {
                C[i] += A[i][j] * B[j];
            }
        }
    
    
        printf ("The product of the matrix A and vector B is: ");
        for ( i = 0; i < 3; i++ )
        {
            printf ("%d", C[i]);
        }
        _getch();
    }
    Last edited by Cazan Adelin; 04-08-2015 at 08:59 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You posted what appears to be C code, in the C forum, but your title says "C++" and you're using a C++ header.

    If you're programming in C, then you need to include "stdio.h" to use "printf()".

    Your array "C" is uninitialized, so when you do "C[i] += ...", you're adding the results to the garbage values in "C".

    A quick way to initialize the array to zero:

    Code:
    int C[3] = { 0 };

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    Perhaps you should include the necessary include file? Why do you have C++ include files in what appears to be a C program?

    Jim

  4. #4
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15
    Ohh, my bad! I just realised I am in C programming section, really sorry and thanks for the tip
    I'm learning C++ (for the moment) at faculty so...

    Thanks for your answers! I'll try right away!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    *moved to C++ programming*

    Well, if you are learning C++, then the use of printf, though possible, is rather strange. Also, you took care to use the correct C++ header <cstdlib>, but then used the header inherited from C, <math.h> instead of the C++ header <cmath>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Product
    By Cazan Adelin in forum C Programming
    Replies: 6
    Last Post: 04-08-2015, 08:31 AM
  2. Matrix product using macro definition
    By ElNoob in forum C Programming
    Replies: 5
    Last Post: 12-08-2012, 09:44 PM
  3. Matrix product
    By Cotizo in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2008, 10:10 AM
  4. matrix using std::vector
    By alyeska in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2008, 12:44 AM
  5. vector/matrix
    By tony1 in forum C++ Programming
    Replies: 0
    Last Post: 09-03-2001, 05:27 AM