Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
2.60/5 (5 votes)
See more:
This is my question:
Find all natural numbers not exceeding a given m, the binary notation which is a symmetrical sequence nudey and ones (starting from one). Show decimal and binary records of these numbers.

I am new to programming in C++. I've been trying to solve this question but haven't been successful.

int m;
cout<<"Enter the starting number: ";
cin>>m;

while(m>0)
{
  cout<<m<<"   ";
  --m;
}

This gave me what i needed.

Now I need to get the binary form of these numbers and show out the ones that are symmetrical.
i.e for example 1001.
After which the ones that fall into the category will be changed back decimal and shown on the monitor.

I will be very grateful if some could help me out.
Thank you in anticipation for your response.
Posted
Updated 3-Sep-12 2:04am
v3
Comments
pasztorpisti 2-Sep-12 21:33pm    
And what have you tried so far? What is the first obstacle you couldn't get over?
karochi123 2-Sep-12 21:59pm    
my first obstacle was creating a function that detect the symmetrical numbers. The symmetrical numbers are to be in binary form and are to start with '1' and end with '1'. for example 1001.
pasztorpisti 3-Sep-12 3:16am    
Depending on the maximum value of m you might not want to detect but construct a symmetric value.
[no name] 2-Sep-12 21:37pm    
Did your teacher happen to mention what "symmetrical sequence nudey" is supposed to mean?
pasztorpisti 2-Sep-12 21:40pm    
Tried to search for nudey in a dictionary and with google but all I found was "nudey beach" :-) I guess it has something to do with the trailing zeros in the binary representation...

Check out the following api's for easy converting to binary and to strings :

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/[^]

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/[^]

Now how go about this :
- user enters a number, this number is the maximum number, you only want numbers that are binary symetrical below that number.
- now you need a loop where you will start with say i and initialise it to 1
- convert i to a binary string using above api
- copy the result binary string in another string, this string must be big
- keep appending to this string from the first binary string, but from the back of the string to the beginning. You now have a symetrical string
- convert it to a number.
- if the number is bigger than what the user enters, loop should stop, otherwise print the number and binary representation.

This should get you started with enough information to get the job done I think.
 
Share this answer
 
Starting from the string of the binary number (which you said you have), create two pointers pointing to (1) the start of the binary string and (2) the last character of that same string. Make a loop, and inside that loop compare the characters the two pointers are pointing to, then move the first pointer to the next, and the end pointer to the previous character. Repeat until either you find a mismatch, or the first pointer moves past the end pointer.

Note, when I say pointer, nothing stops you from using a std::string and iterators instead. In that case however comparing the iterators may be tricky (feel free to ask again if you can't solve it)
 
Share this answer
 
Comments
karochi123 9-Sep-12 12:29pm    
#include "stdafx.h"
#include <iostream>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;

int _tmain(int argc, _TCHAR* argv[])
{

int m;
cout<<"Enter a number: ";
cin>>m;

while (m>0)
{
cout<<m<<" ";

int num, rem, i =1, total = 0;
num = m;

do

{
rem=num%2;
total=total +(i*rem);
num/=2;
i=i*10;

}
while (num>0);
cout<<total<<endl;
--m;
}


return 0;
}

This is what i have been able to do. how do i use the pointers now to compare and finish up the project?
Stefan_Lang 10-Sep-12 5:57am    
You should try and fix your code first:
1. there are numerous opening and closing braces missing
2. there is no variable to hold the result, or at least no variable of a proper type for that purpose (array of char or std::string)
3. You are mixing base 10 and base 2 conversion in the two lines
num/=2;
i=i*10;


You are suffering from attention splitting: you have all the things you wish to do in your head and try to solve it all at the same time. As a result your code mixes up parts of the solutions, resulting in a jumble of code that is hard to follow, understand, and fix.

I suggest you split up the individual steps into functions. That will allow you to better focus on each step without distraction. It will also result in a better code structure.

Start with this:
#include <io>
int requestNumber() {
// request and read a number from standard input
int result = 0;
// ask for input, read, and check that it's ok
// ...

return result;
}
std::string convertToBinary(int m) {
// convert a number into a binary string representation
std::string result;
// iterate over the bits of m and store them as '1' and '0' in result
// ...

return result;
}
bool checkPalindrome(std::string str) {
// test a string for being a palindrome
return result = true;
// iterate over string until you find a non-symmetric part, or you are done
// ...

return result;
}
int _tmain(int c, TCHAR* v[]) {
int result = 0;
// call each function and print intermediate results
// ...

return result;
}

Another hint: compile and run this regularly, to make sure you have no syntax errors, and to check at which step of the program your code fails to deliver the expected results.

You may notice that I defined a 'result' variable in each and every function and initialized it to some value. It is a practice I keep for various reasons, but mostly because it's very easy to recognize as the variable for storing a functions' final result. Also, it's easy to check whether a function result is equal to its initial value when you know that it's always initialized in the first line of any function.

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



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