1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Triss [41]
3 years ago
12

Write a function to add two large integers of any length, say up to 200 digits. A suggested approach is as follows: treat each n

umber as a list(array), each of whose elements is a block of digits of that number (say block of 1 to 4 digits, your choice). For example the integer 123456789101112 might be stored with N(1)
Computers and Technology
1 answer:
Gekata [30.6K]3 years ago
5 0

Answer:

see explaination

Explanation:

/ C++ program to find sum and product of two large numbers.

#include<bits/stdc++.h>

#include<cstring>

using namespace std;

// Multiplies str1 and str2

string big_multiply(string num1, string num2)

{

int n1 = num1.size();

int n2 = num2.size();

if (n1 == 0 || n2 == 0)

return "0";

// will keep the result number in vector

// in reverse order

vector<int> result(n1 + n2, 0);

int i_n1 = 0;

int i_n2 = 0;

// Go from right to left in num1

for (int i=n1-1; i>=0; i--)

{

int carry = 0;

int n1 = num1[i] - '0';

// To shift position to left after every

// multiplication of a digit in num2

i_n2 = 0;

// Go from right to left in num2

for (int j=n2-1; j>=0; j--)

{

// Take current digit of second number

int n2 = num2[j] - '0';

int sum = n1*n2 + result[i_n1 + i_n2] + carry;

carry = sum/10;

// Store result

result[i_n1 + i_n2] = sum % 10;

i_n2++;

}

// store carry in next cell

if (carry > 0)

result[i_n1 + i_n2] += carry;

i_n1++;

}

// ignore '0's from the right

int i = result.size() - 1;

while (i>=0 && result[i] == 0)

i--;

if (i == -1)

return "0";

// generate the result string

string s = "";

while (i >= 0)

s += std::to_string(result[i--]);

return s;

}

// Function for finding sum of larger numbers

string findSum(string str1, string str2)

{

if (str1.length() > str2.length())

swap(str1, str2);

// Take an empty string for storing result

string str = "";

// Calculate lenght of both string

int n1 = str1.length(), n2 = str2.length();

int diff = n2 - n1;

// Initialy take carry zero

int carry = 0;

// Traverse from end of both strings

for (int i=n1-1; i>=0; i--)

{

int sum = ((str1[i]-'0') +

(str2[i+diff]-'0') +

carry);

str.push_back(sum%10 + '0');

carry = sum/10;

}

// Add remaining digits of str2[]

for (int i=n2-n1-1; i>=0; i--)

{

int sum = ((str2[i]-'0')+carry);

str.push_back(sum%10 + '0');

carry = sum/10;

}

// Add remaining carry

if (carry)

str.push_back(carry+'0');

// reverse resultant string

reverse(str.begin(), str.end());

return str;

}

// Driver function code

int main()

{

string big_str1 = "1235421415454545454545454544";

string big_str2 = "1714546546546545454544548544544545";

cout <<"Sum of two number: "<< findSum(big_str1, big_str2)<<endl;

cout <<"Mutiplication of two number: "<< big_multiply(big_str1, big_str2);

return 0;

}

You might be interested in
A CPU scheduler that assigns higher priority to the I/O-bound processes than the CPU-bound processes causes:
Sholpan [36]

Answer:

Low CPU utilization and high I/O utilization

Explanation:

6 0
2 years ago
The two types of one-time-password tokens are ______ and ______.
Nutka1998 [239]

Answer:

event based and time based

Explanation:

OTP tokens come in two types: event-based (HOTP) and time-based (TOTP). Event-based OTP tokens generate new codes at the press of the button and the code is valid until it is used by the application. Time-based OTP tokens generate codes that are valid only for a certain amount of time (eg, 30 or 60 seconds), after which a new code must be generated

5 0
3 years ago
Read 2 more answers
Dzięńdobry kto ogarnia komędy w minicraft bardzo dobrze i by mi pomógł w stworzeniu serwera [dostanie w zamian range właścieciel
mojhsa [17]

Playing Minecraft on a public server can take the game to a whole. This will help your kid learn social skills, such as cooperating, while there are some family-friendly servers for the Pocket Edition most server admins can “roll back” your building to the state it was in.

Explanation:

  • For Minecraft, you'll need to forward TCP port 25565 . You'll also need to enter your server's local IP address as the Output IP or Server IP for the forwarded port. This tells the router which device to point at. To find your server's local IP, open a command prompt and enter ipconfig.
  • They are safe for the most part, however they do expose you to the following risks, which can generally be negated by simply joining reputable servers. Some servers may ask users to set up an AuthMe password. If you decide to enter a password here, never ever enter the password of your Minecraft account.
  • You can divide public Minecraft servers into two types:
  1. Whitelisted servers are protected by a whitelist — that is, a list of usernames that are allowed to join the server. To join a whitelisted server, you need to apply to have your Minecraft username added to the whitelist. Typically, this involves filling out a form and waiting a few hours or days.
  2. Non-whitelisted servers do not have a whitelist, which means anyone can join the server simply by entering the server’s address in their Minecraft client, as shown above.
  • Applying for a whitelisted server can be a bit of a drag especially when you have an impatient eight-year-old tugging at your sleeve but it does provide an extra degree of reassurance that all the players on the server are known to the server administrators.
8 0
3 years ago
Who were 4 major people that attended the constitutional convention
postnew [5]
Alexander Hamilton, George Washington, James Madison, and Benjamin Franklin
7 0
3 years ago
WHY IS BRAINLY NOTIFICATIONS LIKE THIS?????
nikdorinn [45]

Answer:

probly the wifi connection

Explanation:

4 0
3 years ago
Read 2 more answers
Other questions:
  • Discuss in detail why you need to use a write blocker (either hardware or software) in your examinations, whether for a criminal
    6·1 answer
  • How can i become an expert in computer and technology?
    15·2 answers
  • Which are factors that go into a project plan?
    8·1 answer
  • A hard disk is a _ storage device
    11·2 answers
  • How many report charts can be added to the account page layout to meet this requirement? A sales manager would like to look at a
    14·1 answer
  • Assume that strikeCounter has already been declared to be a "pointer to int". Assume further that strikeCounter has been initial
    5·1 answer
  • All of the following are helpful test taking strategies EXCEPT_______________.
    5·2 answers
  • What is the output?
    5·1 answer
  • Type the correct answer in the box. Spell the word correctly. A company has its branches spread over five places in a state. It
    14·1 answer
  • The courts can adjust the penalties in lawsuits to reflect the fact that infringements may be _____.
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!