OK, I'm tearing my hair out over something that is apparently quite easy to do (and it's only Introduction to C apparently). I have to write two programs within 12 hours (no sleep tonight I bet).
One takes an integer input and is supposed to output the digits starting from the least significant number.
i.e. input = 512 / output "Digit (1) = 2 / Digit (2) = 1 / Digit (3) = 5" (on seperate lines)
The code I have thus far is:
I've got the algorithm down on paper for integers with more than two digits: loop a%10 and then a/10 till a/10 is equal to 0, at which point the loop should terminate. The modulus for each iteration should be the digit to be outputted. Problem is, I've tried a few different ways to write this out, though none have been successful.
Second program asks for a positive integer, then decides wether or not it is a power of a base (which in this case is 3). If not, it prints that it is not a power of the base, if so, it says so and also prints the exponent.
i.e. (81) 81 is a power of 3, exponent = 4 / (-5) Invalid Integer! / (8) 8 is not a power of 3
Again, what I have so far is:
Again, I think I have the algorithm down, don't know how to code it. I'm missing the function that will tell me if the integer was a power of the base, and what the exponent is. I believe I have to make a loop that divides the integer by the BASE over and over until it equals 1. Thus, the exponent should be the number of times the loop was executed. For example, 34 = 81, 81/3/3/3/3 = 1. But I can't figure out how to code this either. I figure a for loop won't work because it requires knowledge of how many times you'd want to execute it.
I hope I'm not asking too much, if anyone could give some tips and pointers on how to write up the rest of the programs, I'd appreciate it immensely. Even just a general direction would probably be really helpful at this point.
I apologize for the shitty coding
One takes an integer input and is supposed to output the digits starting from the least significant number.
i.e. input = 512 / output "Digit (1) = 2 / Digit (2) = 1 / Digit (3) = 5" (on seperate lines)
The code I have thus far is:
Code:
#include <stdio.h> int main() { int a, i, n, digit; digit = 1; printf("Enter an integer: "); scanf("%d", &a); if (a < 0) { a = a * -1; } if (a < 10) { printf("Digit (%d): %d\n", digit, a); } return 0; }
Second program asks for a positive integer, then decides wether or not it is a power of a base (which in this case is 3). If not, it prints that it is not a power of the base, if so, it says so and also prints the exponent.
i.e. (81) 81 is a power of 3, exponent = 4 / (-5) Invalid Integer! / (8) 8 is not a power of 3
Again, what I have so far is:
Code:
#include <stdio.h> #define BASE 3 int main() { int a, num, x, y, i; //don't mind the excessive integers printf("Enter a positive integer: "); scanf("%d", &num); if (num < 1) { printf("Invalid Integer!\n"); return 1; } if (num == 1) { printf("%d is a power of %d, exponent = 0\n", num, BASE); } while (num > 1) num%BASE; x = num%BASE; if (x != 0) { printf("%d is not a power of %d!\n", num, BASE); return 1; } return 0; }
I hope I'm not asking too much, if anyone could give some tips and pointers on how to write up the rest of the programs, I'd appreciate it immensely. Even just a general direction would probably be really helpful at this point.
I apologize for the shitty coding
Last edited by Smithereener (2008-10-30 01:51:00)