| | |
Arrays and Bitwise
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jun 2008
Posts: 130
Reputation:
Solved Threads: 2
The first problem is easy
I'll give you the code for it
I'll give you the code for it

C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> int main() { int A[1000]; // for loop to fill the array with random elements between 1 to 1000 for( int i = 0; i < 1000; i++) { A[i] = 1 + rand() %1000; } // for loop to find the duplicate integer for( int j = 0; j < 1000; j++ ) { if( A[j] == A[j]) cout << "the duplicate integer found " << A[j]; } return 0; } // end main
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
That doesn't make sense, because A[j] == A[j] you are comparing same values, so they always will be equal...
•
•
•
•
The first problem is easy
I'll give you the code for it
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> int main() { int A[1000]; // for loop to fill the array with random elements between 1 to 1000 for( int i = 0; i < 1000; i++) { A[i] = 1 + rand() %1000; } // for loop to find the duplicate integer for( int j = 0; j < 1000; j++ ) { if( A[j] == A[j]) cout << "the duplicate integer found " << A[j]; } return 0; } // end main
•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
•
•
•
•
And i made mistake, i was wondering how to multiply number by 7 not divide.
cpp Syntax (Toggle Plain Text)
int x = 21; x = (x << 2) + (x << 1) + x; //multiply number by 7 cout << x;
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
I'm really distracted today, there was mentioned that i can't use addition or subtraction
•
•
•
•
cpp Syntax (Toggle Plain Text)
int x = 21; x = (x << 2) + (x << 1) + x; //multiply number by 7 cout << x;
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
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
.
http://aperiodic.net/phil/archives/G...-elements.html.
will try your 2nd question tomorrow
. •
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
how to sum numbers...
it was hard to write this :/
EDIT: small fix
it was hard to write this :/
EDIT: small fix
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int sum_numbers(int x, int y) { while(y = (x&y) << ((x ^= y)?1:1)); return x; } int main() { int x = 24; int y = 3; for(;y < 100;++y) cout << sum_numbers(x, y) << '-'; return 0; }
Last edited by ivailosp; Jul 3rd, 2008 at 11:46 am.
•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
and here is the file code for multiply number by 7
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int sum_numbers(int x, int y) { while(y = (x&y) << ((x ^= y)?1:1)); return x; } int multiply_by_seven(int x){ return sum_numbers(sum_numbers((x << 2), (x << 1)), x); } int main() { for (int i = 0; i <= 100; ++i){ cout << multiply_by_seven(i) << endl; } return 0; }
![]() |
Similar Threads
- Bitwise: Convert Primitives (Java)
- hex string to true binary w. bit manipulation (Java)
- I Need Help (C++)
- Adding mouse listener to a frame (Java)
- help with errors (C++)
Other Threads in the C++ Forum
- Previous Thread: Converting std::string to System::String^ NEED HELP
- Next Thread: Exception in Visual C++
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





