Please create a C program with the following C code declares variable mtx of matrix type, and dynamically allocates memory for mtx. It will then get keyboard input for the N-by-N matrix. Subsequently, the program de-allocates (free) the memory of the matrix.
A. Your task is to write the codes to dynamically allocate and de-allocate the memory for the matrix.
B. Subsequently, you have to modify the above code so that now the program will first ask the user to enter the size of the matrix, accept two matrices of the same size (maximum size 5-by-5) where each element of these matrices are manually input, and then perform matrix addition. The program will then output all the input matrices and the matrix addition results, both to the standard output screen and also to the text file called matrix.txt.
Below is the source code for the above:
We basically allocate a memory block of size N*N and access the memory locations using pointers
Code(gcc 5.1.0):
#include<stdio.h>
#include <stdlib.h>
//function for freeing memory
int freemem(int *var,int N){
int i,j;
for (i = 0; i < N; i++)
{
int* Ptr = var[i*N];
free(Ptr);
}
}
int main(){
int i, j, N, *intMatrix1,*intMatrix2,*intMatrix3;
//Taking size of the array as input
printf("Enter N:\t");
scanf("%d", &N);
intMatrix1 = (int *)malloc(N * N * sizeof(int));
intMatrix2 = (int *)malloc(N * N * sizeof(int));
intMatrix3 = (int *)malloc(N * N * sizeof(int));
//Taking input for Matrix 1:
printf("Enter Matrix 1:\t");
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
{
scanf("%d",
&intMatrix1[i*N + j]);
}
}
//Taking input for Matrix 2:
printf("Enter Matrix 2:\t");
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
{
scanf("%d",
&intMatrix2[i*N + j]);
}
}
//Matrix additon taking place
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
{
intMatrix3[i*N + j] =
intMatrix1[i*N + j] + intMatrix2[i*N + j];
}
}
printf("Resultant Matrix :\t");
//Printing the matrix
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%d ",
intMatrix3[i*N + j]);
}
}
//Freeing the declared memory
freemem(intMatrix1,N);
freemem(intMatrix2,N);
freemem(intMatrix3,N);
}
Output:
Enter N: 2
Enter Matrix 1: 1
1
1
1
Enter Matrix 2: 2
2
2
2
Resultant Matrix : 3 3 3 3
--------------------------------
Process exited after 14.64 seconds with return value
3221226356
Press any key to continue . . .
Please create a C program with the following C code declares variable mtx of matrix type,...
Write in C code
What to do Write a function with the following signature: float* matrix multiplication(float* left, float* right, int rows, int shared, int columns); The first two arguments are two pointers to the beginning of two matrices with the dimensions (rows,shared) and (shared, columns) correspondingly. The remaining arguments specify these dimensions. The return value will return a pointer to the very beginning of the result matrix. That said, you need to provide the space for the result matrix...
Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...
Create the program in C++ please.
The new operator as outlined in
10.9
. Create a Test class that includes the following Data members • First Name, last name, test1, test2, test3 o Methods • Accessor methods for each of the data members' • Mutator methods for each of the data members . A Default Constructor . A constructor that will accept arguments for each of the 5 data members • A method that will calculate the average of the...
C++,please help. I have no idea how to do that begining with
zero parameter constructor. and help me to test the method,
Write a class template Square Matrix that implements a square matrix of elements of a specified type. The class template should be fully in the Square Matrix.h file. The class uses "raw" pointers to handle the dynamically allocated 2D-array. "Raw" pointers are the classic pointers that you've used before. In addition to it, you will need a variable...
Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...
In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...
READ CAREFULLY AND CODE IN C++
Dynamic Programming: Matrix Chain Multiplication Description In
this assignment you are asked to implement a dynamic programming
algorithm: matrix chain multiplication (chapter 15.2), where the
goal is to find the most computationally efficient matrix order
when multiplying an arbitrary number of matrices in a row. You can
assume that the entire input will be given as integers that can be
stored using the standard C++ int type and that matrix sizes will
be at...
basic c++
2. AC++ program contains the following C++ code (assume an int occupies 2 bytes each in memory): int x[ ] = { 10, 20, 30, 40, 50, 60, 70, 80); int *ptrx ptrx = x; Array x starts at memory location 2140. a. What is the value assigned to ptrx? b. What is the value of (x+2)? c. What is the value of *x? d. What is the value of (*x+2)? e. What is the value of *(x+2)?...
(a) Write a C program to print a list of all integers between 1 and 1,000 (inclusive) which are divisible by 7 but not by 13. The list should be printed to a file called "output.dat". Remember to close the file after use. (35%) (b) Explain what is meant by the C preprocessor. Explain the effect of the following line of code: #define SQUARE (x) (x) * (x) (25%) (c) Explain the concept of an array in C. Write down...
Write C code for the following: Please type the code in the solution. Also, please put the screenshot of the codes of the program being run successfully. Also, put the screenshot of the output from the computer screen. I am getting many errors while trying to run the program. Screenshots of the program will help me. Find all the roots using the Newton-Raphson method: 2 + sin(x) - 3x - x2 = 0