Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could someone please comment on this could and tell me what is going on in each

ID: 3839006 • Letter: C

Question

Could someone please comment on this could and tell me what is going on in each line? How does the linked list work? (draw a picture) How does it calculate the factorial to output it onto the screen? What are each of the for loops doing? What is p? What are the things being passed in? Sorry I am really lost on the idea of linked lists.

#include "stdafx.h"

#include<stdio.h>

int mult(int x, int arr[], int size)

{

   int carry = 0, i, p;


   for (i = 0; i<size; ++i)

   {

       p = arr[i] * x + carry;

       arr[i] = p % 10;

       carry = p / 10;

   }


   while (carry != 0)

   {

       arr[size] = carry % 10;

       carry = carry / 10;

       size++;

   }

   return size;

}

int main()

{

   int n, arr[10000], i, size = 1;

   arr[0] = 1;


   printf("Enter any large number:");

   scanf("%d", &n);


   for (i = 2; i <= n; ++i)
   {

       size = mult(i, arr, size);

   }


   for (i = size - 1; i >= 0; --i)

   {

       printf("%d", arr[i]);

   }


   return 0;

}

Explanation / Answer

This program is for calculating factorial (n)!

Normally this program is used to calculate big factorials.

Say the given number is 4.

Looking at the for loop in the main, it is calculauting 1*2*3*4 and storing the result in arr.

The mult function is the simple school mathematics multiplication.

So lets see how it works with n = 4:

First time mult(2, arr,size) is called. Size is one

Looking at mult function:

carry = 0;
for loop runs for only once:
p = 2 * a[0] = 2    (P is the product)
arr[0] = 2;
carry is still 0 so carry while loop does not run. Size is returned as 1

Second time mult(3, arr,size) is called. Size is one

Looking at mult function:

carry = 0;
for loop runs for only once:
p = 3 * a[0] = 6
arr[0] = 6;
carry is still 0 so carry while loop does not run. Size is returned as 1

Third time mult(4, arr,size) is called. Size is one

Looking at mult function:

carry = 0;
for loop runs for only once:
p = 4 * a[0] = 24
arr[0] = 4;
carry is 2 so carry while loop will run. Size is returned as 1
so arr[1] = 2
size is increased by 1 (i.e size = 2)

Main for loop ends

Printing the contents of arr happens:

24.