trying to multiply two matrices filled with random numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Barnwell
    New Member
    • Dec 2010
    • 2

    trying to multiply two matrices filled with random numbers

    Im trying to multiply a 5X5 matrix filled with random numbers with another matrix, also filled with random numbers. I'm still trying to fill the two matrices and then display them my code is as follows

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    
    void printArray( const int a[5][],int row, int column); /* function prototype */
    void initializeArray(  int a[5][],int row, int column); /* function prototype */
    int main()
    {
         int rowA=5; /* counter */
         int columnA=5;
         
         int rowX = 5;
         int columnX = 0;
         
         
         printf("\nEnter the number of columns for the dense vector X MAX 100: ");
         scanf("%d",&columnX);
         
         while( !(columnX >=1) && !(columnX%2 == 0))
         {
                printf("\nIncorrect  input");
                printf("\nPlease enter an integer value greater than or equal to one (1)");
                printf("\nEnter the number of columns for the dense vector X: ");
                scanf("%d",&columnX);
         }
      
      /*initialise the dense vectors to 0 at the beginning- to get rid of junk data */
      
       int arrayA[5][5]={0}; /* A is going to be a dense array of 25 integers */
       int vectorX[5][100]={0}; /* X is going to be a dense array having at most 500 numbers*/
       
       /*seed the random number generator with the system time*/
       
       srand( time( NULL ) );
       
       /*initialise elements of each dense vector*/
       initializeArray(arrayA[5][5],rowA,columnA);
       initializeArray(&vectorX[5][],rowX,columnX);
       
       
       
       /*display results */
       printf( "Values in the arrayA by row are:\n" );
       printArray( arrayA[5][],rowA, columnA);
        
       printf( "Values in vectorX by row are:\n" );
       printArray(vectorX[5][],rowX, columnX);
       
      getch();
      return 0;  
    }
    void initializeArray(int a[5][],int row, int column)
    {
     int i=0; /* row counter */
       int j=0; /* column counter */    
         
      /* loop through rows */
       for ( i = 0; i < 5; i++ ) {
    
          /* and then input column values */
          for ( j = 0; j < column; j++ ) {
             a[ i][j] = 1 + ( rand() % 6 );
          } /* end inner for */   
       } /*end outer for */    
    }
    void printArray( const int a[5][],int row, int column )
    {
       int i=0; /* row counter */
       int j=0; /* column counter */
    
       /* loop through rows */
       for ( i = 0; i < 5; i++ ) {
    
          /* output column values */
          for ( j = 0; j < column; j++ ) {
             printf( "%d ", a[ i ][ j ] );
          } /* end inner for */
    
          printf( "\n" ); /* start new line of output */
       } /* end outer for */
    } /* end function printArray */
    Please help me.
    I've also attached my code as a text file
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    You haven't told us what your problem is. In what way does your program disappoint you?

    A lot of problems can be avoided if you read Arrays Revealed.

    Comment

    • David Barnwell
      New Member
      • Dec 2010
      • 2

      #3
      It doesnt compile, i'm trying to gemnerate 2 arrays, 1 5X5 and one that has 5 rows and at least 1 column.

      Compiler: Default compiler
      Executing gcc.exe...
      gcc.exe "C:\Users\Admin istrator\Docume nts\c-programs\parall el1_1.c" -o "C:\Users\Admin istrator\Docume nts\c-programs\parall el1_1.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
      C:\Users\Admini strator\Documen ts\c-programs\parall el1_1.c: In function `main':

      C:\Users\Admini strator\Documen ts\c-programs\parall el1_1.c:45: warning: passing arg 1 of `printArray' from incompatible pointer type
      C:\Users\Admini strator\Documen ts\c-programs\parall el1_1.c:48: warning: passing arg 1 of `printArray' from incompatible pointer type

      C:\Users\Admini strator\Documen ts\c-programs\parall el1_1.c: In function `initializeArra y':
      C:\Users\Admini strator\Documen ts\c-programs\parall el1_1.c:63: error: invalid use of array with unspecified bounds

      C:\Users\Admini strator\Documen ts\c-programs\parall el1_1.c: In function `printArray':
      C:\Users\Admini strator\Documen ts\c-programs\parall el1_1.c:77: error: invalid use of array with unspecified bounds

      Execution terminated
      That's what I get when i try to compile, after I get that to compile i'm going to have to multiply those two matrices. It's the pointer and array interaction that's killing me. I always have problems with c pointers. Thanks for any help you can give. I hope I've been a little more clear.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        All of these compiler errors refer to improper technique for passing arrays to and from the functions. Take a close look at the article (Arrays Revealed) that I referred you to in my earlier post. This article explains the matter better than I would.

        Comment

        Working...