pls help i cant find the problem :(((

This is a discussion on pls help i cant find the problem :((( within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> typedef int *matrix_member; void print_matrix(matrix_member matrix,int size) ; void multiply_matrix(matrix_member m1,matrix_member m2,matrix_member result,int size); void ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    pls help i cant find the problem :(((

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int *matrix_member;
    
     void print_matrix(matrix_member matrix,int size) ;
     void multiply_matrix(matrix_member m1,matrix_member m2,matrix_member result,int size);
     void add_matrix(matrix_member m1,matrix_member m2,matrix_member result,int size);
     matrix_member create_matrix(int *size);
     matrix_member return_address(int i,int j,matrix_member m,int size);
     matrix_member allocate_memory(int num);
    
     int main()
    {
    
     int size1,size2;
     matrix_member matrix1,matrix2,addition_matrix,multiplication_matrix;
    
     //create first matrix
     //create second matrix
     //print them
     //add them and print the result
     //multiply them and print the result
    
     matrix1=create_matrix(&size1);
     matrix2=create_matrix(&size2);
    
     print_matrix(matrix1,size1);
     print_matrix(matrix2,size2);
    
     addition_matrix= allocate_memory(size1);
     add_matrix(matrix1,matrix2,addition_matrix,size1);
     print_matrix(addition_matrix,size1);
    
     multiply_matrix(matrix1,matrix2,multiplication_matrix,size2);
     print_matrix(multiplication_matrix,size2);
    
     return 0;
    }
    
    matrix_member allocate_memory(int num)
    {
      matrix_member t;
      t=malloc(sizeof(int)*(num*num));
      return (t);
    }
    
    void ask_matrix_size(int *num)
    {
    	printf("enter size of matrix:");
    	scanf("%d",num);
    }
    //-------------------problem--------------
    void read_matrix(matrix_member matrix,int size)
    {
     int i,j,number;
    
    
       for(i=0;i<size*size;i++)
    	{
    	printf("enter value of matrix:");
    	scanf("%d",&matrix[i]);
    	}
    
    
    }
    //-------------------------------------------------------!!!!!!
    
    void  print_matrix(matrix_member matrix,int size)
    {
      int i,j;
      for (i=0;i<size;i++)
      {
       for(j=0;j<size;j++)
        printf("%d  ",*return_address(i,j,matrix,size));
      printf("\n");
      }
      printf("\n");
    }
    
     matrix_member create_matrix(int *size)
    {
      matrix_member st_matrix;
      ask_matrix_size(size);
      st_matrix= allocate_memory(*size);
      read_matrix(st_matrix,*size);
      return(st_matrix);
    }
    
    void  add_matrix(matrix_member m1,matrix_member m2,matrix_member result,int size)
    {
       int i;
    
       for(i=0;i<size*size;i++)
    	  result[i] = m1[i] + m2[i];
    
    
    }
    
    matrix_member return_address(int i,int j,matrix_member m,int size)
    {
        int a;
        for(a=0;a<(i*size+j);a++);
    	m++;
    
    	return m;
    }
    void  multiply_matrix(matrix_member m1,matrix_member m2,matrix_member result,int size)
    {
    }
    the output:

    enter matrix size:2
    enter value of matrix:1
    enter value of matrix:2
    enter value of matrix:3
    enter value of matrix:4
    the output has to be like that:
    1 2
    3 4

    but i cant do this my output is :
    2 2
    2 2
    (the second value of matrix)

    how can i fix this problem pls help
    i think the problem occurs in the function read_matrix()

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I did not test your program but this might be a problem:
    Code:
    matrix_member return_address(int i,int j,matrix_member m,int size)
    {
        int a;
        for(a=0;a<(i*size+j);a++);
    	m++;
    
    	return m;
    }
    You are incrementing m, wich is the original pointer to the matrix. You should create a temp pointer and return it.
    Code:
    matrix_member return_address(int i,int j,matrix_member m,int size)
    {
        int a;
       matrix_member tmp = m;
        for(a=0;a<(i*size+j);a++);
    	tmp++;
    
    	return tmp;
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Question i tried it it doesnt the solution

    i changed return adress but i have the same problem???

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    thanks this works

    thanks))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem understandnig...pls help
    By gtr_s15 in forum C++ Programming
    Replies: 6
    Last Post: 01-19-2006, 03:00 PM
  2. Replies: 5
    Last Post: 04-16-2004, 02:29 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. FPS Problem, pls help
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 06-21-2002, 01:03 PM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21