Smithereener
Member
+138|6312|California
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:

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;
}
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:

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;
}
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

Last edited by Smithereener (2008-10-30 01:51:00)

Scorpion0x17
can detect anyone's visible post count...
+691|6762|Cambridge (UK)
I'm not going to give you the answer, but I'm going to give you a very big hint, that may help you with both problems:

For loops do not need to know 'how many' loops to do - just when to exit.

For example, take the following code snippet:

Code:

b=random_integer();
for(a=0; a!=b; a++)
{
 b=random_integer();
}
first generates a random integer and stores it in 'b', then it loops round incrementing 'a' and generating a new 'b', until 'a' equals 'b' - it does not know in advance how many times it loops, it just knows to stop looping when 'a=b'.

Hope that helps?

If it doesn't help, type up your algorithms and post them here...

Last edited by Scorpion0x17 (2008-10-30 03:19:17)

Smithereener
Member
+138|6312|California
Wasn't expecting a definite answer, but I think I just got an idea out of your snippet. Thanks a bunch.
Scorpion0x17
can detect anyone's visible post count...
+691|6762|Cambridge (UK)

Smithereener wrote:

Wasn't expecting a definite answer, but I think I just got an idea out of your snippet. Thanks a bunch.
That is what I was hoping would happen.
CaptainSpaulding71
Member
+119|6353|CA, USA
how about entering a string instead of a digit.  then you can more easily iterate over the elements of the string (or character array).  you say to the user to enter a 'integer' and you can check whether the string entered is actually an integer by converting it to integer and comparing.  just another idea.  this way you don't have to mess around with bases and crap like that.
Winston_Churchill
Bazinga!
+521|6735|Toronto | Canada

Did you declare or set a value for BASE in the second one?  Because I cant find it...

Btw, I'm taking the exact same course, Intro to C Programming

Edit: nvm, didnt look that far up

Last edited by Winston_Churchill (2008-10-30 17:56:43)

CaptainSpaulding71
Member
+119|6353|CA, USA
i don't know C/C++ very well, but how about this in Perl for the second program you have.  i decided to attack it by building up rather than with modulo (dividing):

Code:

#!/usr/bin/perl
$| = 1;
use strict;
my $BASE = 3;
my $input = $ARGV[0];
if (($input eq "") || (! ($input =~ /^(\d+)$/))) {
    print("-E-:  must input an integer\n");
    exit(1);
}
my $temp = 1;
my $is_a_power_of_base = 0;
for (;;) {
    $temp = $temp * $BASE;
    if ($temp == $input) {
        $is_a_power_of_base++;
        last;
    }
    if ($temp > $input) {
        $is_a_power_of_base = 0;
        last;
    }
}
if ($is_a_power_of_base) {
    print("DEBUG:  $input is a power of base $BASE\n");
} else {
    print("DEBUG:  $input is NOT a power of base $BASE\n");
}
my idea was to loop forever and increment the starting point (temp) by multipying it by the base each time through the loop.  then, you check for the corner cases of whether it is more than your input value (81) in which case it is not a power of the base.  if it equals your input at any point in the loop, then it is a power of your base.  in either case, you want to exit the for loop (i used last in perl here), otherwise, keep iterating.

good luck

Last edited by CaptainSpaulding71 (2008-10-30 20:24:40)

Smithereener
Member
+138|6312|California
Woooah, I guess an update is in order; I got both of them working by using a while loop 5 minutes before the deadline.

Appreciate the help.

Btw, we only go over C programming, so using another language is out of question. Still appreciate it
cowami
OY, BITCHTITS!
+1,106|6286|Noo Yawk, Noo Yawk

Smithereener wrote:

Woooah, I guess an update is in order; I got both of them working by using a while loop 5 minutes before the deadline.

Appreciate the help.

Btw, we only go over C programming, so using another language is out of question. Still appreciate it
EGR115/Intro to Computing for Engineers, i presume
https://i.imgur.com/PfIpcdn.gif
mtb0minime
minimember
+2,418|6651

cowami wrote:

Smithereener wrote:

Woooah, I guess an update is in order; I got both of them working by using a while loop 5 minutes before the deadline.

Appreciate the help.

Btw, we only go over C programming, so using another language is out of question. Still appreciate it
EGR115/Intro to Computing for Engineers, i presume
cow, what kind of engineer are you? I'm just curious since as a Civil Eng. I had to learn fortran and MATlab, never did anything with C
Smithereener
Member
+138|6312|California

cowami wrote:

Smithereener wrote:

Woooah, I guess an update is in order; I got both of them working by using a while loop 5 minutes before the deadline.

Appreciate the help.

Btw, we only go over C programming, so using another language is out of question. Still appreciate it
EGR115/Intro to Computing for Engineers, i presume
Actually...

ECE15
Winston_Churchill
Bazinga!
+521|6735|Toronto | Canada

Smithereener wrote:

cowami wrote:

Smithereener wrote:

Woooah, I guess an update is in order; I got both of them working by using a while loop 5 minutes before the deadline.

Appreciate the help.

Btw, we only go over C programming, so using another language is out of question. Still appreciate it
EGR115/Intro to Computing for Engineers, i presume
Actually...

ECE15
No!

APS105
cowami
OY, BITCHTITS!
+1,106|6286|Noo Yawk, Noo Yawk

mtb0minime wrote:

cowami wrote:

Smithereener wrote:

Woooah, I guess an update is in order; I got both of them working by using a while loop 5 minutes before the deadline.

Appreciate the help.

Btw, we only go over C programming, so using another language is out of question. Still appreciate it
EGR115/Intro to Computing for Engineers, i presume
cow, what kind of engineer are you? I'm just curious since as a Civil Eng. I had to learn fortran and MATlab, never did anything with C
aero babes

i'll learn matlab soon, we're just doing C for those cases when you have to repeat calculations for different data, since it's relatively simple
https://i.imgur.com/PfIpcdn.gif
Beduin
Compensation of Reactive Power in the grid
+510|5746|شمال
lol
just handed a MATLAB assigment, simulation of DTMF System.
must say, not easy stuff for me..takes ALOT of time.. But it works

Last edited by Beduin (2008-10-31 11:53:42)

الشعب يريد اسقاط النظام
...show me the schematic
unnamednewbie13
Moderator
+2,053|6768|PNW

Drop C and use APL. /ftw

Board footer

Privacy Policy - © 2024 Jeff Minard