Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:48:28+00:00 2026-06-11T15:48:28+00:00

I have the following problem. Suppose I have to arrays X and Y whose

  • 0

I have the following problem. Suppose I have to arrays X and Y whose entries consist of integers in the range 0 through 9 like

X = [1 2 3]
Y = [7 0 9]

I want to think of these arrays as being the digits of base-10 numbers, so X represents the number 123 and Y represents the number 709. I want to write a program which outputs the digits of the sum of these integers. So in the example given, it should output the array

Z = [8 3 2]

since 123 + 709 = 832. For the sake of this question it suffices to assume that X and Y have the same length, i.e., that the numbers they represent have the same number of digits. While I am doing this, I also want to keep track of carries which were performed during the addition. So in the example, I’d also want to output

C = [0 0 1]

which represents that I had to carry a 1 when I added the digits 9 + 3 = 12, but that the additions of the digits in other positions went through without carries. So my main question is

  • Does anyone know of a simple way to achieve this goal using MATLAB?

So far, what I’ve come up with is the following code which is given the two numbers as X and Y

clear all; clc;

C = zeros(1, length(X));
for j=length(X):-1:1
    if X(j) + Y(j) > 9
        C(j) = 1;
    end
    Z(j) = mod(X(j) + Y(j), 10);
    if j < length(X)
        if Z(j)  + C(j+1) < 9
            Z(j) = Z(j)  + C(j+1);
        else
            Z(j) = mod(Z(j) + C(j+1), 10);
            C(j) = 1;
        end
    end
end

if C(1) == 1
    Z = [1 Z];
end

Z
C

The problem is that the code only works sometimes. For example, it has no problem working through the example I gave above, that 123 + 709 = 832 with a “carry array” of [0 0 1]. But the input X = 52514 and Y = 41525 does not yield the correct results. So my follow up question is

  • Does anyone see the bug in the code above/how can I fix it?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:48:29+00:00Added an answer on June 11, 2026 at 3:48 pm

    You need to change the line

    if Z(j)  + C(j+1) < 9
    

    to

    if Z(j)  + C(j+1) <= 9
    

    Then it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have the following problem - we want to read a set of
Propose to consider the following problem. Suppose we have some composite object. Are there
I am using GWT/JAVA for development. I have following problem: I want to remove
if have the following problem: I have a List which i am going through
suppose you are given the following problem. You have two index sets that have
I have the following problem with QT model/view framework. I want to render a
I have following problem where I have to group different transactions into timeslots. Suppose
I have a trivial, but irritating problem in Java. Suppose we have the following
I have following problem: I have to make a ASP.NET Webapplication with a two-row
I have following problem: I have built a tabbar application with 4 tabs. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.