Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


dpb
Posts:
9,850
Registered:
6/7/07


Re: Writing a function with while and for loops
Posted:
Feb 6, 2013 2:34 PM


On 2/6/2013 12:18 PM, Sara wrote: > I am asked create a function of 2 inputs, the first defining a vector of > random numbers and the second a scalar that sorts the vector in > ascending if the scalar is 1 and descending order if the scalar value is > 2. If only one input is given the function should sort in ascending > order. The function is supposed to use a for loop that is nested in a > while loop. The for loop should compare the vector value by value, > switching the two as necessary to complete the ascending order. The > while loop stops the function once the ascending or descending condition > is met. > > So far I don't have much, but here it is. > > > function BubbleSort = ScottS_BubbleSort(x,y) > %UNTITLED3 Summary of this function goes here > % Detailed explanation goes here > > while for k=x > if k(1) >k(2) %if statement to compare and switch values > > end > end > end > end
Well, you're probably going to have to have make more of a stab at it than that...there are a zillion implementations of bubble sort you can look at to understand the ideaswhen you have a specific Matlab implementation problem, then you'll likely get some help on that but on an openended begging mission, not so much.




