Related questions
Concept explainers
(Haskell) Let us use the list [d1, d2, ..., dn], where each di is between 0 and 9, to represent the (positive) big-integer d1d2...dn.
For example, [9, 9, 9, 9, 9, 9, 9, 9, 9, 8] represents the big-integer 9999999998. Fill out the implementation for
so that it takes two integer lists, where each integer is between 0 and 9 and returns the list corresponding to the addition of the two big-integers. Again, you have to fill in the implementation to supply the appropriate values to f, base, args. You should get the following behavior:
You may find the integer functions div and mod to be helpful here.
Your implementation should not be recursive.
Note about BigInts: we expect the result of bigAdd to not have any leading zeroes, like [0, 1, 0, 9, 9, 8]. The number zero should be represented as [].
Code:
import Prelude hiding (replicate, sum)
import Data.List (foldl')
foldLeft :: (a -> b -> a) -> a -> [b] -> a
foldLeft = foldl'
--------------------------------------------------------------------------------
-- | `bigAdd n1 n2` returns the `BigInt` representing the sum of `n1` and `n2`.
--
-- >>> bigAdd [9, 9] [1, 0, 0, 2]
-- [1, 1, 0, 1]
--
-- >>> bigAdd [9, 9, 9, 9] [9, 9, 9]
-- [1, 0, 9, 9, 8]
bigAdd :: BigInt -> BigInt -> BigInt
bigAdd l1 l2 = removeZero res
where
(l1', l2') = padZero l1 l2
res = foldLeft f base args
f a x = error "TBD:bigAdd:f"
base = error "TBD:bigAdd:base"
args = error "TBD:bigAdd:args"
--------------------------------------------------------------------------------
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 3 images
- This is my c++ homework, and I get a question about the following code written by the professor. I did not understand the "p++" in the code, and I do not understand why we have to write this. I think we can just simply assign *pn=n then will be okay? why we write p++? Please explain to me, thank you. // more pointers #include <iostream> using namespace std; int main () { int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) cout << numbers[n] << ", "; return 0; }arrow_forward5. Write a C++ function which accepts an int array and the array size as arguments. The function should dynamically create a new array which is the same size as the array passed in. Each element in the new array should be double the corresponding element in the array passed in. Return a pointer to the new array. E.g. if you pass in the array {2, -3, 5} of size 3, it should create a new array of size 3 with values {4, -6, 10} and return a pointer to it. int *doubler(int a[], int size) 6. Consider the following line of code: int temps[3] = {68, 72, 62}; - What does *temps contain? - What does *(temps+1) contain? - What does temps[1] contain? 7. The _____ function returns true if the character is an upper case letter of the alphabet: a) toupper() b) isupper() c) isalpha() d) isdigit() e) none of these 8. The _____ function converts a C-string to an int: a) strlen() b) length() c) len() d) atoi() e) none of thesearrow_forwardWrite in C++ When you're processing data, it’s useful to break up a text string into pieces using a delimiter. Write a function split() that takes a string, splits it at every occurrence of a delimiter, and then populates an array of strings with the split pieces, up to the provided maximum number of pieces. Function specifications: Name: split() Parameters (Your function should accept these parameters IN THIS ORDER): input_string string: The text string containing data separated by a delimiter separator char: The delimiter marking the location where the string should be split up arr string array: The array that will be used to store the input text string's individual string pieces arr_size int: The number of elements that can be stored in the array Return Value: int: The number of pieces the input text string was split into Note: No input will have delimiters in the beginning or the end of the string. (Eg: ",apple, orange" OR "apple, orange,") No input will have multiple...arrow_forward
- (Generic binary search) Implement the following method using binary search. public static <E extends Comparable<E>> int binarySearch(E[] list, E key) Note:- Please type this java code fully running and also need an output for this given code.arrow_forwardplz help with c++....and keep output same as given and paste indented code plzzarrow_forwardHello everyone. I have a questionWrite a program that accepts a 7-9 digit integer and echoes the number with commas between every three digits from the right c++. Note: Do not use arrays, for loops, functions, pointers, or while loops. Only use <iostream>. This problem was designed to highlight the difference between integer and real division. I have a problem when I input number 10000000, output 10,0,0. I want to 100,000,00 but I do not how to code. Can you help me and fix it? Thank you so much. And this is my code #include <iostream> using namespace std; int main() { int leadingDigits, middleDigits, lastDigits; int tempValue, original; cout<<"Please enter 7- to 9-digit number.\n"; cin>>original; tempValue = original / 1000; lastDigits = original % 1000; //add code leadingDigits = tempValue / 1000; middleDigits = tempValue % 1000; //add code cout<<"The number with commas is "<<leadingDigits;...arrow_forward
- Refer to imagearrow_forward(Attach Python file only) Write a program that does the following 1- Declare the list my_list that has the following integer numbers 6, 5, 1, -3, 10, 0, and 4 as items of the list. 2- Modify the value of list items based on the following If the item of the list is an integer and less than three, increment its value by 2. If the item of the list is an integer is greater than or equal to three, decrement its value by 3. 3- after modification, print out the items of the list that have only the even index in the reverse order. A- В I T Ff ♥ = E E E !arrow_forwardPls help answer this question in C++ code using pointers and arrays of pointers. ANSWER NEEDED ASAP I WILL UPVOTEarrow_forward
- Question 2: multiply Problem statement In this question first we will practice function overloading and then function templates. Please follow below instructions. We can extend multiplication easily for string types if we interpret the operation as repetition. For example "code" * 3 may be interpreted as "codecodecode". In fact, languages like Python already support this operation. Write a C++ function named multiply that can multiply(repeat) an std::string by a given integer number and return the repeated string. Write another C++ function named multiply that can multiply two given integer (int type) numbers and return the product as an integer. Write another C++ function with the same name that can multiply a floating point number (double type) by a given integer number and return the product as a floating point number. We defined three functions with the same name without a problem. It is either because they have a different number of parameters, or because any of their...arrow_forwardQuestion: First write a function mulByDigit :: Int -> BigInt -> BigInt which takes an integer digit and a big integer, and returns the big integer list which is the result of multiplying the big integer with the digit. You should get the following behavior: ghci> mulByDigit 9 [9,9,9,9] [8,9,9,9,1] Your implementation should not be recursive. Now, using mulByDigit, fill in the implementation of bigMul :: BigInt -> BigInt -> BigInt Again, you have to fill in implementations for f , base , args only. Once you are done, you should get the following behavior at the prompt: ghci> bigMul [9,9,9,9] [9,9,9,9] [9,9,9,8,0,0,0,1] ghci> bigMul [9,9,9,9,9] [9,9,9,9,9] [9,9,9,9,8,0,0,0,0,1] ghci> bigMul [4,3,7,2] [1,6,3,2,9] [7,1,3,9,0,3,8,8] ghci> bigMul [9,9,9,9] [0] [] Your implementation should not be recursive. Code: import Prelude hiding (replicate, sum)import Data.List (foldl') foldLeft :: (a -> b -> a) -> a -> [b] -> afoldLeft =...arrow_forwardUse C++arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education