1

Ok, so this is my homework assignment. But before everyone says, "figure it out," I have gotten the program to work. I just can't figure out why it won't work for the full ten characters of an integer. The homework asks a user to input an integer. Using one switch we are to print the number in single integer words (26 would be two six). I can get that to work. The problem I'm having is when I enter 1-0 as the integer it prints, "one two three four five six seven eight zero zero." If I enter 1-9 it prints, "one two three four five six seven nine nine." If I enter 1-8, it works just fine. I'm not sure what's going wrong here. Here is my coding.

#include <stdio.h>

int main(void)
{
    int num, num2, integer, decimal_place, length, sum;
    float multiplier, integer_length, avg;

    printf("\nPlease enter an integer: ");
    scanf("%d", &num);
    printf("\n");

    printf("\tYou have entered:\n\n\t");
    if (num < 0)
    {
        printf("negative ");
        num *= -1;
    }

    num2 = num;
    length = 1;
    sum = 0;
    integer_length = 0;


    // Get the length of the input
    while (num2 > 9)
    {
        length++;
        num2 /= 10;
    }

    for (integer = length; integer > 0; integer--)
    {
        multiplier = 10;

        for (decimal_place = integer; decimal_place > 0; decimal_place--)
        {
            multiplier *= 0.1;
        }

        num2 = num * multiplier;
        num2 %= 10;

        switch(num2)
        {
            case 0:
                printf("zero");
                sum += 0;
                break;
            case 1:
                printf("one");
                sum += 1;
                break;
            case 2:
                printf("two");
                sum += 2;
                break;
            case 3:
                printf("three");
                sum += 3;
                break;
            case 4:
                printf("four");
                sum += 4;
                break;
            case 5:
                printf("five");
                sum += 5;
                break;
            case 6:
                printf("six");
                sum += 6;
                break;
            case 7:
                printf("seven");
                sum += 7;
                break;
            case 8:
                printf("eight");
                sum += 8;
                break;
            case 9:
                printf("nine");
                sum += 9;
                break;
        }

        printf(" ");
    }

    while ( num > 0 )
    {
        num /= 10.00;
        integer_length++;
    }


    avg = sum / integer_length;
    printf("\n\nThe sum of the individual integers is: %d\n", sum);
    printf("The average is: %.2f", avg);

    printf("\n\n");

    return 0;
}

Any help would be greatly appreciated.

Sojourn85
  • 25
  • 6
  • 1
    Use a debugger so you can see the values that the variables take on each time through the loop. – Barmar Nov 08 '13 at 23:35
  • 1
    Rewrite your code without using floats. Not only it's easier (there is no advantage in using *any* of your floats), but you will notice some differences in output as well. – Jongware Nov 08 '13 at 23:41
  • I haven't thoroughly checked the code but skimming through it, I'd say using floats is a bad idea. – rwols Nov 08 '13 at 23:41
  • Oh well, apart from `avg` then. – Jongware Nov 08 '13 at 23:42
  • @Jongware ... you can do something like: `printf("avg=%d.%d%d",sum/int_len,((10*sum)/int_len)%10,((100*sum)/int_len)%10);` or just cast them to float in the printf – technosaurus Nov 09 '13 at 00:41

2 Answers2

1

You calculate your multiplier as a float with only 24 bits (about 7 digits) of precision. So when you have a number with more than about 6 digits, you get a round-off error when you're extracting digits.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
0

Tiny fix:

double multiplier;

instead of

float multiplier;

Single-precision floating point number cannot represent big integer value. So MSB bits are ok, but LSB has a "noise" of discretization:

i = 1234567890, f = 1234567936.000000

Single precision floating point can represent some value without error with quantization step of 0.000000119 (2^-23). So float type can represent numbers from -2 to 2-2^-23 with a step of 2^-23. Thus 123456789 achieves this limitation for float type at this line:

num2 = num * multiplier;
(int)  (int)  (float)

And LSB bits of num2 get a quantization noise.

Michael
  • 1,505
  • 14
  • 26
  • Thanks. A bit confusing to a new programmer, but I get the gist of it. – Sojourn85 Nov 09 '13 at 01:41
  • @Sojourn85 See my answer for the same topic here: http://stackoverflow.com/questions/19871086/float-variable-doesnt-meet-the-conditions-c/19871124#19871124. It has more detailed explanation of how floating point numbers are computed. And don't forget to mark answers! ;) – Michael Nov 09 '13 at 02:11