Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

i really need help in a palindrome and binary conversion program..

hi..
im trying to build a program using arrays to do the following things:
1- convert a digital number to binary using mathematical operators by using the function void binaryConversionUsingArithematicOperators(unsigned long)
2- convert a digital number to binary using bitwise operators by using the function void binaryConversionUsingBitwiseOperators(unsigned long)
3- check if the word is a palindrome using iteration by using this function
void bool isPalindromeUsingIteration(string)
4- check if the word is a palindrome using recursion by using this function
void bool isPalindromeUsingRecursion(string)

here is what i wrote:

#include<iostream>
#include<string>
#include<cctype>
using std::string;
using std::cout;
using std::cin;
using std::endl;

// Here are some function declarations

unsigned short getMenuSelection( );
void binaryConversionUsingArithmeticOperators(unsigned long);
void binaryConversionUsingBitwiseOperators (unsigned long);
void isPalindromeUsingIteration(string);
void isPalindromeUsingRecursion(string);

// Utilityfunctions declarations
void initializeArray(unsigned short[],int);
void initializeArray(string[],int);
void printBinary(unsigned short[],int);
void printBinary(string[],int);

int main()
{
unsigned short choice;
do
{
choice = getMenuSelection();

cout <<"You chose Menu Option #"<<choice<<endl;
switch(choice)
{
case 1 :
unsigned long a;
cout<<" Please enter an integer ";
cin>>a;
binaryConversionUsingArithmeticOperators(a);
break;
case 2 :
cout<<" Please enter an integer ";
cin >>a;
binaryConversionUsingBitwiseOperators(a);
break;
case 3 :
string b;
cout<<"Please enter a string ";
cin>>b;
isPalindromeUsingIteration(b);
break;
case 4:
string b;
cout<<"Please enter a string ";
cin>>b;
isPalindromeUsingIteration(b);
break;
case 5:
break;
default:
cout<<"you should never get here\a\a\a\a!!!";
}
}while(choice != 5);
cout<<"you choose to quit\a\a\a"<<endl;
return 0;
}

//function definitions
unsigned short getMenuSelection()
{
unsigned short selection=0;
do
{
cout<<"-1- Binary Conversion using Arithmetic Operators"<<endl;
cout<<"-2- Binary Conversion using Bitwise Operators"<<endl;
cout<<"-3- Testing for Palindroms Using Iteration"<<endl;
cout<<"-4- Testing for Palindroms Using Recursion"<<endl;
cout<<"-5- Quit"<<endl;
cout<<"please enter your selection <1-5>:"<<endl;
cin>>selection;
}
while(selection<1 || selection>5);
return selection;
}
void binaryConversionUsingArthematicOperators(unsigned long x)
{
unsigned short binaryDigit[100],
numberOfDigits=0;
initializeArray(binaryDigit,100);
do
{
binaryDigit[numberOfDigits++]=x%2;
x=x/2;
}while(x!=0);
printBinary(binaryDigit,numberOfDigits);
}

void binaryConversionUsingBitwiseOperator(unsigned long x)
{
unsigned short binaryDigit [100],
numberOfDigits=0;
initializeArray(binaryDigit , 100);
do
{
binaryDigit[numberOfDigits++]=x&1;
x=x>>1;
}while(x!=0);
printBinary(binaryDigit, numberOfDigits);
}

void isPalindromeUsingIteration(string y)
{
while (length>1)
{
if ( y[] != [y + length - 1])
{
return false;
}
++y;
length=length-2;
}
return true;
}
void isPalindromeUsingRecursion(string y)
{
if ( length<2 )
{
return true;
}
else
{
if ( y[] != [y + length - 1])
{
return false;
}
else
{
return ispalindromeUsingRecursion(string y+1);
}
}

}
//utality function definitions
void initializeArray (unsigned short a[], int size)
{
for(int i=0;i<size; i++)
a[i]=0;
}

void printBinary(unsigned short a[], int size)
{
for(int i=size-1; i>=0; i--)
cout<<a[i];
cout<<endl;
}
void initializeArray (string b[], int length)
{
for(int i=0;i<length; i++)
b[i]=b;
}

