Function Subtracts Two Nonnegative Integers
    6 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Sophie Culhane
 le 27 Oct 2020
  
    
    
    
    
    Modifié(e) : Cris LaPierre
    
      
 le 27 Oct 2020
            My task is to create a MATLAB function that subtracts two nonnegative integers. x, y, and x_minus_y are row vectors representing nonnegative integers. I have a program that works for most cases except for some such as 'ex6(7500,7499)'. I am not sure why the program is not working for this case. Here is what I have:
function x_minus_y = ex6(x,y)
%
%
x = [0 x];
xLength = length(x);
yLength = length(y);
y = [zeros(1, xLength - yLength), y];
x_minus_y = x - y;
for position = xLength:-1:2
    if x_minus_y(position) < 9 
        x_minus_y(position) = x_minus_y(position) + 10;
        x_minus_y(position - 1) = x_minus_y(position - 1) + 1;
    end
end 
if x_minus_y(1) = 0   
    x_minus_y = x_minus_y(2:end);
end
0 commentaires
Réponse acceptée
  Cris LaPierre
    
      
 le 27 Oct 2020
        
      Modifié(e) : Cris LaPierre
    
      
 le 27 Oct 2020
  
      For your if conditional, use two = for comparison.
if x_minus_y(1) == 0   
0 commentaires
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

