Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please give the correct answer for this programming question in C language, thank you Exercise: BigNum You are required to complete the implementation of the

Please give the correct answer for this programming question in C language, thank you

image text in transcribed

image text in transcribedimage text in transcribed

Exercise: BigNum You are required to complete the implementation of the BigNum so that the add program works as expected. Specifically, you need to provide implementations for the following functions: Note that the following functions use the convention that if the function only reads a struct parameter, then it's passed by value. However, if the intention of the function is to change the value of the struct, then it is passed by reference (i.e. using a pointer). void initBigNum(BigNum *n, int Nbytes) Initialise a BigNum object by setting the byte counter and by creating (malloc'ing) an array of length Nbytes. Use an assert(to check whether the array was successfully created. No return value. void showBigNum(BigNum n) Display the contents of a BigNum on standard output. Ignore leading 'O' digits and then display all digits to the (lower) end of the array. If there are no non-'0'. digits in the array, then print a single 0. Do not print any other characters except the digits. No return value. int scanBigNum(char *s, BigNum *n) Convert a string into a BigNum. Ignores any leading space characters in the string. If the first non-space is a digit, then scan to a non-digit (or end of string) and note where the last digit occurs. Then scan the digit-rich region of the string (i.e. a contiguous sequence of digits) and place them in the appropriate location in the Bignum's array. If the string does not contain a sequence of digits after the spaces, then return 0; otherwise return 1. void addBigNums (BigNum n, BigNum m, BigNum *res) Add the values in n and m and store the result in res. If res is not initially large enough to hold the result, increase its size. No return value. void freeBigNum(BigNum n) Free any memory that was created inside the BigNum. Note do not actually free the struct for the BigNum itself as that is never malloced. We recommend that you implement the functions in the following order: initBigNum(), showBigNum() (possibly inserting some data manually into the digits array), scanBigNum(), addBigNums (). Altogether, it will require around 50 lines of C code. Once the program is working, you should be able to do things like: $ ./add 1000000000 42 Sum of 1000000000 and 42 is 1000000042 $ ./add 987654321987654321987654321 98765987659876598765 Sum of 987654321987654321987654321 and 98765987659876598765 is 987654420753641981864253086 BigNum.c x test1.c x 1 // BigNum.h ... LARGE positive integer values 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include "BigNum.h" 10 // Initialise a BigNum to N bytes, all zero 11 void initBigNum(BigNum *n, int Nbytes) 12 { 13 // TODO 14 return; 15 16 17 // Add two BigNums and store result in a third BigNum 18 void addBigNums (BigNum n, BigNumm, BigNum *res) 19 { 20 // TODO 21 return; 22 } 23 24 // Set the value of a BigNum from a string of digits 25 // Returns 1 if it *was* a string of digits, otherwise 26 int scanBigNum( char *s, BigNum *n) 27 { 28 // TODO 29 return 1; 30} 31 32 // Display a BigNum in decimal format 33 void showBigNum(BigNum n) |34 { 35 // TODO 36} 37 38 void freeBigNum(BigNum n) { 39 // TODO 40 } BigNum.h x 1 / BigNum.h ... LARGE positive integer values 3 typedef unsigned char Byte; 5 typedef struct _big_num { 6 int nbytes; // size of array 7 Byte *bytes; 111 array of Bytes 8} BigNum; 10 // Initialise a BigNum to N bytes, all zero 11 void initBigNum(BigNum *n, int Nbytes); 12 13 // Add two BigNums and store result in a third BigNum 14 void addBigNums (BigNum n, BigNum m, BigNum *res); 15 16 // Set the value of a BigNum from a string of digits 17 // Returns 1 if it *was* a string of digits, 0 otherwise 18 int scanBigNum(char *s, BigNum *n); 20 // Display a BigNum in decimal format 21 void showBigNum(BigNum n); 22 24 // Free all memory associated with a BigNum 25 void freeBigNum(BigNum n)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions