# multiplying a 2D array by a 1D array

• 06-11-2009
youngvito
multiplying a 2D array by a 1D array
my second function does not seem to work. It is supposed to multiply each row of a 2D array by a 1D array. Im reading in a 2D and a 1D array from a file using I/O redirection <data1.txt > out1.txt. Can somebody point out the problem im doing. It does not print numbers.
Code:

```#include<stdio.h> #define SIZE 100 void calcSumAs(double a[][SIZE], double y[], int m, int n); void calcSumAXs(double a[][SIZE], double y[], double rs[], int m, int n); int main(void) {         int m;         int n;         int i;         int j;         double ar[SIZE];         double prod[SIZE];         double mx[SIZE][SIZE];         double sum[SIZE];         double Y;         scanf("%d%d", &m, &n);                 for (i = 0; i < m; i++)         {                 for (j = 0; j < n; j++)                         scanf("%lf", &mx[i][j]);         }                 for (i = 0; i < m; i++)                 scanf("%lf", &ar[i]);                 calcSumAs(mx, sum, m, n);         calcSumAXs(mx, ar, prod, m, n);                 for (i = 0; i < m; i++)                 printf("%lf ", prod[i]);                                return 0; } void calcSumAs(double a[][SIZE], double y[], int m, int n) {         int i, j;         for (i = 0; i < m; i++)                 for (y[i] = 0, j = 0; j < n; j++)                         y[i] += a[i][j];         return; } void calcSumAXs(double a[][SIZE], double y[], double rs[], int m, int n) {         int i, j;                 for(i = 0; i < m; i++)                 for(rs[i] = 0, j = 0; j < n; j++)                         rs[i] += a[i][j] * y[j];         return; }```
• 06-11-2009
quzah
Code:

```for each row     for each column         dosomethingwith: onedeearray[ col ] * twodeearray[ row ][ col ]```
There's the general idea.

Are you sure what you're really doing is what you mean to be doing?

Quzah.
• 06-11-2009
youngvito
yes, i know everything is correct but im not getting the numbers printed, the problem is in my second function and i tried it, can you give me some more advice please.
• 06-11-2009
quzah
When you say you're not getting the numbers printed, what exactly does that mean? You input numbers and ... ? Your program ends early? It prints stuff, but it's not what you expect? What exactly?

Quzah.
• 06-11-2009
youngvito
the last loop in the main function is supposed to print the sums of the products of a one D array and the 2 D array. It prints the works of the second function. What is printed is this 1.#QNAN0 1.#QNAN0 1.#QNAN0 -1.#QNAN0 1.#QNAN0 1.#QNAN0 , instead its supposed to be five numbers.
• 06-11-2009
youngvito
sorry i mean 6 numbers
• 06-11-2009
youngvito
i know the loop executes six times which is correct its just not printing out the sums of the products of the 1 d array and 2d array
• 06-11-2009
matsp
The #QNAN is a "not-a-number indication". The most obvious cause for this is that the number is "an illegal bitcombination", and that's usually caused by not initializing the floating point value correctly (such as not initializing it at all).

Check your variables that you calculate with and make sure everything is set an appropriate initial value.

--
Mats
• 06-12-2009
youngvito
i checked my code over and over again and i cant seem to find a problem. Please take a look at my program and see if i made a mistake anywhere.
• 06-12-2009
tabstop
I can't get any NaN output. What input are you using?
• 06-12-2009
youngvito
6 11
21 22 23 24 25 26 27 28 29 30 31
-21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31
0 1 0 1 0 1 0 1 0 1 0
1 2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21 22
11 10 9 8 7 6 5 4 3 2 1
0 1 -1 0 1 -1 0 1 -1 0 1

this is the imput from a file, the first function is supposed to calculate the sum of each row which works fine. The second function is supposed to multiply each row by the last row of the input
• 06-12-2009
tabstop
And the program returns -2, 2, 0, -2, -2, 2. Is that not what you want?
• 06-12-2009
youngvito
the number for the first row is supposed to be 28, i havent calculated the other ones
• 06-12-2009
youngvito
it doesnt return any numbers it all it returns 1.#QNAN0 1.#QNAN0 1.#QNAN0 1.#QNAN0 1.#QNAN0 1.#QNAN0. Instead its supposed to return 6 floating point numbers, the first one being 28
• 06-12-2009
quzah
Stop reading it from a file for starters, and instead stick it in a fixed array and see if it gives you what you expect:
Code:

```#include<stdio.h> void foo( int bar[], int baz[][ 5 ], size_t r, size_t c ) {     size_t col, row;     for( row = 0; row < r; row++ )     {         for( col = 0; col < c; col++ )         {             bar[ col ] += baz[ row ][ col ] * 5;         }     } } int main( void ) {     int a1[ 5 ] = { 0 },         b1[ 3 ][ 5 ] =     {         { 4, 5, 6, 7, 8 },         { 1, 2, 9, 0, 3 },         { 5, 10, 15, 20, 25 },     };     size_t x;     foo( a1, b1, 3, 5 );         for( x = 0; x < 5; x++ )     {         printf( "a1[ %d ] is %d. Expected: %d.\n",         x, a1[ x ], (5 * ( b1[ 0 ][ x ] + b1[ 1 ][ x ] + b1[ 2 ][ x ]) )         );     }         return 0; }```
Here's a helpful tip also: Never post code you've written when way too tired, the night before. (See above!)

Quzah.