I have a two dimensional array and a one dimensional vector. My task is to write a program, that assigns to the i-th component of the vector
- the first element of the i-th row of the matrix, if there is at least one negative element in that row,
- the last element of the i-th row, if there isn't. Here is what I've done.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double matrix[4][4] = {{1.3, -5.0, 4.4, 3.0}, {7.0, 4.0, 2.01, 1.0}, {7.0, 12.3, -8.7, 9.0}, {4.0, 1.0, 33.0, 63.3}};
double vector[4];
for(int i = 0; i < 4; i++) {
bool rowHasNegNum = false;
for(int j = 0; j < 4; i++) {
if (matrix[i][j] < 0) {
rowHasNegNum = true;
}
}
if(rowHasNegNum) {
vector[i] = matrix[i][0];
} else {
vector[i] = matrix[i][3];
}
}
for(int i=0; i<4; i++) {
cout<<vector[i]<<"\t";
}
cout<<endl;
return 0;
}
But when I compile the program, I get the following error. What's the problem?
Segmentation fault (core dumped)
vectoras a variable name is a terrible idea.using namespace std, not the use of a perfectly sensible name.