-4

If you already have a set 4x4 matrix. Ex.

Matrix A = [1 2 3 4;
            5 6 7 8;`
            9 10 11 12;
            13 14 15 16]

Matrix B = [1, 2, 3]

How would you convert Matrix A into C coding? Also what would there positions be in code? For position I mean: if I'm trying to multiply the first row into matrix B, can I do this?

A[1][0]*B[0]+A[1][1]*B[1]+A[1][2]*B[2]

Outline code:

main(){

    int matrixA[4][4] = [{"1","2","3","4"};
                        {"5","6","7","8"};
                        {"9","10","11","12"};
                        {"13","14","15","16"}];
    printf(matrix A);
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Bob
  • 53
  • 3
  • 10
  • For position, if i would be able to get the position of the matrix and multiply it by another matrix? Example A[1][1] and say pretend B[0]. would i be able to code A[1][1]*B[0]? – Bob Sep 11 '16 at 16:13
  • Bob..what is your question? – user2736738 Sep 11 '16 at 16:14
  • The matrix above, is matrix A, I don't know how to implement that example into code, and you know when we add, multiply, divide, matrices, there are in different positions (Like a1, a2, a3, etc.). My question is, are those positions present in coding C? If so, what is it? So in Matrix A above if i wanted to get 4, would I have to code matrixA[1][3] in order to get the number 4? And if i have another matrix say B, and i want to multiply it by four in matrix A. Can I do B[0]*A[1][3]? – Bob Sep 11 '16 at 16:18
  • In mathematics, you can't multiply a 4x4 matrix with a 1x3 vector/matrix; you need a 1x4 or 4x1 vector. If you want to multiply the first three elements of a row from a 1x4 or 4x1 vector a 1x3 vector, then you may. You can write out the multiplication or you can use a loop; both work, but the loop adapts to matrices or vectors of different sizes. You can't initialize a matrix of `int` values with strings; you need to use plain numbers. Drop all the quotes. You can't use a single simple `printf()` function call to print an entire matrix. – Jonathan Leffler Sep 11 '16 at 17:05
  • You should also be coding in at least C99 and preferably C11 where `int main()` or — better — `int main(void)` is the appropriate start to the main program when you ignore the command line arguments. See also [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/) – Jonathan Leffler Sep 11 '16 at 17:08

4 Answers4

1

First of all you cannot multiply a 1×3 matrix with 4×4 matrix. You should have matrices like m×n and n×p to get them multiplied (the result will be an m×p matrix).

Also for having a 4×4 matrix in C you should implement it like this:

int main()
{
    int mat[4][4];
    for(int i=0;i<=3;i++)
    {
        for(int j=0;j<=3;j++)
        {
            scanf("%d", &mat[i][j]);
        }
    }

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
arif abbas
  • 341
  • 2
  • 6
  • You can refer to multidimensional array concept in c for getting an idea about the indexing of elements in a matrix. That may help ya!! – arif abbas Sep 11 '16 at 16:39
  • Note that it is perfectly legitimate to initialize a 4×4 matrix without having to resort to using `scanf()` to read the values (though you will often want to read values into a matrix). When you use `scanf()`, you should check that it succeeds. – Jonathan Leffler Sep 11 '16 at 17:11
0

As far i can understand, you want to make a program to execute mathematical fractions related to matrices. Example in linear algebra. Matrix sizes are not checked, but you get the idea. In division you have to make the calculation of Array2^-1 to find it. Hope i helped. After you find it, multiply the result ,to Array1. In division however, things are more complicated. You need to approach your coding in different ways, depending to the array(matrix) dimensions you have. Exceptions must be included and need to read a bit of theory on how to divide those matrices. See https://en.wikipedia.org/wiki/Division_(mathematics) for more details.

#include <stdio.h>
#include <math.h>
int main(){
//counters
int i=0,j=0,k=0,sum=0;
//first array
int Array1[4][4] ={
        {87,96,70,22},
        {18,65,77,78},
        {76,72,84,65},
        {87,93,73,77}};
//second array
int Array2[4][4]={
        {14,45,66,88},
        {45,32,97,44},
        {34,64,23,66},
        {39,98,55,32}};
//result array.
int ResultArray[4][4];
// Add
for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j) {
        ResultArray[i][j]= Array1[i][j]+Array2[i][j];
    }
}
//result
printf("\tAdd Array1 and Array2\n");
for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j) {
        printf(" %10d \t",ResultArray[i][j]);
    }
    printf("\n");
}
//subtract
for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j) {
        ResultArray[i][j]= Array1[i][j]-Array2[i][j];
    }
}
//result
printf("\tSubtract Array1 and Array2\n");
for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j) {
        printf(" %10d \t",ResultArray[i][j]);
    }
    printf("\n");
}
//multiply
for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j) {
        for (k = 0; k < 4; ++k) {
            sum=sum+Array1[i][k]*Array2[k][j];
        }
        ResultArray[i][j]=sum;
        sum=0;
    }
}
//result
printf("\tMultiplication Array1 and Array2\n");
for (i = 0; i    < 4; ++i) {
    for (j = 0; j < 4; ++j) {
        printf(" %10d \t",ResultArray[i][j]);
    }
    printf("\n");
}
return 0;
}
Paulkokos
  • 36
  • 5
-1

you can write like this:

int matrixA[4][4] = {
    { 1,  2,  3,  4},//matrixA[0][0] is 1
    { 5,  6,  7,  8},//matrixA[1][1] is 6
    { 9, 10, 11, 12},
    {13, 14, 15, 16}
};
int matrixB[] = { 1, 2, 3};
for(int r = 0; r < 4; ++r){
    for(int c = 0; c < 4; ++c){
        printf("%3d", matrixA[r][c]);
    }
    printf("\n");
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
-1

Let me help you a bit, but you in return help me understand the question. :)

Representation


Ok Matrices are represented using 2-d array. Like

int a[][]= { {1,2,3,4}, {5,6,7,8},{9,10,11,12},{13,14,15,16}};

Matrix is ready. :)

If you want to create it on the fly..malloc() will be your friend. Here I meant to say that you have to dynamically allocate the array. For example:

int **a;
a= malloc(sizeof(int*)*m);
for(int i=0;i<m;i++)
  a[i]=malloc(sizeof(int)*n);

Forming mxn matrix where m and n are got as input maybe.

How to multiply 2 matrices [ ofcourse compatible]?

Here in your case A[4x4] B[1x4] so p=4,m=1,q=4.

for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • whoa!! why is that? Feel free to criticize. Thanks – user2736738 Sep 11 '16 at 16:36
  • 1
    Not my DV, but — Have you tried compiling the first array? It won't; you must supply the second dimension. In your multiplication, it would be better to set `int sum = 0;` inside the `for (d …)` loop at the top; it can be defined there too. I'd use `for (int d = 0; …` too. The "if you want to create it on the fly" comment is … incomplete? At least, it invites "well, how do you do it with `malloc()`" as a follow-up. However, your basic diagnosis — the definition of the matrix in the question is wrong — is correct, even if you've not clearly explained why. – Jonathan Leffler Sep 11 '16 at 17:01
  • hey Jonathan...thanks for the reply. I get my wrongs.. Let me work on it. I will be around it when I have some time. Thanks really. I will ask you something later. Thanks again. @JonathanLeffler – user2736738 Sep 11 '16 at 17:29