Jump to content

Compare Signs


Sayuri2009

Recommended Posts

Hi,

In general I am very new in programming. 

Write a program that compares the sign of pairs of 32-bit numbers from two
arrays of numbers and counts how many of them have a different sign.

Now I tried to solve but somehow I got stuck and wondering how to do that? First I have to create two arrays and then I have to do two loops for iterate through each array and compare them and if there is difference between the sign then I have to take in account otherwise I have to leave it and finally if I am finished with comparing the array or if I am reaching the end of the array then I have to return the total count. Is the way of thinking correct?

Array1 = 1 3 4 5 6

Array2 = 3 4 5 6 -3 Count = 1

public int compareSign(int count){

int [] a1 = new Int[]; int [] a2 = new Int[];

for(int i = 0; i < a1.length; i++){ for(int j = 0; j < a2.length; j++){ if(a1 == a2[j]) return } }

}

 

Thanks,

Link to comment
Share on other sites

52 minutes ago, Sayuri2009 said:

In general I am very new in programming. 

In which language do you want to code it? 

52 minutes ago, Sayuri2009 said:

int [] a1 = new Int[]; int [] a2 = new Int[];

You (almost) allocated arrays, but not initialized their fields yet.

new [] takes a parameter, the length of the array, which you did not provide.

Newly allocated memory block has 1) uninitialized data (always forgotten by newbies) 2) initialized to default values (if you allocated array of C++ objects, and default constructor was called, and it filled the all C++ object members by some default value e.g. zero).

52 minutes ago, Sayuri2009 said:

for(int i = 0; i < a1.length; i++){ for(int j = 0; j < a2.length; j++)

So far, so good.

..but it showed possible issue: what if arrays have different lengths..?

52 minutes ago, Sayuri2009 said:

 if(a1 == a2[j])

This way you compare entire object with other object integer e.g. compare their pointers (which will always fail in this case)

52 minutes ago, Sayuri2009 said:

return } }

Why do you want to return here from subroutine?

You should count elements.

Return quantity of matched elements should be at the end of function, outside of for loops.

 

Should not you compare one element from array #1 with one element from array #2 ("pairs of 32-bit numbers from two arrays of numbers")? So arrays should have equal length (ask teacher), and utilize just one for-loop.

 

Shouldn't you be comparing SIGNS of integers?

How to check the sign of a single integer?

How to compare if the signs of two integers are equal?

How to treat zero element? As positive sign?

Edited by Sensei
Link to comment
Share on other sites

In java should be programmed

So I have to init the value of the array everytime? And if I do not know the size of the array?

if the array have different length then I can't compare the array. I have to use one for loop so it means

array [] a1 = new int[4];

array[] a2 = new int[4];

for(int i = 0 ; i < a1.length; i ++){

if a1 = a2{

}

} return count

Shouldn't you be comparing SIGNS of integers?   Yes, I should do that. 

How to check the sign of a single integer? I have to check it with xor and then I have to shift to left to get the MSB if its 0 then its positive otherwise negative

How to compare if the signs of two integers are equal? counter stays the same I don't have to increment

How to treat zero element? As positive sign?  if its zero element then I don't have to compare and return null?

 

Link to comment
Share on other sites

1 hour ago, Sayuri2009 said:

So I have to init the value of the array everytime?

If you won't initialize to some integers, entire procedure of checking integers won't have data to compare them, isn't?

1 hour ago, Sayuri2009 said:

And if I do not know the size of the array?

If you don't know length of the array, you should use dynamic lists/ dynamic arrays instead. Long story, short version: they have no initial length, but grow as developer is appending/inserting elements to them.

1 hour ago, Sayuri2009 said:

if the array have different length then I can't compare the array.

You should *check* if lengths are equal and return error (e.g. throw exception telling about the problem). That would be the nicest/cleanest thing to do.

I would use:

int length = 4;

int [] a1 = new int[ length ];

int [] a2 = new int[ length ];
[...initialize them to some values...]

for( int i = 0; i < length; i++ ) {

 

1 hour ago, Sayuri2009 said:

if a1 = a2{

No, it is wrong! What are you doing here? Do you have any idea?

1 hour ago, Sayuri2009 said:

Shouldn't you be comparing SIGNS of integers?   Yes, I should do that. 

Then compare signs of integers ("element" of array)...

How to read element of an array/list?

1 hour ago, Sayuri2009 said:

How to check the sign of a single integer? I have to check it with xor and then I have to shift to left to get the MSB if its 0 then its positive otherwise negative

Completely wrong answer.

If it would be C/C++ or assembler, you could cast from signed integer, to unsigned integer (so shift operations will work correctly), and then shift RIGHT 31 times (32 bit integer as sign bit at #31 bit), and then use bit-wise AND operation, and then you have "sign bit" which you can compare with something else).

BUT, BUT..

You SHOULD NOT do it this way, as there is 100 times easier way to find out whether integer is positive or negative! Think!

1 hour ago, Sayuri2009 said:

How to compare if the signs of two integers are equal? counter stays the same I don't have to increment

Wrong again. Counter of matching comparisons shall be incremented...

1 hour ago, Sayuri2009 said:

How to treat zero element? As positive sign?  if its zero element then I don't have to compare and return null?

You should not return ANY NULL.

Your procedure have to return an integer of quantity ("counter") of matched comparisons!!

 

Link to comment
Share on other sites

22 hours ago, Sensei said:

If you won't initialize to some integers, entire procedure of checking integers won't have data to compare them, isn't?

If you don't know length of the array, you should use dynamic lists/ dynamic arrays instead. Long story, short version: they have no initial length, but grow as developer is appending/inserting elements to them.

You should *check* if lengths are equal and return error (e.g. throw exception telling about the problem). That would be the nicest/cleanest thing to do.

I would use:

int length = 4;

int [] a1 = new int[ length ];

int [] a2 = new int[ length ];
[...initialize them to some values...]

for( int i = 0; i < length; i++ ) {

 

No, it is wrong! What are you doing here? Do you have any idea?

Then compare signs of integers ("element" of array)...

How to read element of an array/list?

Completely wrong answer.

If it would be C/C++ or assembler, you could cast from signed integer, to unsigned integer (so shift operations will work correctly), and then shift RIGHT 31 times (32 bit integer as sign bit at #31 bit), and then use bit-wise AND operation, and then you have "sign bit" which you can compare with something else).

BUT, BUT..

You SHOULD NOT do it this way, as there is 100 times easier way to find out whether integer is positive or negative! Think!

Wrong again. Counter of matching comparisons shall be incremented...

You should not return ANY NULL.

Your procedure have to return an integer of quantity ("counter") of matched comparisons!!

 

To your question How to read element of an array/list?

I have to iterate through the array list. That means I need a for or while loop. 

whether integer is positive or negative!

if(number > 0 )  this for positiv

if(number < 0) this for negativ

 

 

Link to comment
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
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.