Can someone help me interpret this code in C? (Tell me whats going on)

Can someone interpret this code for me? I dont understand whats going on. This code is supposed to take a number n that is inputted and show all the numbers below n that are frugal.

A little background, a frugal number is a number where the amount of the digits in the number exceed the amount of digits in its prime factorization (including exponents). So for example, 1024 is a frugal number because 1024 = 2^10, where 1024 has 4 digits and 2^10 has 3 digits.

A code was given to me that compiles and executes exactly what is needed but all the variables, etc have been replaced by random letters, and im trying to figure out what each variable is so that it makes sense to someone reading it. Ive only been able to figure out what FactorDigits, DigitsCount, and n are. The rest i dont know cause i dont really understand what is going on.

Here is the code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>

int m(int m) {
int c=0;
m: if (!m) goto c;
m/=10;
c++;
goto m;
c: return c;
}

int c(int c) {
int n=2,b=0,DigitsCount=m(c),FactorDigits=0;
m: if (!(c%n)) { b++; c/=n; goto m;}
if (!b) { n++; goto m; }
if (b>1) FactorDigits+=m(b);
FactorDigits+=m(n);
if (c==1 || FactorDigits>=DigitsCount) goto c;
n++;
b=0;
goto m;
c: if (DigitsCount>FactorDigits) return 1;
return 0;
}

int main () {
int n,b=2;
printf("Enter a number: ");
scanf("%d",&n);
m: if (c(b)) printf("%d\n",b);
if (++b<n) goto m;
}


Can someone please help me understand this with as much detail as possible.
Thanks
The function m accepts an int. If the int is anything other than zero, the function returns 0. If m is not zero, it is divided by ten over and over nutil it is zero, and then you get returned the number of times it was divided by ten. This counts the number of digits in the number.

You should just try it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int m(int m) {
int c=0;
m: if (!m) goto c;
m/=10;
c++;
goto m;
c: return c;
}

int main()
{
  for (int i=0;i<10000; i+=99)
  {
     std::cout << "i = " << i << ", function return = " << m(i) << std::endl;
  }
return 0;
}



Now you do the same for function c

Last edited on
I still dont quite understand. Based on what you said, what could function m and function c stand for?
m could stand for "my function for telling how many digits there are in an int". It's more likely to stand for "the original coder couldn't be bothered coming up with a decent function name".
Can you explain to me what is happening in c, its a lot more confusing then m.
That code is crap. Throw it away.

The original coder threw you a loop by naming two things "c": a variable and a label. And he wasted time in more than one way. He is not your friend.

If you want to know the number of digits in a value, use some math:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cmath>

int CountCharacters( int value )
  {
  // Zero always uses exactly one character
  if (value == 0) return 1;

  // Negative values need an extra character for the minus sign
  if (value < 0) return CountCharacters( -value ) + 1;

  // (This is the math)
  return std::floor( std::log10( value ) ) + 1;
  }

Hope this helps.
Thanks for letting me know so i could stop wasting my time. Unfortunately, my assignment is due tonight and have no chance writing a completely new code by then, since i dont know much if any c. Looks like ill need to find a tutor. Thanks for the help.
But you know C++? C is just a subset of C++.
Last edited on
Yes, but it asks for a different set of methodologies and functions to be used. I don't think that statement is so absurd...

-Albatross
To be more clear, i dont know c or c++. I currently only know FORTRAN, we just started leaning c, and i am completely lost.
Ah. Well then that is probably not the function to start with, then.
Oh, well, think then. If a number is zero, then it takes exactly one digit. Otherwise, you can count the number of digits by using integer division until the number is zero.

For example, to find the number of characters required to print the number 1234:
1234 / 10 = 123
 123 / 10 =  12
  12 / 10 =   1
   1 / 10 =   0

That was four divisions until we got a quotient of zero. Hence, it takes four characters.

The math trick still works, of course, but as this is homework I suspect your professor wants to see you using the loop.

Good luck!
I love the explanation here, the code is crap, the coder is not your friend! Sorry, I laughed so hard when I saw that, I had to say something! LOL

Of course he is right, just loved the way it was said!
Topic archived. No new replies allowed.