Thread: multiplying a 2D array by a 1D array

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    40

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    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.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    sorry i mean 6 numbers

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    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

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    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.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I can't get any NaN output. What input are you using?

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    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

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And the program returns -2, 2, 0, -2, -2, 2. Is that not what you want?

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    the number for the first row is supposed to be 28, i havent calculated the other ones

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    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

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 06-12-2009 at 03:53 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1D and 2D Arrays
    By Rajin in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 06:23 PM
  2. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM