Question

In: Computer Science

Please, write code in c++. Using iostream and cstring library. Your friend is the person who...

Please, write code in c++. Using iostream and cstring library.

Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue.
Your task is to write a program that which can add two integer numbers that are not more than 101000 to each other.
Your program have to implement void addVeryLongIntegers(char *a,char *b,char *c) function.

Note. The program have to use pointer.

Input:
First line contains a and b numbers that are not bigger that 101000

Output:
The sum of a and b.

example:

input:

222222222222222222222 111111111111111111111

4958439238923098349024 1

output:

333333333333333333333

4958439238923098349025

Solutions

Expert Solution

C++ code

#include<iostream>
#include<string.h>
#define size 1001
using namespace std;

void addVeryLongIntegers(char *a,char *b,char *c){
   int n1 = strlen(a)-1 ;
   int n2 = strlen(b)-1;
   int sum, carry=0,count=0;
      
   while(n2>=0 && n1>=0){
       sum = (*(a + n1) - '0') + (*(b+n2) - '0') + carry;
       carry = sum / 10;
       sum = sum % 10;
       *(c+count) = sum+'0';
       n1--;
       n2--;
       count++;
   }
  
   while(n1>=0){
       sum = (*(a + n1) - '0') + carry;
       carry = sum / 10;
       sum = sum % 10;
       *(c+count) = sum + '0';
       n1--;
       count++;
   }
  
   while(n2>=0){
       sum = (*(b+n2) - '0') + carry;
       carry = sum / 10;
       sum = sum % 10;
       *(c+count) = sum + '0';
       n2--;
       count++;
   }
   if (carry !=0) {
       *(c+count) = carry + '0';
       count++;
   }
  
   int i=0,j=count-1;
  
   while(i<j){
       char temp = *(c+i);
       *(c+i) = *(c+j);
       *(c+j) = temp;
       i++;
       j--;
   }
   *(c+count) = '\0';
  
  
}

int main(){
   char a[size];
   char b[size];
   char c[size];
  
   cin>>a;
   cin>>b;
   addVeryLongIntegers(a,b,c);
   for(int i=0;*(c+i) != '\0' ;i++)cout<<*(c+i);
   return 0;
}


Related Solutions

Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
Please, write code in c++. Using iostream library Most modern text editors are able to give...
Please, write code in c++. Using iostream library Most modern text editors are able to give some statistics about the text they are editing. One nice statistic is the average word length in the text. A word is a maximal continuous sequence of letters ('a'-'z', 'A'-'Z'). Words can be separated by spaces, digits, and punctuation marks. The average word length is the sum of all the words' lengths divided by the total number of words. For example, in the text...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
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...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
Written in C code: Using a structure called person, write a C function called compare_age that...
Written in C code: Using a structure called person, write a C function called compare_age that takes two person structure pointers as input and returns 1 if the first person is older, -1 if the second person is older, or 0 if they have exactly the same birthday. Hint: Compare the years first, then if equal compare months, if they are equal compare days. The structure called person should contain the following; has fields for name (50 char array), dateOfBirth...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT