You are here:

C/program not running right way

Advertisement


Question
QUESTION: #include<conio.h>
#include<stdio.h>
#include<process.h>
#include<alloc.h>
#include"dos.h"   // for delay() function
void main()
{
  int **x,**y,**z,i,j,r1,r2,c1,c2,n,k,l,temp;  //static int temp;
  clrscr();
  printf("Enter number of Rows in A:");
  scanf("%d",&r1);
  printf("Enter number of  Columns in A:");
  scanf("%d",&c1);
  x=(int**)calloc(r1*c1*2);
  if(x==NULL)
  {
     printf("Failed to allocate memory to A");
     delay(100);
     exit(1);
  }
   //printf("sizeof x= %d,sizeof(x));
  printf("enter elements in the matrix A");
  for(i=0;i< r1;i++)
  {
     for(j=0;j <c1; j++)
     {
        scanf("%d",&n);
        //printf("n=%d \n ",n);
        *(*(x+i)+j)=n;
        l=*(*(x+i)+j);
        //printf("x= %d \n ",*(*(x+i)+j));
        //printf("l= %d \n",l);
     }
  }
  printf("\n----------------------------\n");
  printf("Enter number of Rows in B:");
  scanf("%d",&r2);
  printf("Enter number of Columns in B:");
  scanf("%d",&c2);
  y=(int**)calloc(r2*c2*2);
  if(y==NULL)
  {   printf("Failed to allocate memory to B");
     delay(100);
     exit(1);
  }
  printf("enter element is matrix B: ");
  for(i=0;i< r2; i++)
  {
     for(j=0;j< c2;j++)
     {
        scanf("%d",&n);
        //printf("n= %d \n",n);
        *(*(y+i)+j)=n;
        //printf("y=%d \n",*(*(y+i)+j));
     }
  }
  printf("\n ---------------------------------");
  printf("\n Elements of A Matrices are : \n");
  for(i=0; i< r1; i++);
  {
     for(j=0;j<c1;j++)
     {
        printf("%d",*(*(x+i)+j));
     }
     printf("\n");
  }
  printf("\n\n Elements of B Matrices are : \n");
  for(i=0; i< r2; i++);
  {
     for(j=0;j<c2;j++)
     {
        printf(" %d ",(*(*(y+i)+j)));
     }
     printf("\n");
  }
  if((r1==c2)&&(r2==c1))
  {
     z=(int**)malloc(r1*r2*2);
     if(z==NULL)
     {
     printf("Failed to allocate memory to A*B");
     delay(100);
     exit(1);
     }
     printf("\n-------------------------\n");
     printf("\n\n A*B is : \n");
     for(i=0; i <r1; i++)
     {
        for(j=0;j <c2; j++)
        {
        temp=0;
        for(k=0; k< r2; k++)
        {
         temp=temp+ (*(*(x+i)+k) **(*(y+k)+j));
         //temp=*(*(z+i)+j)
        }
        *(*(z+i)+j)=temp;
        printf("z[%d] [%d]=%d ",i,j,*(*(z+i)+j));
        temp=0;
     }
     printf("\n");
  }
}
  else
  {
  printf("A cannot be multipied with B");
  delay(1000);
  exit(1);
  }
  //printf("%d",x);
  getch();
}

ANSWER: There is a syntax error while writng calloc function.
Calloc function always takes 2 parameters. One is the no. of elements you want to enter and second is the total size required for that.
I think in above program you are trying to write the code that will calculate the marix multiplication. For that purpose you have to write calloc function as follows:
x=(int**)calloc(r1*c1,r1*c1*2);
y=(int**)calloc(r2*c2,r2*c2*2);
Try this. I think it should work properly now.



---------- FOLLOW-UP ----------

QUESTION: sir i have made following program for multiplication of 2d matrix using DMA....but in the columns to show entered value it is not showing the entered values correctly altough the result of the multiplication is correct.
here is the column i m facing issue:

printf("\n ---------------------------------");
      printf("\n Elements of A Matrices are : \n");
      for(i=0; i< r1; i++);
      {
         for(j=0;j<c1;j++)
         {
         printf("%d ",*(*(x+i)+j));
         }
         printf("\n");
      }
      printf("\n\n Elements of B Matrices are : \n");
      for(i=0; i< r2; i++);
      {
         for(j=0;j<c2;j++)
         {
         printf("%d ",*(*(y+i)+j));
         }
         printf("\n");
      };




