0

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

  1. the first element of the i-th row of the matrix, if there is at least one negative element in that row,
  2. 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)
7
  • 4
    unrelated, but using vector as a variable name is a terrible idea. Commented Feb 4, 2021 at 9:32
  • @KamilCuk thank you so much, I corrected it and my program worked as expected :) Commented Feb 4, 2021 at 9:35
  • @M.A thanks for advice, indeed... Commented Feb 4, 2021 at 9:35
  • 1
    @M.A strongly disagreed. It can be an entirely appropriate variable name. Commented Feb 4, 2021 at 10:32
  • 1
    @M.A The issue in that case is unambiguously the using namespace std, not the use of a perfectly sensible name. Commented Feb 4, 2021 at 11:22

3 Answers 3

4

The fault is here for (int j = 0; j < 4; i++). Your not incrementing j, instead your incrementing i. And since j is not getting incremented, i gets incremented passing the bounds of the array. This is the reason for the segmentation fault.

The fix is simple, just swap i with j here,

for (int j = 0; j < 4; j++)
Sign up to request clarification or add additional context in comments.

Comments

3

For starters neither declaration from this header

#include <cmath>

is used in the program. So remove it.

Do not use magic numbers like 4 or 3 in these statements

for(int i = 0; i < 4; i++)

ector[i] = matrix[i][3];

Instead use named constants.

In this for loop

 for(int j = 0; j < 4; i++) {
                       ^^^

there is a typo. You need to write

 for(int j = 0; j < 4; j++) {
                       ^^^

This for loop

        bool rowHasNegNum = false;
        for(int j = 0; j < 4; j++) {
            if (matrix[i][j] < 0) {
                rowHasNegNum = true;
            }
        }

should be interrupted as soon as a negative number is found.

Also instead of this for loop you could use the standard algorithm std::any_of.

In this case your program could look the following way

#include <iostream>
#include <functional>
#include<iterator>
#include<algorithm>

int main() 
{
    const size_t N = 4;
    double matrix[N][N] = 
    {
        { 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[N];
    
    size_t i = 0;
    
    for ( const auto &row :matrix )
    {
        using std::placeholders::_1;
        if ( std::any_of( std::begin( row ), std::end( row ), 
                          std::bind( std::less<>( ), _1, 0 ) ) )
        {
            vector[i++] = row[0];
        }
        else
        {
            vector[i++] = row[N-1];
        }
    }
    
    for ( const auto &item : vector )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

The program output is

1.3 1 7 63.3 

If to use only loops then the program can look the following way

#include <iostream>

int main() 
{
    const size_t N = 4;
    double matrix[N][N] = 
    {
        { 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[N];
    
    for ( size_t i = 0; i < N; i++ )
    {
        bool rowHasNegNum = false;  
        
        for ( size_t j = 0; not rowHasNegNum && j < N; j++ )
        {
            rowHasNegNum = matrix[i][j] < 0;
        }
        
        vector[i] = rowHasNegNum ? matrix[i][0] : matrix[i][N-1];       
    }
    
    for ( const auto &item : vector )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

Comments

3
 for(int j = 0; j < 4; i++) {

You are incrementing the wrong variable here. A compiler warning picked this up, you should make sure you have warnings enabled.

And by incrementing i in this loop, you are accessing values outside the array resulting in the segmentation fault.

2 Comments

Thanks, my program worked. How can I enable warnings? I'm using an online compiler
@ArmanGrigoryan depends on which you use, you should be able to set the compile flags somewhere where you can add -Wall which roughly stands for warnings all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.