Anurag sharma
Anurag sharma

Reputation: 39

My system is showing wrong output for correct code and the same code is giving correct output in other device.How to rectify this issue?

I have written the code in c language(for n factorial).

The code is-->

#include <stdio.h>
#include <string.h>

int main()
{
    int n;
    scanf("%d", &n);
    char str[200];
    str[0] = '1';
    int k;
    int t = 0;
    int carry = 0;
    for (int i = 1; i <= n; i++)
    {
        carry = 0;
        for (int j = 0;; j++)
        {
            int arr = str[j] - 48;
            k = arr * i + carry;
            arr = k % 10;
            str[j] = arr + 48;
            carry = k / 10;

            if (carry == 0 && str[j + 1] == '\0')
            {
                break;
            }
            if (carry != 0 && str[j + 1] == '\0')
            {

                for (int r = j;; r++)
                {
                    str[r + 1] = (carry % 10) + 48;
                    carry = carry / 10;

                    if (carry == 0)
                    {
                        str[r + 2] = '\0';
                        t = 1;
                        break;
                    }
                }
                break;
            }
        }
    }
    int len = strlen(str);
    // // printf("%d\n",len);
    char prr[200];
    for (int i = 0; i < len; i++)
    {
        int b = len - i - 1;
        printf("%c", str[b]);
        
    }
    // printf(" %s\n", str);

    return 0;
}

In other systems(including online c compilers) it is showing correct answer

Input=7
Output=5040

In my system(laptop):

Input=7 Output=+,*)'040

My laptop is hp envy 13-ab070TU

os:Windows 10 Home

system type:64-bit operating system, x64-based processor

I have also tried my code on virtual machine in my laptop on ubuntu and kali but the result is same that it is showing wrong output.

What is the reason for this and how I can rectify this issue?

Upvotes: 0

Views: 99

Answers (1)

Steve Summit
Steve Summit

Reputation: 48020

You're filling digits in to your str array, but str is not necessarily a proper, null-terminated string.

At the end, you call strlen(str) to discover how many digits you computed in your result. But since str is not necessarily a null-terminated string, strlen doesn't necessarily get the right answer.

str is a local (stack allocated) variable, and you don't give it an initializer, so it starts out containing unpredictable garbage.

If str happens to start out containing zeroes (which it might), your program will happen to work. But if it contains one or more nonzero bytes, strlen might compute too long a length, so your digit-printing loop at the end might print some extra characters, as you saw on your laptop.

There are two or three ways to fix this.

  1. Call memset(str, '\0', sizeof(str)); to fill the array with 0.
  2. Initialize the array: char str[200] = "";. (It turns out that will fill the whole array with 0.)
  3. Keep track of the number of digits some other way. I suspect it's the maximum value ever taken on by k or r, or something like that.

Upvotes: 1

Related Questions