Question

Write a C++ function that initializes all the elements of M × N matrix mat to...

Write a C++ function that initializes all the elements of M × N matrix mat to 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
void calc(int *mat,int M,int N){ //function to initialize elements of M × N matrix mat to 0.
for(int i=0;i<M;i++)
for (int j=0;j<N;j++)
*((mat+i*N)+j)=0;
}
int main() //main function to enter value of M and N
{
int M,N;
cout<<"Enter M and N :";
cin>>M>>N;
int mat[M][N];
calc((int *)mat, M, N);
for(int i=0;i<M;i++){
for (int j=0;j<N;j++)
cout<<mat[i][j]; //prints the value inside Mat
cout<<endl;
}
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ function that initializes all the elements of M × N matrix mat to...
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
  • Write a doubly nested loop in C that Initializes all 50 * 50 elements of 2D...

    Write a doubly nested loop in C that Initializes all 50 * 50 elements of 2D nit array table [50] [50] to 123456 use no literals (magic numbers) except 0.

  • Topic Ch. 10 : Write a class definition for a 2x2 matrix, with methods for get(i,j),...

    Topic Ch. 10 : Write a class definition for a 2x2 matrix, with methods for get(i,j), set(i,j,v) for getting and setting (i,j)-the entry where i is the row index 0 or 1, and j is the column index 0 or 1, and set() sets the entry in row i and column j to be v, a method for adding a matrix y to given x (e.g. x.add(y) will set x=x+y where the corresponding matrix entries are added), for multiplying( e.g....

  • Bonus 1 • Write a java program to transpose a matrix mat[N][M], where: 1) You ask...

    Bonus 1 • Write a java program to transpose a matrix mat[N][M], where: 1) You ask the user to input N and M 2) You then ask the user to input the matrix row by row 3) You transpose the matrix 4) You output the transpose to the user

  • CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from...

    CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from row and // column wise sorted matrix #include<iostream> #include<climits> using namespace std;    #define INF INT_MAX #define N 4    // A utility function to youngify a Young Tableau. This is different // from standard youngify. It assumes that the value at mat[0][0] is // infinite. void youngify(int mat[][N], int i, int j) {     // Find the values at down and right sides of...

  • 47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all...

    47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all the rows of column n of the matrix A. b. Refers to the elements in all the rows between columns m and n of the matrix A. c. Refers to the elements in all the columns between rows m and n of the matrix A. d. Refers to the elements in all the columns of row n of the matrix A. 48. What does...

  • let mat be a matrix of integers in r. write a function to find how many...

    let mat be a matrix of integers in r. write a function to find how many rows have exactly two instances of the number 7

  • *** 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 a function Transpose that transposes a matrix T with M rows and N colums. The...

    Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14) Example: 1 2 3 0 -6 7 The transposed matrix TT is 1 0 2 -6 3 7 DEFAULT CODE: Add to code as needed: #include <iostream> #include <string> #include <cmath> using namespace std; void Transpose(int T[100][100], int TT[100][100], int M, int N) { int i; int j;...

  • This needs to be in C++ Problem3.cpp Problem3.h: Given a matrix, clockwise-rotate elements in it. Please...

    This needs to be in C++ Problem3.cpp Problem3.h: Given a matrix, clockwise-rotate elements in it. Please add the code to problem3.h and problem3.cpp, provided. Input 4 Output: 1 8 For 4*4 matrix Input: 4 7 9 10 11 12 13 14 15 16 Output: 9 10 6 4 13 11 7 8 14 15 16 12 #include "problem3.h" 2 3 void rotatematrix(int m, int n, int mat[][MAX]) 4 //write your code here 5 1 #ifndet LAB3 PROBLEM3 2 #define LAB3PROBLEM3...

  • 2. (4 marks) Write a C function to find the average of all array elements in...

    2. (4 marks) Write a C function to find the average of all array elements in an one-dimensional array. The function prototype is as follows, float array_average(float a[], int n); /* find the average of all array elements in array a, and n is the size of array a.

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