Thread: Adding two matrix using malloc

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    3

    Adding two matrix using malloc

    This is my code without malloc. I need to change the array size so there is no max size for each matrix. I must dynamically allocate space for all arrays used. So I need to use malloc to create my arrays. So I cant use int A[rows][cols].

    Code:
    /* This program asks the user for 2 matrices called A and B, as integers,
       and displays their sum, C. The max dimension of each matrix is 100. */
    
    #include<stdio.h>
    
    // Construct function
    void construct()
    {
       int m, n, i, j;// Variables
       int first[100][100], second[100][100], sum[100][100];// Matrices variables
    
       printf("Please enter the number of rows: ");
       scanf("%d",&m);
       printf("Please enter the number of columns: ");
       scanf("%d",&n);
    
       // User enters m x n amount whole numbers for the Matrix A
       printf("Enter Matrix A\n");
       for(i =0; i < m; i++)
          for(j =0; j < n; j++)
             scanf("%d",&first[i][j]);
    
       // User enters m x n amount whole numbers for the Matrix B
       printf("Enter Matrix B\n");
       for(i =0; i < m; i++)
          for(j =0; j < n; j++)
                scanf("%d",&second[i][j]);
    
       // Adds the sum of Matrix A and Matrix B
       for(i =0; i < m; i++)
          for(j =0; j < n; j++)
             sum[i][j]= first[i][j]+ second[i][j];
    
       // Display the sum of Matrix A and Matrix B
       printf("A + B =\n");
       for(i =0; i < m; i++)
       {
          for(j =0; j < n; j++)
             printf("%d ", sum[i][j]);
    
          printf("\n");// Prints new line
       }
    
       return;
    }
    
    // Main Function
    int main()
    {
        construct();// Calls construct function
        return 0;
    }
    Can someone help guide me through this? Im having a hard time understanding/using malloc. Should first, second, and sum be single pointers or double? How do I scan the matrix correctly? And how do I add them properly? Do I have to change my for loops?
    Any suggestions for the next step to be?

    Code:
    /* This program asks the user for 2 matrices called A and B, as integers,
       and displays their sum, C. The max dimension of each matrix is 100. */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    // Construct function
    void construct()
    {
       int m, n, i, j;      // m = rows; n = columns
       int *first, *second, *sum; // first = matrix1; second = matrix2
    
    
       printf("Please enter the number of rows: ");
       scanf("%d", &m);
       printf("Please enter the number of columns: ");
       scanf("%d", &n);
    
    
       first  = (int*)malloc(m * n * sizeof(int));
       second = (int*)malloc(m * n * sizeof(int));
       sum    = (int*)malloc(m * n * sizeof(int));
    
    
    
    
       // User enters m x n amount whole numbers for the Matrix A
       printf("Enter Matrix A\n");
       for (i = 0; i < m; i++)
          for (j = 0; j < n; j++)
             scanf("%d", *(*(first+i)+j));
    
    
       // User enters m x n amount whole numbers for the Matrix B
       printf("Enter Matrix B\n");
       for (i = 0; i < m; i++)
          for (j = 0; j < n; j++)
                scanf("%d", second[i][j];
    
    
       // Adds the sum of Matrix A and Matrix B
       for (i = 0; i < m; i++)
          for (j = 0; j < n; j++)
             *(sum[i][j])= *(first[i][j]) + *(second[i][j]);
    
    
    // Display the sum of Matrix A and Matrix B
       printf("A + B =\n");
       for (i = 0; i < m; i++)
       {
          for (j = 0; j < n; j++)
             printf("%d ", *(sum + i + j));
    
    
          printf("\n");
       }
    
    
       return ;
    }
    
    
    // Main Function
    int main()
    {
        construct(); // Calls construct function
        return 0;
    }

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by machine392 View Post
    Code:
       printf("Enter Matrix A\n");
       for (i = 0; i < m; i++)
          for (j = 0; j < n; j++)
             scanf("%d", *(*(first+i)+j));
    You have m rows and n columns in first. The element on row i,column j in first is therefore first[n*i + j], and the pointer to this is &(first[n*i+j]).

    For example, if m==2 and n==5, the elements are arranged as
    Code:
    first[0] first[1] first[2] first[3] first[4]
    first[5] first[6] first[7] first[8] first[9]
    which is also called row-major order.

    Also, you should always check the return value from scanf(); it returns the number of successful conversions (here 1). So, the above code snippet should really look like
    Code:
       printf("Enter Matrix A\n");
       for (i = 0; i < m; i++)
          for (j = 0; j < n; j++)
             if (scanf("%d", &(first[n*i+j])) != 1) {
                printf("Not an integer (row %d, column %d for matrix A).\n", i+1, j+1);
                return EXIT_FAILURE;
             }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Looks to be the same as this
    Converting 2D Array Code To Malloc - C And C++ | Dream.In.Code

    Except for the lack of any progress on your part to engage in a conversation, or attempt to make any changes to your code.

    If you're hoping to get someone to finish your assignment, you're in for a long wait.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    3
    Thank you so much it worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2011, 09:28 AM
  2. MPI read matrix malloc problem
    By Chris_1980 in forum C Programming
    Replies: 1
    Last Post: 06-10-2010, 03:48 AM
  3. C program 2D matrix multiplication using malloc
    By college_kid in forum C Programming
    Replies: 5
    Last Post: 04-03-2009, 10:04 AM
  4. adding two matrix
    By vaibhav in forum C++ Programming
    Replies: 7
    Last Post: 11-15-2005, 03:09 AM
  5. adding matrix
    By steven in forum C Programming
    Replies: 1
    Last Post: 10-24-2001, 12:32 AM

Tags for this Thread