Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
//Task : There is given square two-dimensional array: Find the biggest negative element and its array adress. 

#include < iostream>        
#include < iomanip>
#include < string>
using namespace std;
int main()
{
    int a[5][5];
    int did,n,m,i,j;

    cout<<"Type array measurements(max 5):" <<endl;
    cin>>m>>n;
    cout<<"Type array elements" <<endl;

    for(i=0;i<m;i++)
        for(j=0;j<n;++j)
            cin>>a[i][j];
    
    did = a[0][0];
    for (i=0; i < m; ++i)
    {
        for (j=0; j < n; ++j)
        {
            if (a[i][j] > did && a[i][j] < 0)
                did = a[i][j];
            i = m;
            j = n;
        }
    }

    cout <<"Biggest negative element is " << did << endl;
    cout <<"Its array adress: " << m <<";" << n <<endl;
    system("pause");
    return 0;
}
Posted
Updated 29-Nov-15 13:33pm
v3
Comments
Sergey Alexandrovich Kryukov 29-Nov-15 11:37am    
Sorry, but this is not a question. Moreover, the formulation of the problem does not make sense. Without formulation of the the goals, a piece of code cannot be wrong or right; it can be wrong for one purpose and right for another one. If the behavior of your code does not match what you expected, use the debugger and fix it. If you failed to do so, describe what did you want to achieve, what's expected behavior, what is observed behavior, why do you feel it's wrong. If you have some exceptions, describe steps to reproduce, complete exception information. Comment the points in code related to exception throwing/propagation. Post all what's relevant. See also: http://www.sscce.org.
—SA
George Jonsson 29-Nov-15 19:38pm    
I formatted the code so it is a bit more easy to read.
Now it should be pretty easy to find the problem.
Hint: How do you check if a number is more negative than another number?
Do you use < or >?

You need to make a little change in your code.
First take two variable to save the Index.

C#
int nI,nJ;
if (a[i][j] < did && a[i][j] < 0){ //check if value less than did and 0.
      did = a[i][j];
      nI = i;        // save index of i.
      nJ = j;        //save index of j.
}



Earlier you were doing :
i = m; // here no matter what the condition is.
j = n; // it will always assign the same value of m and n every time.
As value of m and n are not changing in whole program so you will get the same value always.
as m and n are the size of array and you are assigning it to i and j respectively. it will reach end of the loop. so it comes out of the for loop.
 
Share this answer
 
v2
Comments
CPallini 1-Dec-15 6:20am    
My 5.
Note you can even get rid of did variable, in your code.
Member 10641779 1-Dec-15 6:34am    
thanks for your and suggestion.
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.

Advices:
- Use braces {} everywhere it should be even if optional.
- use indentation, it makes your program easier to read and gives hints on what you intend to do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900