set of C++ functions

Eish guys am honesly don't know where to start, please help!
I have write the following set of c++ functions

 fromBinaryToDecimal: The function must accept a pointer to a one-dimensional, dynamically allocated, Boolean array together with an integer representing its length as parameters. You may assume that the array represents a positive value. The function’s return value is an unsigned integer.
 fromDecimalToBinary: The function receives an unsigned integer as a parameter and returns a string.
 fromHexToBinary: The function receives a string as a parameter and returns a string. Your function must check to make sure that the incoming string represents a valid hexadecimal value.
 fromBinaryToHex: The function must accept a pointer to a one-dimensional, dynamically allocated, Boolean array together with an integer representing its length as parameters. You may assume that the array represents a positive value. The function’s return value is a string.
 Overloaded versions of the fromBinary* functions which accept a string parameter.
 A main function in a separate cpp file which demonstrates the use of your functions via a menu system.
Start by writing the fromBinaryToDecimal function. Test it with the following main():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cassert>
#include <iostream>

unsigned fromBinaryToDecimal( bool* binaryNum, int length ) {
    unsigned asDecimal = 0;
   // Put your code here
    return asDecimal;
}

int main() {
    bool* binaryNum = new bool[8];
    binaryNum[0] = true;
    binaryNum[1] = false;
    binaryNum[2] = true;
    binaryNum[3] = false;
    binaryNum[4] = true;
    binaryNum[5] = false;
    binaryNum[6] = true;
    binaryNum[7] = false;

    unsigned result = fromBinaryToDecimal( binaryNum, 8 );
    std::cout << "The number in decimal is " << result << std::endl;
    assert( result == 170 );  // 170 is the right answer
    delete [] binaryNum;
    return 0;
}
Instructions followed but i dont see anything happening as yet and i'd appreciate very much if i can get sum explanations there and there. Can we proceed to the next step please..
Don't you think you should actually learn some C++ yourself?

http://www.cplusplus.com/doc/tutorial/
Please Galik i know i may sound stupid but please i nid ur help...i have 2mit this deadline please, it wil tac me tym to learn c++..this is a whole new world for me..please be wit me..thank u
Ok am getting there so what is the header <cassert> do?
jsmith am still making my way there but now am stuck please help! how to reverse this function fromDecimalToBinary?
There is nothing stupid about not knowing how to program in C++. But why should someone do your work for you because you haven't done it?

You need to try to answer those questions by yourself based on what you have already been taught. Then post your code here and we can help you where you make mistakes. No one here wants to just do your work for you so that you don't learn anything for yourself. You need to be studying C++ along side the questions you ask here. People are going to need to see some evidence of that.

http://www.cplusplus.com/forum/beginner/1/#msg6680

If you post your complete, working fromBinaryToDecimal function we can move on to the next step.
First let's make fromBinaryToDecimal work.
Hi, please help am stuck.

I have write the following set of c++ functions

 fromBinaryToDecimal: The function must accept a pointer to a one-dimensional, dynamically allocated, Boolean array together with an integer representing its length as parameters. You may assume that the array represents a positive value. The function’s return value is an unsigned integer.
 fromDecimalToBinary: The function receives an unsigned integer as a parameter and returns a string.
 fromHexToBinary: The function receives a string as a parameter and returns a string. Your function must check to make sure that the incoming string represents a valid hexadecimal value.
 fromBinaryToHex: The function must accept a pointer to a one-dimensional, dynamically allocated, Boolean array together with an integer representing its length as parameters. You may assume that the array represents a positive value. The function’s return value is a string.
 Overloaded versions of the fromBinary* functions which accept a string parameter.
 A main function in a separate cpp file which demonstrates the use of your functions via a menu system

here is my code so far:

#include <iostream>


using namespace std;

int BinaryToDecimal(char *bin)
{
int b, k, m, n;
int len, sum = 0;

len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
n = (bin[k] - '0'); // char to numeric value
if ((n > 1) || (n < 0))
{
puts("\n\n ERROR! BINARY has only 1 and 0!\n");
return (0);
}
for(b = 1, m = len; m > k; m--)
{
// 1 2 4 8 16 32 64 ... place-values, reversed here
b *= 2;
}
// sum it up
sum = sum + n * b;
printf("%d*%d + ",n,b); // uncomment to show the way this works
}
return(sum);
}

/*int DecimalToBinary(char *dec)
{
int b, k, m, n;
int len, sum = 0;

len = strlen(dec) + 1;
for(k = 0; k >= len; k++)
{
n = (dec[k] + '0');
if((n < 1) || (n > 0))
{
puts("\n\n ERROR!");
return (0);
}
for(b = 1, m = len; m > k; m--)
{
b *= 2;
}

sum = sum - n / b;
printf("%d*%d + " , n , b);
}
return (sum);
}*/

int main()
{
char *Num;
int name;

cout << "Enter the binary number " << endl;

cin >> Num;
name = BinaryToDecimal(Num);
cout << endl;


cout << name << endl;


system("PAUSE");
return 0;
}




That works so far. The only problem was with your input buffer:
1
2
3
4
5
6
int main()
{
//char *Num; // This pointer does not point to anything
char Num[100]; // Now it accesses 100 characters
int name;


When you declare a char* you can't use it to access characters until you assign some memory to it.

http://www.cplusplus.com/doc/tutorial/dynamic/

So instead of declaring a char* I declared a char array which has 100 memory locations assigned to it.
Last edited on
Also:

1
2
3

puts("\n\n ERROR! BINARY has only 1 and 0!\n");

In C++ its better to use the new output mechanism std::cout:
1
2
3

std::cout << "\n\n ERROR! BINARY has only 1 and 0!\n";


And:

1
2
3
4
5
len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
    // .. stuff
}


When iterating over an array most people will go to less than len, rather than alter len to suit:

1
2
3
4
5
len = strlen(bin);
for(k = 0; k < len; k++)
{
    // .. stuff
}


That way len still contains the actual number of characters (length) of the char array.

Its a little clearer and more likely what most people will expect.
Last edited on
Ok thanks and how can you correct me on this function?
int DecimalToBinary(char *dec)
{
int b, k, m, n;
int len, sum = 0;
len = strlen(dec) + 1;
for(k = 0; k >= len; k++)
{
n = (dec[k] + '0');
if((n < 1) || (n > 0))
{
puts("\n\n ERROR!");
return (0);
}
for(b = 1, m = len; m > k; m--)
{
b *= 2;
}
sum = sum - n / b;
printf("%d*%d + " , n , b);
}
return (sum);
}

Eish i'm not really sure on how to reverse it(fromDecimalToBinary)
Topic archived. No new replies allowed.