Quiz4_Solution - Solution: %quiz4.m clear N = input(Enter...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Quiz 4: While Loops Name: ASE 201 Write a MATLAB script (BE SURE TO USE MATLAB NOTATION!) that will determine the exact number of colored balls in a sequence of N balls input by the user. There are only 3 possibil- ities: blue, red and yellow. Blue will correspond to ‘B’, red will correspond to ‘R’ and yellow will correspond to ‘Y’. 1. Read in the total number of balls in the sequence N . 2. Read-in the color of each ball individually. 3. Keep track of the total number of each color entered. 4. Displays the total number of each color entered. You may use disp() or fprintf(). Note that unlike the assignment you completed, this program should only be called once.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Solution: %quiz4.m clear N = input(Enter the number of balls in the sequence: ); num_R = 0; num_B = 0; num_Y = 0; k = 0; while k < N k = k+1; color = input(Enter the color of ball: ,s); % switch(color) % case R % num_R = num_R + 1; % case B % num_B = num_B + 1; % case Y % num_Y = num_Y + 1; % end if color == R num_R = num_R + 1; elseif color == B num_B = num_B + 1; else num_Y = num_Y + 1; end end fprintf(\n Number of Red = %g \t Number of Blue = %g \t Number of Yellow = %g \n, . .. num_R,num_B,num_Y)...
View Full Document

Ask a homework question - tutors are online