Easy(Small Factorial): Code runs well but chef not happy :(

Hi, I’ve written my code according to the tutorial on the problem and when I’ve run it against the sample and larger numbers ~100 it works. The code isn’t being accepted by Chef, just says WRONG. I’ve checked for OBOB and printing errors but I can’t fathom whats wrong.

Any help would be useful.
Note: some multiplications and underscores seem to have been edited out…it should be multiply_digit, init[index]*next etc

include 
include 


/*
    Helper program to factorial.
    Program to print vector of digits of a number
    Pre-Condition: number expressed in the vector starts from
        least significant digit to most
    Post-Condition: console shows expressed number and cursor on
        next line
*/
void extractAns(std::vector digits){
    for(int i = digits.size()-1; i > 0; --i)
    {
        printf("%i", digits[i]);
    }
    printf("%i\n", digits[0]); // Move to next line
}

/*
    Helper program to factorial.
    Program to multiply a number, expressed as vector of digits
    from least significant to most, by a small integer. The vector
    increases in size as neccesary and is ammended by the program.
    Pre-Condition: number expressed in the vector starts from
        least significant digit to most.
*/
void multiply(std::vector &init, int next){
    int temp = 0;
    int index = 0;
    while((index < init.size()) || (temp != 0))
    {
        int multiply_digit = init[index]*next + temp;
        init[index] = multiply_digit % 10;
        temp = multiply_digit/10;
        if((index == init.size()-1) && (temp != 0))
        {
            init.push_back(0);
        }
        ++index;
    }
}

/*
    Program that utilises two helper functions to calculate
    the factorial of a small number ~100. This is done by representing
    the number as a vector of digits starting from 1 and multiplying all
    the numbers up to our input. The answer is printed to the console.
*/
void factorial(int input){
    std::vector here({1});
    for(int i = 2; i <= input; ++i)
    {
        multiply(here, i);
    }
    extractAns(here);
}

int main(){
    int no_of_tests;
    scanf("%i", &no_of_tests);
    int input(0);
    for(int i = 0; i < no_of_tests; ++i)
    {
        scanf("%i", &input);
        factorial(input);
    }
}

```

your program is showing ans zero for all input :confused:

it works for me…I’ve copy and pasted my code above again to make sure. Here’s proof

mingw32-g++.exe -std=c++0x -g -c C:\Users\Andrew\Documents\CodeChef\small_factorial2.cpp -o C:\Users\Andrew\Documents\CodeChef\small_factorial2.o
mingw32-g++.exe -o C:\Users\Andrew\Documents\CodeChef\small_factorial2.exe C:\Users\Andrew\Documents\CodeChef\small_factorial2.o “C:\Program Files (x86)\CodeBlocks\MinGW\lib\libSDL.dll.a” “C:\Program Files (x86)\CodeBlocks\MinGW\lib\libSDLmain.a” “C:\Program Files (x86)\CodeBlocks\MinGW\lib\libmingw32.a”
Process terminated with status 0 (0 minutes, 3 seconds)
0 errors, 0 warnings (0 minutes, 3 seconds)

Checking for existence: C:\Users\Andrew\Documents\CodeChef\small_factorial2.exe
Executing: C:\Program Files (x86)\CodeBlocks/cb_console_runner.exe “C:\Users\Andrew\Documents\CodeChef\small_factorial2.exe” (in C:\Users\Andrew\Documents\CodeChef)
Process terminated with status 0 (0 minutes, 13 seconds)

check yourself

its showing wrong ans

2 Likes

Thank you. This helped me narrow it down. Needed to put c++11. Goes through correct.
No idea why though.
Thank you again

Figured it out,
std::vector here({1}); // only works in c++11
std::vector here(1, 1); // works in both c++98 and c++11

//