Integer40 *big40Add(Integer40 *p, Integer40 *q); Description: Return a pointer t
ID: 3856906 • Letter: I
Question
Integer40 *big40Add(Integer40 *p, Integer40 *q);
Description: Return a pointer to a new, dynamically allocated Integer40 struct that contains the result of adding the 40 digit integers represented by p and q.
Special Notes: If a NULL pointer is passed to this function, simply return NULL. If any dynamic memory allocation functions fail within this function, also return NULL, but be careful to avoid memory leaks when you do so.
Hint: Before adding two huge integers, you will want to create an array to store the result. Remember that all integers in this problem are 40 digits long. In the event that the most signicant digits (MSD) result in a carry, the carry will be ignored. For example, if the MSD of the two inputs are 9 and 7, the resultant MSD will be 6 with a carry of 1 for the MSD + 1 digit. (916 +716 =1016, therefore 6 is the MSD and the 1 is ignored.)1
Returns: A pointer to the newly allocated Integer40 struct, or NULL in the special cases mentioned above.
Integer40 *parseString(char *str);
Description: Convert a number from string format to Integer40 format. (For example, function calls, see big40-main01.c.)
Special Notes: If the empty string (“”) is passed to this function, treat it as a zero (“0”). If any dynamic memory allocation functions fail within this function, or if str is NULL, return NULL, be careful to avoid memory leaks when you do so. You may assume the string will only contain ASCII digits ‘0’ through ‘9’ and the letters ’A’ thru ’F’ in either upper or lower case, for a minimum of 40 digits. In the event that 40 digits are not in the input string, print an error message to STDERR and ll with leading zeroes. Also, if there are more than 40 digits in the input string use the rst 40 digits in the string.
Returns: A pointer to the newly allocated Integer40 struct, or NULL if dynamic memory allocation fails or if the input str is NULL.
This is the structure's definition. We aren't allowed to add anything to the following structure.
typedef struct Integer40 {
// a dynamically allocated array to hold a 40
// digit integer, stored in reverse order
int *digits;
} Integer40;
This is a c program. Please Help!!
Explanation / Answer
Answer: See the code of required function below:
------------------------------------------------------
//function to add two Integer40 numbers
//Remember for this function to work digits in both the numbers should be 40,
//otherwise wrong result will be generated.
Integer40* big40Add(Integer40 *P, Integer40 *Q)
{
Integer40 *N;
int i,n,carry=0, digit;
if(P==NULL || Q==NULL)
return NULL;
N=(Integer40*)malloc(sizeof(Integer40));
if(N==NULL)
return NULL;
N->digits=(int*)calloc(40,sizeof(int));
if(N->digits==NULL)
return NULL;
for(i=0;i<40;i++)
{
n=P->digits[i]+Q->digits[i]+carry;
if(count_digits(n)==2)
{
digit=n%10;
carry=n/10;
N->digits[i]=digit;
//condition for MSD
if(i==39)
carry=0;
}
else
{
N->digits[i]=n;
carry=0;
}
}
return N;
}
----------------------------------------
See the example program below and associated output. Pay attention to comments of function
Example program:
---------------------------------------------
#include <stdio.h>
#include <stdlib.h>
//Integer40 structure: an structure to store an integer of 40 digits
typedef struct Integer40 {
// a dynamically allocated array to hold a 40
// digit integer, stored in reverse order
int *digits;
} Integer40;
//function to count digits in an integer
int count_digits(unsigned long long n)
{
int nd=0; //number of digits in number
while(n!=0)
{
nd++;
n=n/10;
}
return nd;
}
//function to extract digits from a number in Integer40 format
void extract_digits(Integer40 *N, unsigned long long n, int nd)
{
int i;
int digit;
for(i=0;i<nd;i++)
{
digit=n%10;
n=n/10;
N->digits[i]=digit;
}
}
//function to print Integer40
void print_Integer40(Integer40 *N, int nd)
{
int i;
for(i=0;i<nd;i++)
{
printf("%d ",N->digits[i]);
}
}
//function to add two Integer40 numbers
//Remember for this function to work digits in both the numbers should be 40,
//otherwise wrong result will be generated.
Integer40* big40Add(Integer40 *P, Integer40 *Q)
{
Integer40 *N;
int i,n,carry=0, digit;
if(P==NULL || Q==NULL)
return NULL;
N=(Integer40*)malloc(sizeof(Integer40));
if(N==NULL)
return NULL;
N->digits=(int*)calloc(40,sizeof(int));
if(N->digits==NULL)
return NULL;
for(i=0;i<40;i++)
{
n=P->digits[i]+Q->digits[i]+carry;
if(count_digits(n)==2)
{
digit=n%10;
carry=n/10;
N->digits[i]=digit;
//condition for MSD
if(i==39)
carry=0;
}
else
{
N->digits[i]=n;
carry=0;
}
}
return N;
}
int main(void) {
unsigned long long p,q; //two integers to add
int np; //number of digits in p
int nq; //number of digits in q
printf("Remember both integers should contain same number of digits. ");
printf("Enter first integer:");
fflush(stdout);
scanf("%llu",&p);
printf("Enter second integer:");
fflush(stdout);
scanf("%llu",&q);
printf("p:%llu, q:%llu ",p,q);
np=count_digits(p);
nq=count_digits(q);
printf("np:%d, nq:%d ",np,nq);
//allocate memory for Integer40 pointers
Integer40 *P, *Q;
P=(Integer40*)malloc(sizeof(Integer40));
Q=(Integer40*)malloc(sizeof(Integer40));
//now allocate memory for digits array in respective pointers
P->digits=(int*)calloc(np,sizeof(int));
Q->digits=(int*)calloc(nq,sizeof(int));
//convert number to Integer40 format
extract_digits(P,p,np);
extract_digits(Q,q,nq);
printf("P:");
print_Integer40(P,np);
printf(" ");
printf("Q:");
print_Integer40(Q,nq);
Integer40 *N=big40Add(P,Q);
printf(" ");
printf("N:");
print_Integer40(N,40);
return EXIT_SUCCESS;
}
--------------------------------
Associated output:
-------------------------------
Remember both integers should contain same number of digits.
Enter first integer:123456789
Enter second integer:123456789
p:123456789, q:123456789
np:9, nq:9
P:9 8 7 6 5 4 3 2 1
Q:9 8 7 6 5 4 3 2 1
N:8 7 5 3 1 9 6 4 2 0 1705509812 402691310 10228977 10223816 668205522 134236793 3 2 9 5 2 9 819200482 201345659 10228970 10223808 -1921251969 536928097 10228985 10223823 668205527 134236796 4 1 6 0 5 9 -1770257002 604036969
----------------------------------------
Note: Pay attention how vacant spaces in result are filled as number being added don't contain 40 digits.
2. parseString() function: See the code below:
-----------------------------------------------
//function to parse a string to Integer40 format
Integer40* parseString(char *str)
{
Integer40 *N=NULL;
if(str==NULL)
return NULL;
N=(Integer40*)malloc(sizeof(Integer40));
if(N==NULL)
return NULL;
N->digits=(int*)calloc(40,sizeof(int));
if(N->digits==NULL)
return NULL;
if(strcmp(str,"")==0)
return 0;
char *st;
int num_leading_zeros,i;
//check whether string is <40 or >40
if(strlen(str)<40)
{
fprintf(stderr,"Not containing 40 characters. Filling with leading zeors... ");
num_leading_zeros=40-strlen(str);
st=(char*)calloc(40,sizeof(char));
for(i=0;i<=num_leading_zeros;i++)
st[i]='0';
strcat(st,str);
fprintf(stderr,"Now string is:%s ",st);
strcpy(str,st);
}
else if(strlen(str) > 40)
{
fprintf(stderr,"Containing more than 40 characters. Extracting 40 characters... ");
st=(char*)calloc(40,sizeof(char));
strncpy(st,str,40);
strcpy(str,st);
}
//store null at last position
str[40]='';
//now parse string
int j=0;
for(i=39;i>=0;i--)
{
N->digits[j]=str[i]-'0';
j++;
}
return N;
}
-------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.