answersLogoWhite

0


Best Answer

The easiest way would be to write a function that accepts two arrays and returns one.

Lets say you are working with two 2-dimensional arrays.

array<int,2>^ SubtractMatrices(int arr1[][2], int arr2[][2]);

void main()

{

int arr1[2][2] = {0,1,2,3};

int arr2[2][2] = {0,1,2,4};

array<int,2>^ newarr;

// array<int^,2>^ newarr[2][2] = SubtractMatrices(arr1, arr2);

}

array<int,2>^ SubtractMatrices(int arr1[][2], int arr2[][2])

{

array<int,2>^ newarr;

//Insert subtraction algorithm here

return newarr;

}

In this scenario you must pass the function 2 matrices, and then return a pointer to a new matrix.

Hmm, still seems to be an error in there with the return type. I'm not sure if that's the correct way to do it, I did this in Visual Studio's managed C++. .

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago



#include
#include


void main()
{
int a[20][20],b[20][20],c[20][20],i,j,r1,r2,c1,c2,ta,tb;
clrscr();
printf("Enter order of Matrix A: ");
scanf("%d%d",&r1,&c1);
ta=r1*c1;
printf("\nEnter %d elements=",ta);
for(i=0;i{
for(j=0;jscanf("%d",&a[i][j]);
}
printf("\nEnter order of Matrix B: ");
scanf("%d%d",&r2,&c2);
tb=r2*c2;
if(r1==r2 && c1==c2)
{
printf("\nEnter %d elements=",tb);
for(i=0;i{
for(j=0;jscanf("%d",&b[i][j]);
}
for(i=0;i{
for(j=0;jc[i][j]=a[i][j]-b[i][j];
}
printf("\nSubtraction of matrix is\n");
for(i=0;i{
for(j=0;j{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("\nSubraction is not possible pls enter same order");
getch();
}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include <iostream>

#include <iomanip>

using namespace std;

int ** CreateMatrix( const int & rows, const int & cols )

{

int ** Matrix = ( int ** ) calloc( rows, sizeof( int * ));

for( int row=0; row<rows; ++row )

Matrix[row] = ( int * ) calloc( cols, sizeof( int ));

return( Matrix );

}

void DestroyMatrix( int ** Matrix, const int & rows, const int & cols )

{

for( int row=0; row<rows; ++row )

free( Matrix[row] );

free( Matrix );

}

void PrintMatrix( int ** Matrix, const int & rows, const int & cols )

{

for( int row=0; row<rows; ++row )

{

for( int col=0; col<cols; ++col )

cout << setw(6) << Matrix[row][col];

cout << endl;;

}

cout << endl;

}

int main()

{

// Seed random number generator.

srand(( unsigned int ) time( NULL ));

// Randomly set rows and columns for two-dimensional arrays (2-10 elements each).

int rows = rand() % 9 + 2;

int cols = rand() % 9 + 2;

// Instantiate two-dimensional arrays of int.

int ** Matrix1 = CreateMatrix(rows, cols);

int ** Matrix2 = CreateMatrix(rows, cols);

// Initialise arrays with random values (1 to 99).

for( int row=0; row<rows; ++row )

for( int col=0; col<cols; ++col )

{

Matrix1[row][col] = rand() % 99 + 1;

Matrix2[row][col] = rand() % 99 + 1;

}

// Print matrices.

PrintMatrix( Matrix1, rows, cols );

cout << "minus" << endl << endl;

PrintMatrix( Matrix2, rows, cols );

// Instantiate and initialise the result matrix.

int ** Result = CreateMatrix(rows, cols);

for( int row=0; row<rows; ++row )

for( int col=0; col<cols; ++col )

Result[row][col] = Matrix1[row][col] - Matrix2[row][col];

// Print result.

cout << "equals" << endl << endl;

PrintMatrix( Result, rows, cols );

// Release the arrays.

DestroyMatrix( Result, rows, cols );

DestroyMatrix( Matrix2, rows, cols );

DestroyMatrix( Matrix1, rows, cols );

Result = NULL;

Matrix2 = NULL;

Matrix1 = NULL;

return( 0 );

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a C program using dynamic memory allocation to subtract two matrices?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the use of Constructor with Dynamic allocation?

Constructors are necessary to initialize classes. It allows to avoid to a lot of problems with unauthorized access of memory. Dynamic allocation makes possible allocation of memory during execution of program. If you do not use dynamic allocation, all required memory will be allocated during initialization phase (constructors are usually responsible for that). But you can't use more memory. Dynamic allocation was designed to overcome such problems.


A C program using dynamic memory allocation to sort n names in ascending order?

Writing a C program that uses dynamic memory allocation to sort names in ascending order is a typical computer science assignment. To write this program, you must be in UNIX.


Contiguous memory allocation program in Linux?

malloc or calloc


What do you understand by static memory allocation and dynamic memory allocation?

MEMORY ALLOCATION MODELS&hellip;&hellip;STATIC MEMORY ALLOCATION DYNAMIC MEMORY ALLOCATIONMemory is allocated before the execution of the program begins.(During Compilation)Memory is allocated during the execution of the program.No memory allocation or deallocation actions are performed during Execution.Memory Bindings are established and destroyed during the Execution.Variables remain permanently allocated.Allocated only when program unit is active.Implemented using stacks and heaps.Implemented using data segments.Pointer is needed to accessing variables.No need of Dynamically allocated pointers.Faster execution than Dynamic.Slower execution than static.More memory Space required.Less Memory space required.


Difference between dynamic and static memory allocation?

Static memory allocation is memory allocated on the "stack" and cannot be resized after the initial allocation, while dynamic memory allocation is memory allocated in the "heap", and can be dynamically expanded and shrunk as necessary.

Related questions

What is difference in static and dynamic storage allocation?

Static storage allocation is when a program dedicates an amount of memory for its use at the start of the program. Dynamic storage allocation is when a program only takes storage as it needs it.


Write a c program using dynamic memory allocation function calloc to find sum of two matrices and also print the resultant matrix?

printf("%s",per&gt;50?:"pass",per&lt;50?:"fail");


What is the use of Constructor with Dynamic allocation?

Constructors are necessary to initialize classes. It allows to avoid to a lot of problems with unauthorized access of memory. Dynamic allocation makes possible allocation of memory during execution of program. If you do not use dynamic allocation, all required memory will be allocated during initialization phase (constructors are usually responsible for that). But you can't use more memory. Dynamic allocation was designed to overcome such problems.


A C program using dynamic memory allocation to sort n names in ascending order?

Writing a C program that uses dynamic memory allocation to sort names in ascending order is a typical computer science assignment. To write this program, you must be in UNIX.


Write a C program using dynamic memory allocation to find the sum of elements of a matrix?

Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.


What is heaped?

The heap is a section of memory controlled by a program used for dynamic variable allocation. Heap size is the size of that section of memory.


What do you mean by run time memory allocation?

When the allocation of memory to the program is done on need, during the execution of a program, it is called as the dynamic memory allocation. Memory is allocated from a free memory pool called as heap.The only way to access this dynamically allocated memory is through pointers. Dynamically allocated memory can be freed at run time and again added to heap.


What is segmented page allocation?

Segmented page allocation is a type of memory management that uses base and bound registers to determine memory faults, similar to dynamic page allocation. More importantly it is different to dynamic page allocation since the entire process doesn't have to be in memory, similar to using virtual memory paging where the program is broken into pieces. Unlike virtual memory paging, the maximum virtual memory size is limited to the size of physical memory.


Program to display multiplication of two matrix?

The matrix multiplication in c language : c program is used to multiply matrices with two dimensional array. This program multiplies two matrices which will be entered by the user.


How do you develop a JAVA program that computes matrices?

Matrices can't be "computed" as such; only operations like multiplication, transpose, addition, subtraction, etc., can be done. What can be computed are determinants. If you want to write a program that does operations such as these on matrices, I suggest using a two-dimensional array to store the values in the matrices, and use for-loops to iterate through the values.


Write a C program to find sum of 3 matrices?

matrix


What is dynamic mamory?

Dynamic memory refers to memory that is allocated and deallocated during program execution, as opposed to static memory which is allocated at compile time. In C and C++, dynamic memory allocation is done using functions like malloc() and free(), allowing for flexibility in managing memory resources at runtime. However, improper use of dynamic memory can lead to memory leaks or segmentation faults.