Info

This question is closed. Reopen it to edit or answer.

For some strange reason MATLAB spits out two answers to the following code , pay2 =, and p =. It is ony supposed to produce 1 answer

1 view (last 30 days)

The following code is spitting out two answers, and it's only supposed to produce one answer, payment.

Instead it spits out a value for pay2 first as well as one for payment. I only want an answer for payment.

Problem 2 (fare):
                                       Testing with argument(s) 4, 44
    Feedback: Your function performed correctly for argument(s) 4, 44
                                       Testing with argument(s) 1, 20
    Feedback: Your function performed correctly for argument(s) 1, 20
                                       Testing with argument(s) 1, 11
    Feedback: Your function performed correctly for argument(s) 1, 11
                                       Testing with argument(s) 0.2, 67
    Feedback: Your function performed correctly for argument(s) 0.2, 67
                                       Testing with argument(s) 0.9, 55
    Feedback: Your function performed correctly for argument(s) 0.9, 55
                                       Testing with argument(s) 1.45, 17
    Feedback: Your function made an error for argument(s) 1.45, 17
      Your solution is _not_ correct.
    function payment = fare(miles,age)
        pay1 =0;
        pay2=0;
        pay3=0;
        if miles <= 1
            pay1 = 200;
            payment = pay1;
        elseif miles <=20
            pay1= 200;
            pay2 = (miles-1)*0.25*100
            payment = pay1+pay2;
        elseif miles>20
            pay1 = 200
            pay2 = (miles-1)*0.25*100
            pay3 = (miles-10)*0.10*100
            payment = pay1+pay2+pay3;
        end
        payment = (pay1+pay2+pay3)/100;
        if age <=18 || age>=65
            payment = (pay1+pay2+pay3)*0.8/100;
        else
            payment=(pay1+pay2+pay3)/100;
        end
    end
  1 Comment
Stephen23
Stephen23 on 27 Nov 2016
Duplicate:
https://www.mathworks.com/matlabcentral/answers/314071-i-m-getting-two-answer-from-my-function-i-only-want-one-how-do-i-fix-this

Answers (2)

Steven Lord
Steven Lord on 27 Nov 2016
You have several lines inside the elseif sections of your code that do not end in semicolons. Add the semicolons at the end of those lines to suppress the display.

DJ V
DJ V on 26 Nov 2016

payment = fare(1.45,17)

pay2 =

   11.2500

payment =

    1.6900

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!