Thread: Need help explaining

  1. #1
    Registered User
    Join Date
    Sep 2011

    Need help explaining

    Write a function called numeric() that takes a c-string made up of numeric characters such as 0's and 1's, and assigns the numeric value of the string to an integer type reference parameter and returns true if the numeric value was computed successfully and false otherwise. For example, if the c-string contains 00100011, it must assign the value of 35 to its integer parameter and return true. Another parameter passed to the function tells what number system the c-string contains. For the example just given, that parameter would be 2 indicating base 2 for the value (binary number). If that parameter is 10, then the string would have to contain a decimal value (0...9).

    The function first checks to see if the elements of the string meet the number system criterion in which case, it computes the value, assigns it to its reference parameter and returns true. If not, it assign -1 to the reference parameter and return false.

    To calculate the numeric value of the string, the function must take the numeric value of each of the characters of the string, multiply each by the weight of the digit and add all the resulting products. For example, if the number is 6903, 6 has a weight of 1000, 9 has 100, 0 is at 10 and 3 has a weight of 1. Remember that the numeric value of a digit is different from its ASCII value. For example, the ASCII value of 0 is not 0; it's 48 and the ASCII value of 1 is 49, etc.
    Also, write a function called shift_left() which takes a c-string and shifts all its digits to left by one. So, for example, 01101001 becomes 11010010. For this to work the leftmost digit must be 0; otherwise, the result will be wrong. That means the input number must not be so large as to make the leftmost digit a non-zero. Also, notice that the rightmost digit takes on a 0 after the shift.
    After performing the shift, the function copies the input c-string to another (output) c-string parameter which the caller (main) can take as the output. An integer parameter again tells the function the number system of the input c-string, 10 for decimal, 2 for binary, etc.
    Now, write the main program to test both functions - once with a binary number and once with a decimal value: declare two c-strings of a size large enough to hold at least 17 characters (large enough for 16 bits plus the null character) called input and output. Read a binary number containing no more than 16 bits from the keyboard and store in input c-string, and using the numeric() function, calculate and print in main its numeric value.
    Then, pass the string read from the user as well as the output c-string to the shift_left() function which will assign the shifted string to the output c-string. Then, have main pass the output c-string to numeric() function to compute its value. Again, print the resulting value in main.
    Also, read a decimal value from the user into the input c-string, pass it to the numeric() function, and print the resulting value in main, and then pass both the input and output c-strings to the shift_left() function and print the result again in main.

    You will see that when the string digits are shifted to the left, it's the same as multiplying its value by the base: 2 for binary, 10 for decimal, etc.

    #include <iostream>
    #include <cmath>
    #include <conio.h>
    using namespace std;
    bool numeric(char[], int, int&);
    void shift_left(char[], char[], int);
    int main()
        char input[17], shifted[17];
        int number, base;
        cout << "Enter a numeric string of maximum 16 characters long: ";
        cin.getline(input, 17, '\n');
        cout << "\nEnter the base; e.g.: 2 for binary, 10 for decimal: ";
        cin >> base;
        if(numeric(input, base, number))
            cout << "\nNumeric value of " << input << " = " << number << endl;
            shift_left(input, shifted, base);
            cout << endl << input << " shifted left by a digit = " << shifted;
            if(numeric(shifted, base, number))
                cout << "\nNumeic value of " << shifted << " = " << number << endl;
            cout << "\nInvalid string entered!\n";
        cout << "\nPress any key to continue.";
        return 0;
    bool numeric(char in[], int base, int& num)
        int len = strlen(in), exp = 0;
        num = 0;
        for(int i = len - 1; i >= 0; i--)
            if(in[i] >= base)
                num = -1;
                return false;
                num += (in[i] - '0') * pow((double)base, exp++);
        return true;
    void shift_left(char in[], char sh[], int base)
        strcpy(sh, in + 1);
        strcat(sh, "0");
        sh[strlen(sh)] = '\0';

    I didn't write this program ,but I need an explanation on why is num += (in[i] - '0') need to has " - '0' " in it.

    Thank you

  2. #2
    Registered User
    Join Date
    Oct 2006
    because the numeric value of the character '0' is not the same as the number zero. in ASCII, it's 48, and the digits are arranged '0'=48, '1'=49, etc. so you subtract '0' from in[i] to get the integer value corresponding to the ASCII representation of the number. effectively, you're subtracting 48 from the ASCII values to get integer values 0 thru 9 from ASCII digits '0' thru '9'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help explaining output
    By alpine in forum C Programming
    Replies: 6
    Last Post: 11-04-2010, 03:33 AM
  2. Help: Explaining Code
    By Student1 in forum C++ Programming
    Replies: 6
    Last Post: 02-08-2008, 03:31 AM
  3. help in explaining the program
    By jazzz in forum C Programming
    Replies: 2
    Last Post: 05-23-2006, 12:42 AM
  4. I need help explaining something...
    By deathstryke in forum A Brief History of
    Replies: 1
    Last Post: 01-08-2003, 12:02 PM
  5. need graphics.h explaining.
    By will bushnell in forum C Programming
    Replies: 17
    Last Post: 03-28-2002, 11:18 PM