void printBinary(string b[], int length)
{
for(int i=length-1; i>=0; i--)
cout<<b[i];
cout<<endl;

but there are 22 errors!!! i really need some help here..
[4671 byte] By [elsa] at [2007-11-11 10:13:24]
# 1 Re: i really need help in a palindrome and binary conversion program..
In isPalindromeUsingIteration() function;
u r using the variable : "length" without declare it as global variable or inside the function .
in the same function , in the if condition;
[y + length - 1]
not all brackets are allowed like math :
(y + length - 1) // u must use every time the "(" and ")"

in the same function inside the if condition :
return false;
how return false while the function is void ?

same for this : return true;

++y ; no defined operation that increase or use this operator with strings .

return ispalindromeUsingRecursion(string y+1);
if u r trying to make recursion don't use the "return" just call the function directly .
and don't specify the type when calling the function , it should be :
ispalindromeUsingRecursion(y+1);

b[i]=b;
have no mean at all : assign a string to a specific char indexed by i ?

finally u missed the "}" at the end .
hope this helps ; after correcting those all , if u got more errors send them here .
Amahdy at 2007-11-11 20:59:17 >
# 2 Re: i really need help in a palindrome and binary conversion program..
What are the errors you're getting? Leraning what compiler errors actually mean in an important step in learning C++ or any other language. Your code has a few bugs and syntactic errors but let's do it systematically. Post the errors, or at least some of them.
Danny at 2007-11-11 21:00:18 >
# 3 Re: i really need help in a palindrome and binary conversion program..
thx alot..now there are 17 more errors to go..lol
the errors i get are the following:
error C2360: initialization of 'b' is skipped by 'case' label
(45) : see declaration of 'b'
(51) : error C2086: 'b' : redefinition
(56) : error C2360: initialization of 'b' is skipped by 'case' label
(51) : see declaration of 'b'
(56) : error C2360: initialization of 'b' is skipped by 'case' label
(45) : see declaration of 'b'
(58) : error C2361: initialization of 'b' is skipped by 'default' label
(51) : see declaration of 'b'
(58) : error C2361: initialization of 'b' is skipped by 'default' label
(45) : see declaration of 'b'
(114) : error C2059: syntax error : ']'
(115) : error C2143: syntax error : missing ';' before '{'
(118) : error C2675: unary '++' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this opera
tor or a conversion to a type acceptable to the predefined operator
(131) : error C2059: syntax error : ']'
(132) : error C2143: syntax error : missing ';' before '{'
(135) : error C2181: illegal else without matching if
(137) : error C2065: 'ispalindromeUsingRecursion' : undeclared identifier
(137) : error C2275: 'string' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\xstring(612) : see declaration of 'string'

and the program is now as this:

#include<iostream>
#include<string>
#include<cctype>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int length;
// Here are some function declarations

unsigned short getMenuSelection( );
void binaryConversionUsingArithmeticOperators(unsigned long);
void binaryConversionUsingBitwiseOperators (unsigned long);
void isPalindromeUsingIteration(string);
void isPalindromeUsingRecursion(string);

// Utilityfunctions declarations
void initializeArray(unsigned short[],int);
void initializeArray(string[],int);
void printBinary(unsigned short[],int);
void printBinary(string[],int);

int main()
{
unsigned short choice;
do
{
choice = getMenuSelection();

cout <<"You chose Menu Option #"<<choice<<endl ;
switch(choice)
{
case 1 :
unsigned long a;
cout<<" Please enter an integer " ;
cin>>a ;
binaryConversionUsingArithmeticOperators(a);
break ;
case 2 :
cout<<" Please enter an integer " ;
cin >>a ;
binaryConversionUsingBitwiseOperators(a) ;
break ;
case 3 :
string b;
cout<<"Please enter a string ";
cin>>b;
isPalindromeUsingIteration(b);
break;
case 4:
string b;
cout<<"Please enter a string ";
cin>>b;
isPalindromeUsingIteration(b);
break;
case 5:
break;
default:
cout<<"you should never get here\a\a\a\a!!!";
}
}while(choice != 5);
cout<<"you choose to quit\a\a\a"<<endl;
return 0;
}

//function definitions
unsigned short getMenuSelection()
{
unsigned short selection=0;
do
{
cout<<"-1- Binary Conversion using Arithmetic Operators"<<endl;
cout<<"-2- Binary Conversion using Bitwise Operators"<<endl;
cout<<"-3- Testing for Palindroms Using Iteration"<<endl;
cout<<"-4- Testing for Palindroms Using Recursion"<<endl;
cout<<"-5- Quit"<<endl;
cout<<"please enter your selection <1-5>:"<<endl;
cin>>selection;
}
while(selection<1 || selection>5);
return selection;
}
void binaryConversionUsingArthematicOperators(unsigned long x)
{
unsigned short binaryDigit[100],
numberOfDigits=0;
initializeArray(binaryDigit,100);
do
{
binaryDigit[numberOfDigits++]=x%2;
x=x/2;
}while(x!=0);
printBinary(binaryDigit,numberOfDigits);
}

void binaryConversionUsingBitwiseOperator(unsigned long x)
{
unsigned short binaryDigit [100],
numberOfDigits=0;
initializeArray(binaryDigit , 100);
do
{
binaryDigit[numberOfDigits++]=x&1;
x=x>>1;
}while(x!=0);
printBinary(binaryDigit, numberOfDigits);
}

void isPalindromeUsingIteration(string y)
{
while (length>1)
{
if (y[] != [y + length - 1])
{
cout<<"false"<<endl;
}
++y;
length=length-2;
}
cout<<"true"<<endl;
}
void isPalindromeUsingRecursion(string y)
{
if ( length<2 )
{
cout<<"true"<<endl;
}
else
{
if ( y[] != [y + length - 1])
{
cout<<"false"<<endl;
}
else
{
cout<<ispalindromeUsingRecursion(string y+1)<<endl;
}
}

}
//utality function definitions
void initializeArray (unsigned short a[], int size)
{
for(int i=0;i<size; i++)
a[i]=0;
}

void printBinary(unsigned short a[], int size)
{
for(int i=size-1; i>=0; i--)
cout<<a[i];
cout<<endl;
}
void initializeArray (string b[], int length)
{
for(int i=0;i<length; i++)

}

void printBinary(string b[], int length)
{
for(int i=length-1; i>=0; i--)
cout<<b[i];
cout<<endl;
}
and by the way..im afraid there are errors in the structure of the program itself..plz try to read the code carefully and tell me where my mistakes are..thx alot..
elsa at 2007-11-11 21:01:17 >
# 4 Re: i really need help in a palindrome and binary conversion program..
Yea it's very better to send errors to explain them :

error C2360: initialization of 'b' is skipped by 'case' label
(45) : see declaration of 'b'
(51) : error C2086: 'b' : redefinition
(56) : error C2360: initialization of 'b' is skipped by 'case' label
(51) : see declaration of 'b'
(56) : error C2360: initialization of 'b' is skipped by 'case' label
(45) : see declaration of 'b'
(58) : error C2361: initialization of 'b' is skipped by 'default' label
(51) : see declaration of 'b'
(58) : error C2361: initialization of 'b' is skipped by 'default' label
(45) : see declaration of 'b'

those couples of errors are because you declare the "b" inside a single "case" and u declare it many times (redefinition) ... u must declare it one time only before the switch case itself .

(114) : error C2059: syntax error : ']'
(115) : error C2143: syntax error : missing ';' before '{'

I told u before why this error and I sent u the correct format for [y+...+...] must be (y+...+...)

(118) : error C2675: unary '++' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this opera
tor or a conversion to a type acceptable to the predefined operator

I told u about this error before too, what is the meaning of increasing a "string" , for example tell me what is the result of this : "++Amahdy" ?

(131) : error C2059: syntax error : ']'
(132) : error C2143: syntax error : missing ';' before '{'
(135) : error C2181: illegal else without matching if

same as before "[...]" must be "(...)" ; thsoe types of brackets : "[...]" are used only for arrays to specify index .

(137) : error C2065: 'ispalindromeUsingRecursion' : undeclared identifier

clair, this is not the correct function name

(137) : error C2275: 'string' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\xstring(612) : see declaration of 'string'

I told u about this again , if u can't understand my post ask me it's better I think; see again what I told u about it later and made it in bold .
Amahdy at 2007-11-11 21:02:16 >
# 5 Re: i really need help in a palindrome and binary conversion program..
ah the ispalindromeUsingRecursion() ; u should not tell the compiler "I want to use recursion" , it will not understand u !
the recursion are simply u call the function again with its name inside the same function like that :
type functionName(type)
{
functionName(value_supported_by_the_type); //here is a recurtion
}

About program logic try to have no errors first and see what is wrong in results and try to solve them, and if u have any problems send here .
Amahdy at 2007-11-11 21:03:22 >
# 6 Re: i really need help in a palindrome and binary conversion program..
Just to encourage your voyage to the realm of C++, you could write your bool isPalindromeUsingIteration(string y) functions as

inline bool isPalindromeUsingIteration( std::string const & y )
{
return std::equal( y.begin(), y.end(), y.rbegin() );
}

This function (taken from "Accelerated C++" by Andrew Koenig & Barbara Moo) demonstrates expressive power behind C++ language mechanisms when put in good use.
dcwexter at 2007-11-11 21:04:15 >
# 7 Re: i really need help in a palindrome and binary conversion program..
hi
thx for the help..i really appreciate it..
to amahdy..well..im not really understanding what you mean..i would really be grateful if u retype my code and write the corrections or what you want me to do near statements itself..
to dcwexter..im afraid i cant use the function you gave me as the assignment states that we have to use the bool isPalindromeUsingRecursion..but anyway..thx alot..
elsa at 2007-11-11 21:05:26 >
# 8 Re: i really need help in a palindrome and binary conversion program..
Here is it:

#include<iostream>
using std::cout;
using std::cin;
using std::endl;

#include<string>
using std::string;

#include<cctype>

enum Option { eBinaryArtithmetic = 1, eBinaryBitwise, ePalindromeIteration, ePalindromeRecursion, eQuit };

const unsigned long BitMax = sizeof( unsigned long ) * 8;

Option getMenuSelection();

void binaryConversionUsingArithmeticOperators( unsigned long );
void binaryConversionUsingBitwiseOperators( unsigned long );
bool isPalindromeUsingIteration( string const & );
bool isPalindromeUsingRecursion( string const & );

// Utilityfunctions declarations
void printBinary( unsigned short[], unsigned long );

int
main()
{
cout << "*** START VALUE TESTING PROGRAM ***\n";

Option choice;
int integerValue;
string stringValue;

do
{
choice = getMenuSelection();

switch ( choice )
{
case eBinaryArtithmetic:
cout << "\n* Binary Conversion using Arithmetic Operators *\n"
<< "Please enter an integer: ";
cin >> integerValue;

binaryConversionUsingArithmeticOperators( integerValue );
break;

case eBinaryBitwise:
cout << "\n* Binary Conversion using Bitwise Operators *\n"
<< "Please enter an integer: ";
cin >> integerValue;

binaryConversionUsingBitwiseOperators( integerValue );
break;

case ePalindromeIteration:
cout << "\n* Testing for Palindroms Using Iteration *\n"
<< "Please enter a string: ";
cin >> stringValue;

cout << "\"" << stringValue << "\""
<< ( isPalindromeUsingIteration( stringValue ) ? " is " : " is not " )
<< "a palindrome.\n";
break;

case ePalindromeRecursion:
cout << "\n* Testing for Palindroms Using Recursion *\n"
<< "Please enter a string: ";
cin >> stringValue;

cout << "\"" << stringValue << "\""
<< ( isPalindromeUsingRecursion( stringValue ) ? " is " : " is not " )
<< "a palindrome.\n";
break;

case eQuit:
cout << "\n\n*** END VALUE TESTING PROGRAM ***\a\a\a" << endl;
break;
}
}
while ( choice != eQuit );

return 0;
}

//function definitions
Option
getMenuSelection()
{
unsigned short selection = 0;

do
{
cout << "\n\nEnter an option:\n"
<< "1) Binary Conversion using Arithmetic Operators\n"
<< "2) Binary Conversion using Bitwise Operators\n"
<< "3) Testing for Palindroms Using Iteration\n"
<< "4) Testing for Palindroms Using Recursion\n"
<< "5) Quit\n\n"
<< "?: ";

cin >> selection;

if ( selection < eBinaryArtithmetic && selection > eQuit )
cout << "Invalid option (" << selection << "). Try again.\n\n";
}
while ( selection < eQuit && selection > eQuit );

return static_cast<Option>( selection );
}

void
binaryConversionUsingArithmeticOperators( unsigned long x )
{
unsigned short binaryDigit[::BitMax] = { 0 };
unsigned long numberOfDigits = 0;

do
{
binaryDigit[numberOfDigits++] = static_cast<unsigned short>( x % 2 );
x /= 2;
}
while ( x != 0 );

printBinary( binaryDigit, numberOfDigits );
}

void
binaryConversionUsingBitwiseOperators( unsigned long x )
{
unsigned short binaryDigit[::BitMax] = { 0 };
unsigned long numberOfDigits = 0;

do
{
binaryDigit[numberOfDigits++] = static_cast<unsigned short>( x & 1 );
x >>= 1;
}
while ( x != 0 );

printBinary( binaryDigit, numberOfDigits );
}

void
printBinary( unsigned short a[], unsigned long n )
{
unsigned long bit = n;
while ( bit > 0 )
cout << a[--bit];

cout << endl;
}

bool
isPalindromeUsingIteration( string const & s )
{
unsigned long begin = 0;
unsigned long end = static_cast<unsigned long>( s.size() - 1 );

while ( begin < end )
if ( s != s[end--] )
return false;

return true;
}

bool
isPalindromeUsingRecursion( string const & s )
{
if ( [B]s.size() < 2 )
return true;
else
{
if ( s[ 0 ] != s[ s.size() - 1 ] )
return false;

return isPalindromeUsingRecursion( s.substr( 1, s.size() - 2 ) );
}
}

I have to outline that isPalindromeUsingRecursion is extremly inefficient, so it's only purporse is to demonstrate recursion. As you can see, I emphasized some parts of code. These are my additions to your solution and if your not familiar with them, you should definitively learn about them.
dcwexter at 2007-11-11 21:06:23 >
# 9 Re: i really need help in a palindrome and binary conversion program..
thx alot sir..i tried the program and it worked..i have been trying to build it for 2 weeks..but it wasnt working everytime i try it..anyway..i have been trying to study what u have written really well..and there were new things that i dont know..i will try to improve my knowledge some more..thx
elsa at 2007-11-11 21:07:27 >
# 10 Re: i really need help in a palindrome and binary conversion program..
It's really nothing. I know it can get sticky sometimes... ;)
dcwexter at 2007-11-11 21:08:28 >