#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pi 3.14159265

int **transpose(int **x,int m,int n);
int **matrix_mul(int **m1,int **m2,int r1,int c1,int r2,int c2 );
int **dc4(int **x,int nrows,int ncolumns);

main()
{

  printf("hi");

  int nrows=80000,ncolumns=1,i,j,**x,**dc4_x,**ix,M=nrows,N=ncolumns;


  //memory allocation for x
  x = malloc(nrows * sizeof(int *));
  if(x == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < nrows; i++)
    {
      x[i] = malloc(ncolumns * sizeof(int));
      if(x[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}	  
    }
 





  printf("hi");



  //define some x(will take audio input later)
  for(i=0;i<80000;i++)
    for(j=0;j<1;j++)
    x[i][j]=rand()%3;

  for(i=0;i<8;i++)
    printf("%d \t",x[i][j]);


  //find dc4
  dc4_x=dc4(x,80000,1);

 
  //display dc4
  for(i=0;i<M;i++)
    for(j=0;j<1;j++)
      printf("%d",dc4_x[i][j]);
    


}









//FUNCTION_dc4
int **dc4(int **x,int nrows,int ncolumns)
{
  int M,N,i,j,**y,**T,**ix,**cn;
  M=nrows;
  N=ncolumns;        //directly giving the size of x (M,N). will include program to find the size of matrix later.




  //memory allocation for y,store dc4
  y = malloc(nrows * sizeof(int *));
  if(y == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < nrows; i++)
    {
      y[i] = malloc(ncolumns * sizeof(int));
      if(y[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}	  
    }








  //memory allocation for cn
  
  cn = malloc(nrows * sizeof(int *));
  if(cn == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < nrows; i++)
    {
      cn[i] = malloc(ncolumns * sizeof(int));
      if(cn[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}	  
    }





  //define cn 
  cn[0][1]=1;
  for(i=0;i<N;i++)
    for(j=2;j<M;j++)
      {
	cn[i][j]=cn[i][j-1]+2;
      }




  //find cn'(i.e. cn_transpose)
  int **cn_transpose;
  cn_transpose=transpose(cn,M,N);





  

  //find ix=cn'*cn
  ix=matrix_mul(cn_transpose,cn,M,N,N,M);


  

  //memory allocation for T
  T = malloc(M * sizeof(int *));
  if(T == NULL)
    {
      printf("out of memory\n");
      return 0;
    }

  
  for(i = 0; i < M; i++)
    {
      T[i] = malloc(M * sizeof(int));
      if(T[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}
    }









  //define T
  for(i=0;i<M;i++)
    for(j=0;j<M;j++)
      T[i][j]=(1/sqrt(M/2))*cos((pi*ix[i][j])/(4*M));







  //find dc4
  y=matrix_mul(T,x,M,M,M,1);

  //return dc4
  return y;
}





	







  //FUNCTION_transpose
  int **transpose(int **x,int m,int n)
  {
    int nrows=n,ncolumns=m,i,j;

    //memory allocation for y,to store transpose
    int **y;
    y = malloc(nrows * sizeof(int *));
    if(y == NULL)
      {
	printf("out of memory\n");
	return 0;
      }
    for(i = 0; i < nrows; i++)
      {
	y[i] = malloc(ncolumns * sizeof(int));
	if(y[i] == NULL)
	  {
	    printf("out of memory\n");
	    return 0;
	  }	  
      }




 
    for(i=0;i<m;i++)
      for(j=0;j<n;j++)
	{
	  y[i][j]=x[j][i];
	}
    return y;
  }






  

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

segmentation fault comes. cant detect any errors . help please

You have some many blank lines it makes your code much longer than required and so less readable.

You have redundant duplicate variables, for example M in dc4 is just a copy of nrows, however neither variable is ever changed so one of them is redundant.

However I believe your error is actually on line 244 of your posted code, work through the logic of your own program.

sorry for being rude and make you work for my code.Thank you, problem was there in 244th itself.
my corrected working code is also posted below.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pi 3.14159265
double **transpose(double **x,int m,int n);
double **matrix_mul(double **m1,double **m2,int r1,int c1,int r2,int c2);
double **dc4(double **x,int M,int N);
main()
{
  int i,j,M=8,N=1;
  double **x,**dc4_x;
  //memory allocation for x
  x = (double**)malloc(M * sizeof(double *));
  if(x == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < M; i++)
    {
      x[i] = (double*)malloc(N * sizeof(double));
      if(x[i] == NULL)
  	{
  	  printf("out of memory\n");
  	  return 0;
  	}
    }
  //define some x(will take audio input later)
  printf("x= \n");
  for(i=0;i<M;i++)
    {
      printf("\n");
      for(j=0;j<N;j++)
	{
	  x[i][j]=rand()%3;
	  printf("%f\t",x[i][j]);
	}
    }
  printf("\n");
  //call dc4
  dc4_x=dc4(x,M,N);
  //print dc4
  printf("dc4=\n");
  for(i=0;i<M;i++)
    {
      printf("\n");
      for(j=0;j<M;j++)
	{
	  printf("%f\t",dc4_x[i][j]);
	}
    }
  printf("\n");
}
//FUNCTION_transpose
double **transpose(double **x,int M,int N)
{
  int nrows=N,ncolumns=M,i,j;
  double **y;
  //memory allocation for y,to store transpose
  y = (double**)malloc(nrows * sizeof(double *));
  if(y == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < nrows; i++)
    {
      y[i] = (double*)malloc(ncolumns * sizeof(double));
      if(y[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}	  
    }
  //taking transpose
  for(i=0;i<N;i++)
    for(j=0;j<M;j++)
      {
	y[i][j]=x[j][i];
      }
  return y;
}
//FUNCTION_matrix_mul
double **matrix_mul(double **m1,double **m2,int r1,int c1,int r2,int c2)
{
  int i,j,k;
  double**p;
  //memory allocation to store product
  p = (double**)malloc(r1 * sizeof(double *));
  if(p == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < r1; i++)
    {
      p[i] = (double*)malloc(c2 * sizeof(double));
      if(p[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}	  
    }
  //find product of matrices
  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;
} 
//FUNCTION_dc4
double **dc4(double **x,int M,int N)
{
  int i,j;
  double **T,**cn,**ix,**y;
  //memory allocation for cn
  cn = (double**)malloc(N * sizeof(double *));
  if(cn == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < N; i++)
    {
      cn[i] = (double*)malloc(M * sizeof(double));
      if(cn[i] == NULL)
  	{
  	  printf("out of memory\n");
  	  return 0;
  	}
    }
  //define cn
  printf("cn=\n");
  cn[0][0]=1;
  for(i=0;i<N;i++)
    {
      printf("\n");
      for(j=1;j<M;j++)
	{
	  cn[i][j]=cn[i][j-1]+2;
	  printf("%f\t",cn[i][j]);
	}
    }
  printf("\n");
  //find cn'(i.e. cn_transpose)
  double **cn_transpose;
  cn_transpose=transpose(cn,N,M);
  //print cn_transpose
  printf("cn_transpose=\n");
  for(i=0;i<M;i++)
    {
      printf("\n");
      for(j=0;j<N;j++)
	{
	  printf("%f\t",cn_transpose[i][j]);
	}
    }
  printf("\n");
  //find ix=cn'*cn
  ix=matrix_mul(cn_transpose,cn,M,N,N,M);
  //print ix
  printf("ix=\n");
  for(i=0;i<M;i++)
    {
      printf("\n");
      for(j=0;j<M;j++)
	{
	  printf("%f\t",ix[i][j]);
	}
    }
  printf("\n");
  //memory allocation for T
  T = (double**)malloc(M * sizeof(double *));
  if(T == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < M; i++)
    {
      T[i] = malloc(M * sizeof(double));
      if(T[i] == NULL)
  	{
  	  printf("out of memory\n");
  	  return 0;
  	}
    }
  //define & print T
  printf("T=\n");
  for(i=0;i<M;i++)
    {
      printf("\n");
      for(j=0;j<M;j++)
	{
	  T[i][j]=(1/sqrt(M/2))*cos((pi*ix[i][j])/(4*M));
	  printf("%f\t",T[i][j]);	  
	}
    }
  printf("\n");   
  //find dc4
  y=matrix_mul(T,x,M,M,M,1);
  return y;     
}
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.