#include <stdio.h>

#include<stdlib.h>



int **matrix_mul(int **m1,int **m,int a,int b,int c,int d);







main()

{

  int i,j,r1,r2,c1,c2,**p,**q;









printf("Enter the number of rows and columns of first matrix :\t");

scanf("%d%d",&r1,&c1);

printf("Enter the number of rows and columns of second matrix :\t");

scanf("%d%d",&r2,&c2);











  //memory allocation for m1

int **m1;

  m1 = malloc(r1 * sizeof(int *));

  if(m1 == NULL)

    {

      printf("out of memory\n");

      return 0;

    }

  for(i = 0; i < r1; i++)

    {

      m1[i] = malloc(c1 * sizeof(int));

      if(m1[i] == NULL)

	{

	  printf("out of memory\n");

	  return 0;

	}	  

    }







//memory allocation for m1

int **m2;

  m2 = malloc(r2 * sizeof(int *));

  if(m2 == NULL)

    {

      printf("out of memory\n");

      return 0;

    }

  for(i = 0; i < r2; i++)

    {

      m2[i] = malloc(c2 * sizeof(int));

      if(m2[i] == NULL)

	{

	  printf("out of memory\n");

	  return 0;

	}	  

    }



















if(c1==r2)

{

printf("Enter the elements of first matrix :\t");

for(i=0;i<r1;i++)

{

for(j=0;j<c1;j++)

{

scanf("%d \t",&m1[i][j]);

}

}  





printf("Enter the elements of second matrix :\n");

for(i=0;i<r2;i++)

{

for(j=0;j<c2;j++)

{

scanf("%d \n",&m2[i][j]);

}

}









printf("\n first matrix is:");

for(i=0;i<r1;i++)

{

for(j=0;j<c1;j++)

{

printf("%d \t",m1[i][j]);

}

}  







printf("\n second matrix is:");

for(i=0;i<r2;i++)

{

for(j=0;j<c2;j++)

{

printf("%d \t",m2[i][j]);

}

}











p=matrix_mul(m1,m2,r1,c1,r2,c2);


for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
q=(1/2*100)*p[i][j];





//display result
for(i=0;i<r1;i++)

{

for(j=0;j<c2;j++)

{

printf("%d\n",q[i][j]);

}

getchar();

}



}









else

printf("Multiplication not possible.\n");

getchar();

}











//FUNCTION_matrix_mul

int **matrix_mul(int **m1,int **m2,int r1,int c1,int r2,int c2 )

{

  int i,j,k;







//memory allocation to store product

int **p;

  p = malloc(r1 * sizeof(int *));

  if(p == NULL)

    {

      printf("out of memory\n");

      return 0;

    }

  for(i = 0; i < r1; i++)

    {

      p[i] = malloc(c2 * sizeof(int));

      if(p[i] == NULL)

	{

	  printf("out of memory\n");

	  return 0;

	}	  

    }











  //product



for(i=0;i<r1;i++)

{

for(j=0;j<c2;j++)

{

p[i][j]=0;

for(k=0;k<r2;k++)

{

p[i][j]=p[i][j]+(m1[i][k]*m2[k][j]);

}

}

}





 return p;

}

in the above matrix multiplication problem i find the product matrix and store the it in the memory location pointed by p. now i want to multiply each element in product by 50. i no longer need p but need only q. will it be ok if i declare q as

int **q;

without allocating separate memory for q??? will the elements of p in the corresponding location be replaced by elements of q???

Firstly, your code is terribly formatted! There are literally hundreds of extra carriage returns and the indenting is very irregular, making the code pretty much unreadable. Please show some effort and format you code in a sensible manner, rather than expect the reader to put in the extra effort to understand it. You will be much more likely to get a response that way. Also, have you tried to compile this code? I would say not, since there are a number of errors. For instance, you have used int **m = malloc(r*sizeof(int *)); which will not compile since, malloc() returns void * . You need to explicitly cast the return-type to int ** , like int **m = (int **)malloc(r*sizeof(int *)); . Also, in your function matrix_mul() you try and check for an error and then on finding it, you have return 1; , but 1 is of integer type, not int ** , which is what you declare the return-type of the function to be. You should at least try and compile your code! Incidentally, the standard way to do this kind of error-checking is to use the return value as an indicator of errors and pass a pointer to the variable that you want to put the result in.

To answer your question: why don't you just keep using p , but with the elements multiplied by 50? If you need to use a different variable name, you can just do int **q = p; and then use q as you would have p . This doesn't copy the values or anything though, you'll be writing over the old ones. Is that what you want?

You need to explicitly cast the return-type to int ** , like int **m = (int **)malloc(r*sizeof(int *));

I think this is only necessary in C++, in C there is an implicit conversion.

I think this is only necessary in C++, in C there is an implicit conversion.

Ah, I see. Maybe it's not strictly necessary then, but it's probably good practice to specify the return type explicitly, unless there's a really good reason for wanting an implicit cast? If not, then I take back my comment in that respect. I think that my general point is still valid though.

thanks.. these replies helps a lot.. i understand the problems with the format i presented the post.. pardon me for mistakes...
1. so i dont have to allocate memory for q and only thing needed is to declare q as int (**q)???
2. also i can use p and q both the same time. am i right???
3.even without the int** before malloc, program works well. anyway, i will try to follow standard methods and will declare the return type also in my next program

thanks.. these replies helps a lot.. i understand the problems with the format i presented the post.. pardon me for mistakes...
1. so i dont have to allocate memory for q and only thing needed is to declare q as int (**q)???

You need to declare q as int ** so that the type matches with p . For q to do anything to the values that p points at, you need to aim it at the same piece of memory, so something like:

/* Make an array of ints */
int *p = (int *)malloc(10*sizeof(int *));

/* Set the values of the array (the numbers 0 - 9, in this case) */
for(int i = 0; i < 10; i++)    p[i] = i;

/* Print the resulting array to the terminal */
printf("p = ");
for(int i = 0; i < 10; i++)    printf("%d ", p[i]);
printf("\n");

/* Make a pointer and aim it at the same place as p */
int *q = p;

/* Now do some things to the values that the pointer is aiming at */
for(int i = 0; i < 10; i++)    q[i] = 50*q[i];

/* Print out the new values */
printf("q = ");
for(int i = 0; i < 10; i++)    printf("%d ", q[i]);
printf("\n");

/* Now check the old values */
printf("p = ");
for(int i = 0; i < 10; i++)    printf("%d ", p[i]);  // They've changed as well!
printf("\n");

2. also i can use p and q both the same time. am i right???

You can, but they're both pointing at the same thing, so using them both at the same time might be confusing.

Getting back to your original application (matrix multiplication), I think it's quite common to do this kind of thing "in place". That is you have a function like

int mat_mult(double * const matrix_A, const double * const matrix_B, int &r_A, int &c_A, const int r_B, const int c_B);

The return value can be use to check for errors (eg. incompatible row/column sizes) and the result is written over the first matrix, and the new row/column sizes passed out to the those that were inputted as the sizes for the first matrix. This has exactly the effect that you're talking about, except that the data is written over the original data, and we just keep using the same pointer. For the exact same thing that you were talking about (scaling the elements of a matrix by a constant) you would use something like

int mat_scale(double * const matrix, const int size, const double scaleFactor)
{
    for(int i = 0; i < size; i++)
        matrix[i] *= scaleFactor;

    return 0;
}

There's not really any need to make another pointer to do the manipulation.

thank you... i think i have my answer...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.