TRENDING NEWS

POPULAR NEWS

How To Write A O N Algorithm

How do I write an algorithm?

An algorithm for what?That is the really important question. Find out exactly what needs to be done. Find out what you may safely assume. What are the inputs? What do you know about the input? What output do you need?Then, try to break down into small parts what needs to be done. For each of the parts, try to think of a way of accomplishing this. Study lots of existing algorithms, they might give you inspiration for other tasks.Do you need some specific data structures to store the input? To store temporary data? For the output?Then, tie it all together.Once you have a working algorithm, take a breath. You’re doing fine. The next very important bit: how does your algorithm deal with “edge cases”? These are inputs that are “special”. For example, for a sorting algorithm, what happens if you give it an empty list? What happens with a list with only one element? Or if all elements are the same? Does your algorithm still work?As you get more experience, you will start taking care of these cases while coming up with your original solution. Don’t worry too much if that is not the case yet — you will need practice, and with practice you will get better at this.The next step is where you need to be creative. Can you think of a better way of doing it? For this, it might help to analyze what the “time complexity” of your solution is. If you give it an input that is twice as large, will it take twice as long? Four times as long? Can you give an explanation of why it cannot be done faster?Maybe you could use a different data structure and that might help? For example, given a list, finding out if any element occurs twice might take quadratic time if you just look at the first element, check if it occurs anywhere else in the list; then check the second element, and so on. But, you could sort the input (which takes [math]n \log(n)[/math] time using a clever sorting method) and then you can just go through the list, checking if two elements next to each other are the same. Much faster!This kind of thing needs more practice. Study various algorithms and data structures.Good luck!

How to write algorithm?

The above poster is correct, but I think it can be done with an average of 8/3 comparisons.

First compare a,b then compare b,c. If b lies between a and c, then b is the median and we are done after 2 comparisons.

Otherwise, b is either largest or smallest, and we need to compare a and c to find out which is the median. Thus in these cases 3 comparisons are needed.

To find the average number of comparisons, notice that the probability b is the median is 1/3 (since all elements are equally likely to be the median), so the expected number of comparisons is: (1/3)*2 + (2/3)*3 = 8/3.

Note that in finding the median, we have also sorted the elements. However for larger sets, it is not necessary to sort the elements -- we can find the median more quickly in other ways (eg: algorithms by Hoare, and by Blum et al)

How do I write a good algorithm?

It depends on how you define the word “good”. It depends on the model of computation you use and your objectives.Take the simple example of reducing time complexity of an algorithm (assuming RAM model). Your algorithm’s complexity is lesser than those of all existing algorithms doesn’t necessarily mean that it is practical to implement because Big-Oh notation swallows constants and can be misleading - like O(1) doesn’t necessarily mean that it is super-fast.If you want to invent an algorithm for a problem, you have to come up with it first, prove the correctness, run-time bounds on time, space, disk accesses, cache hits and misses and so on depending on the underlying model of computation. In case of Quantum Computing Algorithms you might have to derive the bounds on number of gates needed and deal with noise issues and so on. It is math, math and lots of math!

Can you help me write an algorithm?

Start:
Enter ItemName
Enter ItemQuantity
Enter ItemUnitsPerBox
Calculate FullBoxes
Calculate ItemsRemaining
Output ItemName
Output FullBoxes
Output ItemsRemaining
End:

This is the top line algorithm. The 'Calculate' part should now be expanded

Calculate FullBoxes
Start:
FullBoxes = Integer(ItemQuantity / ItemUnitsPerBox)
End:

Calculate ItemsRemaining
Start:
ItemsRemaining = ItemQuantity - (FullBoxes * ItemUnitsPerBox)
End:

NB: At the algorithm stage you are not interested in an exact mathematical expression for finding an integer result or a remainder result. This depends on what programming language is used to implement the algorithm. Most programming languages use the functions INT and MOD for these.

How do I write an algorithm for functions in C?

Writing an algorithm means you are just representing the main logic either using texts or pseudo code.I would prefer to write pseudo code as algorithm.ex: function for adding 2 numbers.Sum(a,b) here Sum is an algorithm that takes two arguments a and b and return their sum/addition.Startstep 1: take input astep 2: take input bstep 3: declare a variable c of integer typestep 4: add a and bstep 5 : store the result in cstep 6: return cStopusing pseudo-code is easy as:Algo sum(a,b)____________here Sum is an algorithm that takes two arguments a and b and return their sum/addition.Startstep 1: take input astep 2: take input bstep 3: c←a+bstep 4: return cStopboth the way of writing algorithm represents the same meaning, writing pseudo-code just saves time.actual c language codes for Sum function:int Sum(int a, int b){int a,b,c;c=a+b;return c;}

How to write an algorithm to add up all odd numbers between 0 and 100.?

some pseudo code:

n=0
for (i = 1 to 50)
{
n=n+2*i-1
}


if you want to be clever, you can notice that adding up the first n odd numbers gives you n^2,
so you get 50^2 = 2500 instantly, without any programming.

What are the rules in writing an algorithm?

Rules for constructing an AlgorithmWhen we are going to create an algorithms, the following points must be keep in mind:Input: There should be some inputs which can be applied in algorithm.Output: At least one result is to be produced.Definiteness: Each step must be clear and unambiguous.Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate after a finite number of steps.Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in principle carry it out. In addition, not only each step is definite, it must also be feasible.Comment Session: Comment is additional info of program for easily modification. In algorithm comment would be appear between two square bracket []. For example: [ this is a comment of an algorithm ].For more details: AlgorithmLet's understand how to create an algorithm with example:Q. Write algorithm to calculate the sum and average of two numbers.Ans.

[procedure for calculate sum and average of two numbers]
Step1 : Start
Step2 : Read two numbers n,m
Step3 : Calculate sum=n+m
Step4 : Calculate avg=sum/2
Step5 : Print sum,avg
Step5 : Stop
[End of procedure for calculate sum and average of two numbers]

I need help writing an algorithm?

number of items purchased = n
price of item = p

Total Price = n * (p + 0.07*p)

ex: if you have purchased 5 items from item A which cost 100$
so the tax is 7$ on the item

and hence the cost of the item is 107$
and u have 5 items
so 5 * 107 is the total cost

i hope i understand you well

Thanks

How to write this algorithm in Microsoft word?

Microsoft Word is a word processor. I think the assignment is asking for you to write the algorithm in "words", not "Microsoft Word." This is known as writing pseudo-code. It's not code that any compiler or interpreter can use, but it describes the actions in code-like detail. You can use any word processor or even text editor to do this.

Beyond that, you seem to want someone here to write the pseudocode for you. Sorry, but that's not how Yahoo! Answers works.

TRENDING NEWS