menu

arrow_back How do I remove a row from a matrix?

by
1 vote
You need to remove the row that contains the maximal element from a 6*7 matrix.
Here is a snippet of code:
#define m 7
#define n 6
#define k 6
.....
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
max = A[0][0];
if (max < A[i][j])
{
max = A[i][j];
str = i;
}
}
}
for (int i = str; i < m-1; i++) {
for (int j = 0; j < k; j++)
{
B[i][j] = A[i + 1][j];
}
}
It just displays zeroed out elements and strange numbers in the last row of the matrix... What am I doing wrong?

2 Comments

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#define m 7
#define n 6
#define k 6
using namespace std;
int main() {
int A[m][n]{};
int B[k][n]{};
int max{}, str{};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = (j + ((pow(j, 0.5)) / cos(i)));
}
}
cout << "A: " << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
{
cout << setw(5) << A[i][j];
}
cout << endl;
}
max = A[0][0];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (max < A[i][j])
{
max = A[i][j];
str = i;
}
}
}
for (int i = str; i < m-1; i++) {
for (int j = 0; j <= n; j++)
{
B[i][j] = A[i + 1][j];
}
}
cout << "B: " << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
{
cout << setw(5) << B[i][j];
}
cout << endl;
}
return 0;
}
give us all the code so we don't have to guess where you have "Outputs" and what B is

2 Answers

by
0 votes
max = A[0][0]; should be outside of cycles looking for a maximum.
B[i][j] = A[i + 1][j]; - Where did B come from?

2 Comments

bunnyyy , Then where is the filling of the first lines of B?
I need to create a matrix B that does not have a row with the maximum element from matrix A
by
0 votes
In full code:

for (int i = str; i < m-1; i++) {
for (int j = 0; j <= n; j++)
{
B[i][j] = A[i + 1][j];
}
}
all lines B before str remain uninitialized. In the j loop the condition is wrong, it should be j < n In the last cycle. for (int i = 0; i < m; i++) Wrong, it should be i < m - 1

k should be removed from the code and replaced with m - 1