and here is the complete program:
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<alloc.h>
#include"dos.h"   // for delay() function
void main()
{
     int **x,**y,**z,i,j,r1,r2,c1,c2,n,k,l,temp;  //static int temp;
     clrscr();
     printf("Enter number of Rows in A:");
     scanf("%d",&r1);
     printf("Enter number of  Columns in A:");
     scanf("%d",&c1);
     x=(int**)calloc(r1*c1*2);
     if(x==NULL)
     {
         printf("Failed to allocate memory to A");
         delay(100);
         exit(1);
     }
      //printf("sizeof x= %d,sizeof(x));
     printf("enter elements in the matrix A");
     for(i=0;i< r1;i++)
     {
         for(j=0;j <c1; j++)
         {
         scanf("%d",&n);
         //printf("n=%d \n ",n);
         *(*(x+i)+j)=n;
         l=*(*(x+i)+j);
         //printf("x= %d \n ",*(*(x+i)+j));
         //printf("l= %d \n",l);
         }
     }
     printf("\n----------------------------\n");
     printf("Enter number of Rows in B:");
     scanf("%d",&r2);
     printf("Enter number of Columns in B:");
     scanf("%d",&c2);
     y=(int**)calloc(r2*c2*2);
     if(y==NULL)
     {       printf("Failed to allocate memory to B");
         delay(100);
         exit(1);
     }
     printf("enter element is matrix B: ");
     for(i=0;i< r2; i++)
     {
         for(j=0;j< c2;j++)
         {
         scanf("%d",&n);
         //printf("n= %d \n",n);
         *(*(y+i)+j)=n;
         //printf("y=%d \n",*(*(y+i)+j));
         }
     }
     printf("\n ---------------------------------");
     printf("\n Elements of A Matrices are : \n");
     for(i=0; i< r1; i++);
     {
         for(j=0;j<c1;j++)
         {
         printf("%d",*(*(x+i)+j));
         }
         printf("\n");
     }
     printf("\n\n Elements of B Matrices are : \n");
     for(i=0; i< r2; i++);
     {
         for(j=0;j<c2;j++)
         {
         printf(" %d ",(*(*(y+i)+j)));
         }
         printf("\n");
     }
     if((r1==c2)&&(r2==c1))
     {
         z=(int**)malloc(r1*r2*2);
         if(z==NULL)
         {
         printf("Failed to allocate memory to A*B");
         delay(100);
         exit(1);
         }
         printf("\n-------------------------\n");
         printf("\n\n A*B is : \n");
         for(i=0; i <r1; i++)
         {
         for(j=0;j <c2; j++)
         {
         temp=0;
         for(k=0; k< r2; k++)
         {
         temp=temp+ (*(*(x+i)+k) **(*(y+k)+j));
         //temp=*(*(z+i)+j)
         }
         *(*(z+i)+j)=temp;
         printf("z[%d] [%d]=%d ",i,j,*(*(z+i)+j));
         temp=0;
         }
         printf("\n");
     }
}
     else
     {
     printf("A cannot be multipied with B");
     delay(1000);
     exit(1);
     }
     //printf("%d",x);
     getch();
}

Answer
whenever we put ; after the for statement it executes the statement block whenever condition goes false.
for e.g., in your code,
printf("\n ---------------------------------");
     printf("\n Elements of A Matrices are : \n");
     for(i=0; i< r1; i++);
     {
        for(j=0;j<c1;j++)
        {
        printf("%d ",*(*(x+i)+j));
        }
        printf("\n");
     }

here you put ; after outer for statement, At this stage it will run only when the value of i=r1.
Similarly you put ; after for statement while printing values of matrices B.
     printf("\n\n Elements of B Matrices are : \n");
     for(i=0; i< r2; i++);
     {
        for(j=0;j<c2;j++)
        {
        printf("%d ",*(*(y+i)+j));
        }
        printf("\n");
     };

Just try it after removing the semicolon it should work now.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Kedar Desai

Expertise

You can ask me any questions about C and c++

Experience

I don't have practical experience. But i have knowledge about C as I belong to IT field since last 5 years.

Education/Credentials
I an studying in MCA from Maharashtra (Mumbai University)

©2012 About.com, a part of The New York Times Company. All rights reserved.