By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,326 Members | 2,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,326 IT Pros & Developers. It's quick & easy.

trying to multiply two matrices filled with random numbers

P: 2
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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <time.h>
  5.  
  6. void printArray( const int a[5][],int row, int column); /* function prototype */
  7. void initializeArray(  int a[5][],int row, int column); /* function prototype */
  8. int main()
  9. {
  10.      int rowA=5; /* counter */
  11.      int columnA=5;
  12.  
  13.      int rowX = 5;
  14.      int columnX = 0;
  15.  
  16.  
  17.      printf("\nEnter the number of columns for the dense vector X MAX 100: ");
  18.      scanf("%d",&columnX);
  19.  
  20.      while( !(columnX >=1) && !(columnX%2 == 0))
  21.      {
  22.             printf("\nIncorrect  input");
  23.             printf("\nPlease enter an integer value greater than or equal to one (1)");
  24.             printf("\nEnter the number of columns for the dense vector X: ");
  25.             scanf("%d",&columnX);
  26.      }
  27.  
  28.   /*initialise the dense vectors to 0 at the beginning- to get rid of junk data */
  29.  
  30.    int arrayA[5][5]={0}; /* A is going to be a dense array of 25 integers */
  31.    int vectorX[5][100]={0}; /* X is going to be a dense array having at most 500 numbers*/
  32.  
  33.    /*seed the random number generator with the system time*/
  34.  
  35.    srand( time( NULL ) );
  36.  
  37.    /*initialise elements of each dense vector*/
  38.    initializeArray(arrayA[5][5],rowA,columnA);
  39.    initializeArray(&vectorX[5][],rowX,columnX);
  40.  
  41.  
  42.  
  43.    /*display results */
  44.    printf( "Values in the arrayA by row are:\n" );
  45.    printArray( arrayA[5][],rowA, columnA);
  46.  
  47.    printf( "Values in vectorX by row are:\n" );
  48.    printArray(vectorX[5][],rowX, columnX);
  49.  
  50.   getch();
  51.   return 0;  
  52. }
  53. void initializeArray(int a[5][],int row, int column)
  54. {
  55.  int i=0; /* row counter */
  56.    int j=0; /* column counter */    
  57.  
  58.   /* loop through rows */
  59.    for ( i = 0; i < 5; i++ ) {
  60.  
  61.       /* and then input column values */
  62.       for ( j = 0; j < column; j++ ) {
  63.          a[ i][j] = 1 + ( rand() % 6 );
  64.       } /* end inner for */   
  65.    } /*end outer for */    
  66. }
  67. void printArray( const int a[5][],int row, int column )
  68. {
  69.    int i=0; /* row counter */
  70.    int j=0; /* column counter */
  71.  
  72.    /* loop through rows */
  73.    for ( i = 0; i < 5; i++ ) {
  74.  
  75.       /* output column values */
  76.       for ( j = 0; j < column; j++ ) {
  77.          printf( "%d ", a[ i ][ j ] );
  78.       } /* end inner for */
  79.  
  80.       printf( "\n" ); /* start new line of output */
  81.    } /* end outer for */
  82. } /* end function printArray */
  83.  
  84.  
Please help me.
I've also attached my code as a text file
Dec 19 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 2,398
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.
Dec 19 '10 #2

P: 2
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\Administrator\Documents\c-programs\parallel1_1.c" -o "C:\Users\Administrator\Documents\c-programs\parallel1_1.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Administrator\Documents\c-programs\parallel1_1.c: In function `main':

C:\Users\Administrator\Documents\c-programs\parallel1_1.c:45: warning: passing arg 1 of `printArray' from incompatible pointer type
C:\Users\Administrator\Documents\c-programs\parallel1_1.c:48: warning: passing arg 1 of `printArray' from incompatible pointer type

C:\Users\Administrator\Documents\c-programs\parallel1_1.c: In function `initializeArray':
C:\Users\Administrator\Documents\c-programs\parallel1_1.c:63: error: invalid use of array with unspecified bounds

C:\Users\Administrator\Documents\c-programs\parallel1_1.c: In function `printArray':
C:\Users\Administrator\Documents\c-programs\parallel1_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.
Dec 19 '10 #3

Expert 100+
P: 2,398
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.
Dec 19 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.