Arrays and Bitwise

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 6
Reputation: Gusts is an unknown quantity at this point 
Solved Threads: 0
Gusts Gusts is offline Offline
Newbie Poster

Arrays and Bitwise

 
0
  #1
Jul 3rd, 2008
So I got such exercise:
You have array of 1000 elements they all are random numbers from 1 to 1000 and there is only one duplicate number. You can access each array member only once, can you find duplicate value? And you can't create another array. You can create some variables to store temporary data.

and how divide number by 7 using bitwise, how to divide by 8 i know it only takes shift to left by 3 i.e. number << 3, but how divide by 7?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster

Re: Arrays and Bitwise

 
0
  #2
Jul 3rd, 2008
The first problem is easy
I'll give you the code for it
  1. #include <iostream>
  2. #include <cstdlib>
  3. int main()
  4. {
  5. int A[1000];
  6.  
  7. // for loop to fill the array with random elements between 1 to 1000
  8. for( int i = 0; i < 1000; i++)
  9. {
  10. A[i] = 1 + rand() %1000;
  11. }
  12.  
  13. // for loop to find the duplicate integer
  14. for( int j = 0; j < 1000; j++ )
  15. {
  16. if( A[j] == A[j])
  17. cout << "the duplicate integer found " << A[j];
  18. }
  19.  
  20. return 0;
  21.  
  22. } // end main
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: Gusts is an unknown quantity at this point 
Solved Threads: 0
Gusts Gusts is offline Offline
Newbie Poster

Re: Arrays and Bitwise

 
0
  #3
Jul 3rd, 2008
That doesn't make sense, because A[j] == A[j] you are comparing same values, so they always will be equal...

Originally Posted by Q8iEnG View Post
The first problem is easy
I'll give you the code for it
  1. #include <iostream>
  2. #include <cstdlib>
  3. int main()
  4. {
  5. int A[1000];
  6.  
  7. // for loop to fill the array with random elements between 1 to 1000
  8. for( int i = 0; i < 1000; i++)
  9. {
  10. A[i] = 1 + rand() %1000;
  11. }
  12.  
  13. // for loop to find the duplicate integer
  14. for( int j = 0; j < 1000; j++ )
  15. {
  16. if( A[j] == A[j])
  17. cout << "the duplicate integer found " << A[j];
  18. }
  19.  
  20. return 0;
  21.  
  22. } // end main
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Arrays and Bitwise

 
0
  #4
Jul 3rd, 2008
can you use std::sort(A, A+1000)
and if A[j] == A[j+1] cout << A[j]
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: Gusts is an unknown quantity at this point 
Solved Threads: 0
Gusts Gusts is offline Offline
Newbie Poster

Re: Arrays and Bitwise

 
0
  #5
Jul 3rd, 2008
Well that might be solution, but i'm not sure. Maybe there is some way without sorting ?

And i made mistake, i was wondering how to multiply number by 7 not divide.

Originally Posted by ivailosp View Post
can you use std::sort(A, A+1000)
and if A[j] == A[j+1] cout << A[j]
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Arrays and Bitwise

 
0
  #6
Jul 3rd, 2008
And i made mistake, i was wondering how to multiply number by 7 not divide.
  1. int x = 21;
  2. x = (x << 2) + (x << 1) + x; //multiply number by 7
  3. cout << x;
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: Gusts is an unknown quantity at this point 
Solved Threads: 0
Gusts Gusts is offline Offline
Newbie Poster

Re: Arrays and Bitwise

 
0
  #7
Jul 3rd, 2008
I'm really distracted today, there was mentioned that i can't use addition or subtraction

Originally Posted by ivailosp View Post
  1. int x = 21;
  2. x = (x << 2) + (x << 1) + x; //multiply number by 7
  3. cout << x;
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Arrays and Bitwise

 
0
  #8
Jul 3rd, 2008
First question is interesting one, I researched on it, but the below mentioned link has figured out some really good stuff regarding your first question.

http://aperiodic.net/phil/archives/G...-elements.html.

will try your 2nd question tomorrow .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Arrays and Bitwise

 
0
  #9
Jul 3rd, 2008
how to sum numbers...
it was hard to write this :/
EDIT: small fix
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sum_numbers(int x, int y) {
  5. while(y = (x&y) << ((x ^= y)?1:1));
  6. return x;
  7. }
  8.  
  9. int main() {
  10. int x = 24;
  11. int y = 3;
  12. for(;y < 100;++y)
  13. cout << sum_numbers(x, y) << '-';
  14. return 0;
  15. }
Last edited by ivailosp; Jul 3rd, 2008 at 11:46 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Arrays and Bitwise

 
0
  #10
Jul 3rd, 2008
and here is the file code for multiply number by 7

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sum_numbers(int x, int y) {
  5. while(y = (x&y) << ((x ^= y)?1:1));
  6. return x;
  7. }
  8.  
  9. int multiply_by_seven(int x){
  10. return sum_numbers(sum_numbers((x << 2), (x << 1)), x);
  11. }
  12.  
  13. int main() {
  14. for (int i = 0; i <= 100; ++i){
  15. cout << multiply_by_seven(i) << endl;
  16. }
  17. return 0;
  18. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC