Question

In: Computer Science

string string_long_division (string dividend, string divisor) { string ans; //code in here return ans; } Create...

string string_long_division (string dividend, string divisor)
{
string ans;


//code in here


return ans;
}

Create a function that takes in 2 strings called dividend (is a string because this could be greater than the size of an int) and divisor (is a string because this could be greater than the size of an int) and performs long division on them and returns the result as a string.
It would be preferred if the code was written in c++.
THIS CODE SHOULD WORK IN CASES WHERE THE DIVISOR IS ALSO GREATER THAN THE SIZE OF ANY VARIATION OF AN INT.

For example: dividend = "10000000000000000000000" and divisor = "1000000000000000000000" should be able to work in the code.
Thanks!

Solutions

Expert Solution

Hi friend please find below code and execute it in your C++ editor. This will definitely work as per your wish. If don't please feel free to write it here . I will help you out. For your reference I have attached snapshot of C++ editor Thank you.

Q 1 solution

// C++ program to find quotient and remainder
// when a number is divided by large number
// represented as string.
#include <bits/stdc++.h>
using namespace std;
  
typedef long long ll;
  
// Function to calculate the modulus
void modBigNumber(string num, ll m)
{
// Store the modulus of big number
vector<int> vec;
ll mod = 0;
  
// Do step by step division
for (int i = 0; i < num.size(); i++) {

int digit = num[i] - '0';
  
// Update modulo by concatenating
// current digit.
mod = mod * 10 + digit;
  
// Update quotient
int quo = mod / m;
vec.push_back(quo);
  
// Update mod for next iteration.
mod = mod % m;   
}
  
cout << "\nRemainder : " << mod << "\n";
  
cout << "Quotient : ";
  
// Flag used to remove starting zeros
bool zeroflag = 0;
for (int i = 0; i < vec.size(); i++) {
if (vec[i] == 0 && zeroflag == 0)
continue;
zeroflag = 1;
cout << vec[i];
}
  
return;
}
  
// Driver Code
int main()
{
string num = "10000000000000000000000";
ll m = 1000000000000000000000;
modBigNumber(num, m);
return 0;
}


Related Solutions

Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                            &n
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word and meanings as the table: word meanings bank robber Steals money from a bank burglar Breaks into a home to steal things forger Makes an illegal copy of something...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string and number exception handling, as well as a user defined exception CREATE A NEW PROGRAM that demonstrates Any 2 of the following 3 choices: 1 numeric and 1 string exception make the program require 1 input argument and throw an exception if there are no command line arguments passed in. In the exception, inform the user that an input argument is required. (You can...
Your code needs to do the following: Create a function called pigLatin that accepts a string...
Your code needs to do the following: Create a function called pigLatin that accepts a string of English words in the parameter sentence and returns a string of those words translated into Pig Latin. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. For example the sentence “The quick brown fox” becomes “hetay uickqay rownbay oxfay”. You may assume the words in the parameter...
1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes...
1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative.
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
i have attached my code here and we are supposed to create two classes. one is...
i have attached my code here and we are supposed to create two classes. one is date the other switches accounts for bank and then displays the bank account,type,name,date(pulled from first class and then prints out. i am having issues getting my code to pull from the first class and dont know how to make it read the data from date class if someone can look at my code and tell me how to fix this i would greatly appreciate...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1 } public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrats are equals(same values in the same indices), false otherwise } public boolean areArraysEqual(String[] arr1, String[] arr2){ // 1. initial check: both arrays need to have the same length, return false...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT