Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:58:09+00:00 2026-06-07T15:58:09+00:00

I have to do an exercise for University that asks me to check (

  • 0

I have to do an exercise for University that asks me to check ( for k times ) if a matrix has positive rows ( i mean a row with all positive elements ) , i think there’s something wrong with the indices of for loops but i cannot find the mistakes.

i tried to debug with a cout statement apply to the counter an it gives me “101” , so it seems like compiler assign “1” to the positive rows and “0” to the negative

This is the code i wrote:

#include <iostream>

using namespace std;

const int N = 3;

bool positive(int a[N][N], int row[N], int x) {
    bool condition = false;

    for(int i = 0; i < N; i++) {
        row[i] = a[x][i];
    }

    int j = 0;

    while(j < N) {
        if(row[j] >= 0) {
            condition = true;
        } else {
            condition = false;
        }

        j++;

    }

    return condition;
}


bool function (int a[N][N], int z, int j, int k) {
    int b[N];
    int c[N];
    int count = 0;

    if(positive(a, b, z) && positive(a, c, j)) {
        count++;
    }

    if(count == k) {
        return true;
    } else {
        return false;
    }
}


int main() {
    int a[N][N] = {
        {
            2, 8, 6
        }, {
            -1, - 3, - 5
        }, {
            6, 10, 9
        }
    };

    int k = 2;

    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            if(function (a, i, j, k)) {
                cout << "OK";
            } else {
                cout << "NO";
            }

        }
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:58:12+00:00Added an answer on June 7, 2026 at 3:58 pm

    You should probably take another look at this problem and restart with a different solution. The objective is pretty easy but your code is surprisingly complex and some of it doesn’t really make sense.

    For example, if you had a matrix like this:

        1   2   4    --> matrix A
       -1   8  -6
        3   9   2
    

    You have N=3 rows and columns. The only thing you have to to based on what you said is take the matrix, cycle through the N rows, and for each row, check it’s N columns to see if anything is < 0.

    Doing this K times, as you put it, makes no sense. The matrix will be the same every time you compare it since you’re not changing it, why would you do it more than once? I think you should reread the assignment brief there.

    As for the logic of finding which rows are positive or negative, just do something simple like this.

    //Create array so we have one marker for each row to hold results.
    bool rowPositiveFlags[N] = { true, true, true };
    
    //Cycle through each row.
    for (int row = 0; row < N; ++row)
        //Cycle through every column in the current row.
        for (int col = 0; col < N; ++col)
            //If the column is negative, set the result for this row to false and break
            //the column for loop as we don't need to check any more columns for this row.
            if (A[row][col] < 0) {
                rowPositiveFlags[row] = false;
                break;
            }
    

    You should always name things so that you can read your code like a book. Your i’s, j’s, and k’s just make something simple confusing. As for the problem, just plan out your solution.

    Solve the problem by hand on paper, write the steps in comments in your code, and then write code below the comments so what you do definitely makes sense and isn’t overkill.


    And this is a great site, but next time, post a smaller piece of code that shows your problem. People shouldn’t ever give you a full solution here for homework so don’t look for one. Just find the spot where your indices are broken and paste that set of 5 lines or whatever else is wrong. People appreciate that and you’ll get faster, better answers for showing the effort 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

During my university exercise I have come across strange behaviour of a variable. /*
i have to crawl last.fm for users (university exercise). I'm new to python and
I have to implement an algorithm that generates a timetable for a university. I've
I am studying the SCJP, and while studying I have found an exercise that
I have an exercise in problem solving for those who like that kind of
I have this small exercise that I am trying to finish, but I get
I have the following exercise: Define a class named circle that accepts objects of
I have been making a matrix class (as a learning exercise) and I have
For a class exercise on game trees , I have to write code that
I have a textbook exercise right after looping and using char values (meaning that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.