-4

Problem: Children are taught to add multi-digit numbers from right to left, one digit at a time. Many find the “carry” operation, where a 1 is carried from one digit position to the next, to be a significant challenge. Your job is to count the number of carry operations for each for a set of addition problems so that educators my assess their difficulty.

Input: Each line of input contains two unsigned integers less than 10 digits. The last line of input contains “0 “0.

Input Examples: For each line of input except the last, compute the number of carry operations that result from adding the two numbers and print them in the format shown below.

This is the original code that satisfies inputs which are less than 10 digits

#include <iostream>
#include <cstdio>
#include <cstdlib>

int main(void){
unsigned int n1, n2, remain_n1, remain_n2, carry;

while(1)
{
    std::cin >> n1 >> n2;

    if(n1 == 0 && n2 == 0)
        break;

    int carry = 0;
    int count = 0;
    int sum = 0;

    while(n1 != 0 || n2 != 0)
    {
        remain_n1 = n1 % 10;        
        remain_n2 = n2 % 10;         

        if(carry == 1)             
            remain_n1++;          


        sum = remain_n1 + remain_n2;       
        carry = 0;

        if(sum >= 10){                     
            carry = carry + 1;                         
            count = count + 1;                       
        }

        n1 = n1 / 10;                          
        n2 = n2 / 10;                              

    }

    if(count == 0)
        std::cout << "No carry operation." << std::endl;
    else if(count == 1)
        std::cout << count << " " << "carry operation" << std::endl;
    else
        std::cout << count << " " << "carry operations" << std::endl;
}
return 0;
}

The problem input says less than 10 digits, but I want to change to satisfy the condition whatever input comes. And this is my code. How should I fix this?

 std:;string n1, n2;
 while(1){
 std:;cint >> n1 >> n2;
 if(n1 == "0" && n2 == "0")
       break;

 int max_len = n1.szie();
 if (max_len < n2.size())
     max_len = n2.size();

 int nn1[max_len] - {0);
 int nn2[max_len] = {0};

 for(int i = 0; i < n1.size(); i++)
       nn1[max_len - n1.size() + i]; = n1[i] - '0'; 
 }
Son
  • 1
  • 1
  • 2
    II presume that `int nn1[max_len] - {0};` and `szie` and `std:;` and `cint` is a typo – Tas Apr 23 '20 at 12:09
  • `int nn2[max_len] = {0};` is a variable-length array and not a part of C++. You should use `std::vector` https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – Thomas Sablik Apr 23 '20 at 12:14
  • Please fix the typographical errors. Normally you should copy the code directly from your ide so you don't get these types of unexpected typing errors. – drescherjm Apr 23 '20 at 12:14

1 Answers1

1

You don't need any extra storage for numbers, you can use the strings' digits directly and convert as you go.

Something like this, perhaps:

std::cin >> a >> b;

int carry = 0;
int count = 0;

// Iterate in reverse.
for (auto ia = a.rbegin(), ib = b.rbegin(); ia != a.rend() && ib != b.rend(); ++ia, ++ib)
{
    int sum = (*ia - '0') + (*ib - '0') + carry;
    carry = sum >= 10;
    count += carry;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82