Question

Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M. it is sufficient to multiply M by itself n _ 1 times. That is,AP-M×M×M Naturally, a different strategy is needed for r Input format The first line of the input file contains an integer k. This indicates the size of the matrix M, which has k rows and k columns. The next k lines in the input file contain k integers. These indicate the content of M.Each line corresponds to a row, beginning with the first (top) row The final line contains an integer n. This indicates the number of times M will be nultiplied by itself, n is guaranteed to be non-negative, but it may be 0 For example, an input file file.txt containing 1 2 3 4 5 6 7 89 indicates that sexp must compute Output format The output of mexp is the computed matrix M. Each row of M is printed on a separate line, beginning with the first (top) row. The items within a row are separated by spaces Using file.txt from above, /nexp filel.txt 30 36 42 66 81 96 102 126 150 This is done in C on command line in Linux
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])

{

if (argc != 2)

{

printf("Usage: ./mexp file1.txt\n");

return 0;

}

FILE *fp;

fp = fopen(argv[1], "r");

if (fp == NULL)

{

printf("Unable to open file\n");

return 0;

}

int i, j, k, m, n, times;

fscanf(fp, "%d", &n);

int **M = (int **)malloc(n * sizeof(int *));

int **M1 = (int **)malloc(n * sizeof(int *));

int **res = (int **)malloc(n*sizeof(int *));

for (i = 0; i < n; i++){

M[i] = (int *)malloc(n * sizeof(int));

M1[i] = (int *)malloc(n * sizeof(int));

res[i] = (int *)malloc(n * sizeof(int));

}

for (i = 0; i < n; i++)

{

for (j = 0; j < n; j++)

{

fscanf(fp, "%d", &M[i][j]);

M1[i][j] = M[i][j];

}

}

fscanf(fp, "%d", &times);

for (m = 0; m < times-1; m++)

{

for (i = 0; i < n; i++)

{

for (j = 0; j < n; j++)

{

res[i][j] = 0;

for (k = 0; k < n; k++)

res[i][j] += M[i][k] * M1[k][j];

}

}

for(i=0;i<n; i++){

for(j=0;j<n; j++){

M1[i][j] = res[i][j];

}

}

}

for(i=0;i<n; i++){

for(j=0;j<n; j++){

printf("%d ", res[i][j]);

}

printf("\n");

}

fclose(fp);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...

    C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the following definition: matrix -size:int -mat:int** ---------------- +matrix(file:string) +~matrix() +operator +(add:matrix*):matrix & +operator-(sub:matrix*):matrix & +friend operator<<(out:ostream &, t:const matrix&):ostream & +displayRow(r:int):void The class variables are as follows: • size: the number of rows and columns in a square matrix • mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2 • matrix(file:string): The default constructor. It receives the...

  • *** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...

    *** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an nby- n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note...

  • Write the following C++ program that searches for specified words hidden within a matrix of letters....

    Write the following C++ program that searches for specified words hidden within a matrix of letters. 1) Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2) Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row...

  • This is being done in c using command line in Linux 1.1 llist: Linked list Write...

    This is being done in c using command line in Linux 1.1 llist: Linked list Write a program llist that maintains and manipulates a sorted linked list of integers according to instructions received from standard inpu. The linked list is maintained in order, meaning that each integer will be larger than the one preceeding it. 1list maintains a linked list containing zero or more integers, ordered from least to greatest llist will need to allocate space for new nodes as...

  • This assignment requires you to write a program to implement an odd-order Magic Square. Prompt the...

    This assignment requires you to write a program to implement an odd-order Magic Square. Prompt the user to enter the “order” of the square and then produce for the user a magic square of that order. Run your code for at least four different inputs – all odd. ODD ORDER MAGIC SQUARES 1. Start by placing 1 in the middle column of the top row. 2. Continue by always placing the next number (say k+1) in the square diagonally up...

  • 4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n...

    4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n is the integer value in matrix B which is having the same index as that of the character taken in A). Solve this using 1D Block. Example: 1 2 4 3 e X a M Output String STR: pCCaaaaPPPeeXXXXaaaMM 4B. Write...

  • Please write code in java You’re given a chess board with dimension n x n. There’s...

    Please write code in java You’re given a chess board with dimension n x n. There’s a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king...

  • Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...

    Please write a C++ Program for the following problem >> Vector: Calculation the sum of each two adjacent Numbers.... >>【Description】 Read integer numbers from keyboard and store them into a vector. Calculation the sum of each two adjacent Numbers, store the result into another vector. Then output the result inversely. For example, if the input is 1 2 3 4 5 then the output is 9 7 5 3 【 Input】 There are 5 test cases. For each case, there...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • A company wants to invest into building a golf course. They could choose the land to...

    A company wants to invest into building a golf course. They could choose the land to build the course from a rectangle area of size MxN. The rectangle area is divided into MxN cells. They measured the elevation of each cell and recorded it into an integer number. The company decided that the golf course should be a KxK square (K ≤ min(M,N)). Thus, there are (M-K+1)(NK+1) possible KxK squares that can be used to build the golf course. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT