factoriadic number calculator problems

May 6, 2010 at 7:55pm
hi guys im quite new to C++, so there is probably lots wrong with this programme. im really having alot of trouble getting it to work. ive made a function for factorial that works. i have then tried to do a function to work out the factoriadic number of a normal decimal number and give them out in an array. if you need me to explain parts then feel free to ask, my code barely makes any sense even to me. below is my code



see my code on next post
Last edited on May 7, 2010 at 1:36pm
May 7, 2010 at 12:24pm
ok i have modified my loop a bit. maybe this will make more sense to people. it always comes out with the number i entered in though and not the factoriadic number. could it be something wrong with the arrays? the reason the array is of size [8] is because this program will only be used to calculate factoriadic numbers of up to 9 digits.

edit: it works for calculating some numbers but not all. also it does not display the zeros in the array.

#include <conio.h>
#include <stdlib.h>
#include <iostream.h>

int factorial (int n) //define function to work out the factorial of a given number
{
int fact=1;
for (int i=1; i<=n; ++i)
fact=fact*=i;
return fact;
}

int factoriadic[8];
int factoriadic_number(int n)
{
int test;
int j = 9;
for (int i=8; i>-1; i--)
{
testpoint:
test = j*factorial(i+1); //starts at 9*9! (j*i!) to generate a test number
if (test==n)
{
factoriadic[i]=j; //if the test number equals the entered number, puts the value of j into the array in i's position
break;
}
else if (test<n)
{
factoriadic[i]=j; //if the test number is less than the entered number, puts the value of j into the array
continue; //continues to the next value of i
}
else if (test==0)
{
factoriadic[i]=0; //if j reaches zero and therefore the test number. enters zero in the array at i's position
continue; //continues to the next value of i
}
else
{
j=j-1;
goto testpoint; //if none of above happens, reduces value of j by 1 and repeats the test until one of the above statements happens.
}
}
}





int number,fact,result;

int main()
{
cout << "enter a number ";
cin >> number;
factoriadic_number(number);
for(int n=0 ; n<9 ; n++ )
{
result = factoriadic[n];
}
cout << result;

getch();
return 0;
}
Last edited on May 7, 2010 at 1:12pm
May 7, 2010 at 1:15pm
You have quite a few logic errors here, and judging by the structure of the code, I don't
think you are very comfortable with C++ syntax.

Although there are problems elsewhere, let's just start with main().

What do you want factoriadic_number() to do? Be as precise as possible.

Next, you have a for() loop whose body just assigns result the value of factoriadic[0], then overwrites
that value with factoriadic[1], then overwrites it with factoriadic[2], etc. Lastly, the program then outputs
result, which according to the for() loop, will always be factoriadic[8], since that was the last value assigned
to result. This is obviously not what you want.

Now, you declare factoriadic as a global array of size 8, meaning that the array has 8 elements in it, the
first one being index 0 and the last one being index 7 (since there are eight values in the closed range [0...7]).
But you are accessing factoriadic[8] which does not exist.

Let's fix main first, then we'll move on to the other functions.
May 7, 2010 at 1:30pm
right now all i am using main for is to enter in a number and then it to calculate the factoriadic number of that number. It works in the sense that if i enter in say 1088640 which is 3*9! the programme will spit out just 3. but i want it to be spitting out [3,0,0,0,0,0,0,0,0,0]. i want it to be able to convert a number to factoriadic form. so say u enter in 137. 137 can be expressed as 1*5!+0*4!+2*3!+2*2!+1*1!+0*0!

so i want the program to give out [1,0,2,2,1,0]

i have added comments next to some of the parts of the factoriadic_number function for clarity. so that explains what ive tried / want the function to do. although it isnt doing it.
Topic archived. No new replies allowed.