Jump to content

applying a condition on a 2D array C++

alphabeta

a 2D array like that      * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

 

I want to apply a condition that on row or column 2 that is it's equal 2 it will print "YES" for example 

 

                                    * * * * * * * * * * * *         

                                    2 2 2 2 2 2 2 2 2 

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

 

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

Link to comment
Share on other sites

Link to post
Share on other sites

I can help you if you explain what you want to do...I didn't understand what is the problem.

Btw I hope this is not your assignment

 C++,Assembly,Reverse Engineering,Penetration testing,Malware analysis


 


Do you have a question?Are you interested in programming?Do you like solving complex problems?Hit me up with a pm.

Link to comment
Share on other sites

Link to post
Share on other sites

I can help you if you explain what you want to do...I didn't understand what is the problem.

Btw I hope this is not your assignment

 

it's just a part of student activity in my college 

 

and about the problem I want to apply an if() condition on a whole row or column in a 2D array 

Link to comment
Share on other sites

Link to post
Share on other sites

Pseudo code:

 

Let "a" be your array.

 

a[x][y]

 

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

{

    for (int j=0;j<y;j++)

    {

        if(a[x][y] == 2)

        {

          *Do something*

        }

    }

}

 

 

 

Do you learn this stuff on college?Lol?I knew this stuff when I was 12....

Tip:

Don't ask for answers , search for answers!

 C++,Assembly,Reverse Engineering,Penetration testing,Malware analysis


 


Do you have a question?Are you interested in programming?Do you like solving complex problems?Hit me up with a pm.

Link to comment
Share on other sites

Link to post
Share on other sites

well for the row its pretty easy, just keep checking the element of the array and move onto the next.  If the element is not 2, then return false. if it is at the end of list return true. If it is 2 then recheck on next element.

 

As for the columns, if its just this specific column, its the same idea instead you check in the next array but same index. Though if this is just a specific case, and you want to be more general, then going through each column is slower(if you ever printed an array by column then row, instead of by row then column, you would see the performance differences).

Link to comment
Share on other sites

Link to post
Share on other sites

Pseudo code:

 

Let "a" be your array.

 

a[x][y]

 

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

{

    for (int j=0;j<y;j++)

    {

        if(a[x][y] == 2)

        {

          *Do something*

        }

    }

}

 

 

 

Do you learn this stuff on college?Lol?I knew this stuff when I was 12....

Tip:

Don't ask for answers , search for answers!

 

still a freshman dude -_- and when I was 12 I had some important things to do 

Link to comment
Share on other sites

Link to post
Share on other sites

still a freshman dude -_-

Work hard.I remember when I was first introduced to a free coding class(C++) that the government did for students,I went there on the middle of the year not knowing a shit.After a month I was better from the teacher.

 

Make programming a passion...

 

EDIT-I downloaded every possible tutorial I could find on the web and here I am....

 C++,Assembly,Reverse Engineering,Penetration testing,Malware analysis


 


Do you have a question?Are you interested in programming?Do you like solving complex problems?Hit me up with a pm.

Link to comment
Share on other sites

Link to post
Share on other sites

Work hard.I remember when I was first introduced to a free coding class(C++) that the government did for students,I went there on the middle of the year not knowing a shit.After a month I was better from the teacher.

 

Make programming a passion...

 

it's a passion I start it while I was 15 but then the revolution happened and it just stopped at loops 

Link to comment
Share on other sites

Link to post
Share on other sites

still a freshman dude -_- and when I was 12 I had some important things to do 

www.cplusplus.com

 

Have fun

 C++,Assembly,Reverse Engineering,Penetration testing,Malware analysis


 


Do you have a question?Are you interested in programming?Do you like solving complex problems?Hit me up with a pm.

Link to comment
Share on other sites

Link to post
Share on other sites

it's a passion I start it while I was 15 but then the revolution happened and it just stopped at loops 

 

Seems like excuse

 

 

Work hard.

 

This

Link to comment
Share on other sites

Link to post
Share on other sites

Seems like excuse

 

 

 I'm able to prove that it isn't But I won't bother you with unnecessary story 

 

 

This

 

Work hard.

 

 

 

This's what I'm doing right now actually 

Link to comment
Share on other sites

Link to post
Share on other sites

I want to apply a condition that on row or column 2 that is it's equal 2 it will print "YES" for example 

 

                                    * * * * * * * * * * * *         

                                    2 2 2 2 2 2 2 2 2 

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

 

char IsTwo(Array, Row, Width) //returns 0 if 2

{

    for (int i = 0; i < Width; i++

    {

        if (Array[i + Row*Width] != 2) //for 1d array or if (Array[i,Row] != 2) for 2d array or if (Array[Row] != 2) for jagged array

            return -1;

    }

    return 0;

}

 

I want to apply a condition that on row or column 2 that is it's equal 2 it will print "YES" for example 

 

                                    * * * * * * * * * * * *         

                                    2 2 2 2 2 2 2 2 2 

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

                                    * * * * * * * * * * * *

 

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

                                    * 2 * * * * * * * * * *

 

char IsTwo(Array, Column, Width) //returns 0 if 2

{

    for (int i = 0; i < Width; i++

    {

        if (Array[column + i*Width] != 2) //for 1d array or if (Array[Column, i] != 2) for 2d array or if (Array[column] != 2) for jagged array

            return -1;

    }

    return 0;

}

 

edit: somehow I managed to hit send prematurely then knock the power cable out of my computer.

 

 

 

it's a passion I start it while I was 15 but then the revolution happened and it just stopped at loops 

Wait are you saying you never made it far enough into programming to learn loops or that you started with Assembly/Scheme/etc. that doesn't have loops?
 
Loops were one of the first things I ever learned when programming.
I assume it wasn't assembly because loops are pretty straight forward from jump (goto) statements. 
Link to comment
Share on other sites

Link to post
Share on other sites

 

 
Wait are you saying you never made it far enough into programming to learn loops or that you started with Assembly/Scheme/etc. that doesn't have loops?
 
Loops were one of the first things I ever learned when programming.
I assume it wasn't assembly because loops are pretty straight forward from jump (goto) statements. 

 

 

What I'm saying that all I knew by that time was if conditions and while loops in C++ just those two and my english was as bad as hell so searching and learning online was not an option.

Link to comment
Share on other sites

Link to post
Share on other sites

What I'm saying that all I knew by that time was if conditions and while loops in C++ just those two and my english was as bad as hell so searching and learning online was not an option.

Where are you from? Aren't there anything in your language? I bet you would find something. If you know how ifs and loops work all you need was to find out how to create and access 2D arrays.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×