0

Here is the code for matrix multiplication of random numbers using rand() function. The output of the program gives very large values as matrix elements.

Why not any small random numbers not generated??

This is the output when N=3

Enter the value of N : 3

Final Matrix :

259448206-96933429-936226671  
-409898077185182340844598571  
-1916994436-653447116470937338   

Program:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
    time_t t;
    int **ptr1, **ptr2, **ptr3;
    int N, col1, row2, col2;
    srand ((unsigned) time (&t));
    int i, j, k;
    printf ("\nEnter the value of N : ");
    scanf ("%d", &N);
    ptr1 = (int **) malloc (sizeof (int *) * N);
    ptr2 = (int **) malloc (sizeof (int *) * N);
    ptr3 = (int **) malloc (sizeof (int *) * N);

    for (i = 0; i < N; i++)
        ptr1[i] = (int *) malloc (sizeof (int) * N);
    for (i = 0; i < N; i++)
        ptr2[i] = (int *) malloc (sizeof (int) * N);
    for (i = 0; i < N; i++)
        ptr3[i] = (int *) malloc (sizeof (int) * N);

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            ptr1[i][j] = rand ();
        }
    }

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            ptr2[i][j] = rand ();
        }
    }

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            ptr3[i][j] = 0;
            for (k = 0; k < N; k++)
                ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
        }
    }

    /* Printing the contents of third matrix. */

    printf ("\n\nFinal Matrix :");
    for (i = 0; i < N; i++) {
        printf ("\n\t");
        for (j = 0; j < N; j++)
            printf ("%4d", ptr3[i][j]);
    }

    printf ("\n");
    return (0);
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
47aravind
  • 13
  • 2
  • 8
  • 2
    Have you checked how big `RAND_MAX` is? – EOF Feb 22 '16 at 23:53
  • Use the modulo operator to get smaller values (e.g. rand()%100). While it is slightly non-uniformly distributed that way this should not be an issue here. – Ctx Feb 22 '16 at 23:55
  • `ptr3[i][j] + ptr1[i][k] * ptr2[k][j]` so readily overflows and `RAND_MAX` is likely large. Use some delimiter like spaces `printf(" %4d", ptr3[i][j]);` – chux - Reinstate Monica Feb 23 '16 at 00:07
  • Given no control on the random numbers generated, I'd expect only 0.00046% of`ptr3[i][j]` to be less than 10,000 and code is only showing 9 of them. Now if you had a square matrix of 470x4700, you might have a 50% change of seeing a value < 10,000. – chux - Reinstate Monica Feb 23 '16 at 00:11
  • 3
    Also, do *NOT* cast the return of `malloc`. It is totally unnecessary and can hide errors. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) for thorough explanation. (e.g. `ptr1[i] = malloc (sizeof *ptr1[i] * N);` is all you need. – David C. Rankin Feb 23 '16 at 00:11
  • I used rand()%100..and it solved the issue.. btw is there any way to set the value of RAND_MAX?? just curious. – 47aravind Feb 23 '16 at 00:13
  • To set the value of `RAND_MAX` would require a rebuild of portion of the STL like `rand()`, `srand()`. No easy way. Using `rand()%n` is a reasonable alternative for learner programs. – chux - Reinstate Monica Feb 23 '16 at 00:15
  • @DavidC.Rankin The casts are indeed useful here to double-check the (intended vs. actual) types by the compiler. Quite a bad example for an unnecessary cast of malloc – Ctx Feb 23 '16 at 00:22
  • To clarify on @chux comment: `RAND_MAX` is an implementation constant defined by your implementation of the standard library. As the name implies, it gives the max. value `rand` can yield. Getting larger values is problematic, but smaller values can be generated by various relatively simple algorithms, the most simple being modulus operation. Note this also has a bad distribution for divisors other than integer divisors of `RAND_MAX + 1`. – too honest for this site Feb 23 '16 at 00:25
  • @chux – _a square matrix of 470x4700_? What a remarkable square! ;-) – Armali Apr 15 '16 at 12:18

1 Answers1

0

Use the modulo operator to get smaller values (e.g. rand()%100). While it is slightly non-uniformly distributed that way this should not be an issue here. – Ctx

Armali
  • 18,255
  • 14
  • 57
  • 171