Tuesday, September 15, 2015

EXCITE

I got it to work!!!!!!

All I had to do was change the variable being tested for zero value, and the location of the test!!! I had designed it one way at first, and then changed a few variables around and forgotten to change that one very important detail.

The more I think about it, the more this program is probably extremely hackish. The whole point of the project was to create a program that would take a whole, non-negative, non-zero number and repeat it back in reverse order.

In the book, my hint was to use a do loop to continuously divide the input by 10 until it reached 0. That was fine and all, but every time it divided by 10, it would give me answers like 9876, 987, 98, 9. If I only used modulo to get the remainder, I would consistently get the last digit.

So I started thinking: if I needed the last digit of each result of dividing the entry, why not use mod?

From this frame of thinking, I realize that I probably went the extra mile and made this more complex than it should be. Here is my first working program, though:

#include <stdio.h>

int main(int argc, char* argv[]) {

int entry, right_digit, left_digits, MOD, DIVISOR;
// these are named to reflect the position of the digit in the input

printf("Enter a whole, non-negative number with any amount of digits: ");
scanf("%d", &entry);

MOD = 10;
DIVISOR = 10;

// This takes care of the right-most digit, and prints it on the left

right_digit = entry % MOD;
printf("%d", right_digit);

// my hint is to use a do loop for the left digits

do {
MOD *= 10;
left_digits = entry;
left_digits %= MOD;
left_digits /= DIVISOR;
if (left_digits <= 0) {
break;
}
printf("%d", left_digits);
DIVISOR *= 10;
} while (left_digits > 0);

printf("\n");

return 0;

}

Skip past all of the printf/scanf nonsense that comes before the actual maths.

The right-most digit of the entry is taken care of first, because it needs to be printed on the far left if the number is to be reversed. Assume we use the number 5932 as our entry. The remainder of 5932 % 10 is 2. 2 is printed first.

Once that's done, we move into the do loop.

MOD starts out at 10 for the first calculation. The first part of the do loop multiplies MOD by 10.

left_digits is then assigned the value of entry. I worked with the original value through this because I couldn't figure out how else to do it (granted, I also couldn't figure out what was decrementing to 0 to give me a floating point exception).

left_digits is then divided by 100 and the remainder is stored back into left_digits. Assuming the original entry is 5932, for example, the remainder is 32.

left_digits is then divided by DIVISOR, which also begins at 10. Because it is an integer, and assuming we continue using the example above, left_digits is assigned the value of 3.

There is a conditional here to end the loop prematurely if left_digits is less than or equal to 0.

left_digits is printed. Continuing with the example entry above, the value printed here is 3. So far, our reversed number is: 23

DIVISOR is then multiplied by 10, making it 100.

Since left_digits is not 0, we loop again!

MOD is multiplied by 10 again and is now 1000.

left_digits is assigned the value of entry again because of the way this works. You'll see the pattern, just hang on.

left_digits is divided by MOD, which is 1000, and the remainder is assigned back into left_digits. Using the example above, the remainder would be 932.

left_digits is divided by DIVISOR, which is 100, and assigns the value to left_digits. Using our example, the value is now 9.

left_digits is printed, as long as it is not 0. So far in this loop, our reversed number is: 239

DIVISOR is multiplied by 10 again, making it 1000.

So, you see??? I got it to work!! Is there a better way to do it??? Probably!! But it does work, hahahaha.

I wonder when it will be that I look back at this little fragment and say, "What was I thinking??"

Oh, and I wonder if it's worth noting that I also hand-calculated the entire loop path to see exactly what it was doing, and that's exactly how I found out why I was getting a floating point exception error.

No comments:

Post a Comment