Beginning Programming-Writing an Algorithm: What's an algorithm?

I have enrolled in a Beginning Programming class at the local CC. It has no prerequisites listed other than English comprehension, etc…
I glanced at the first assignment and it says something to the effect of “Write an algorithm” …something about a 52 card deck.
OK, if there are no Prereqs of note, what am I doing writing an algorithm? Isn’t that something College Algebra level? I graduated with a BA in History, and haven’t had to do College Algebra, I just got to Intermediate Algebra. Can I learn it in two nights? If so, where do I learn this?
This has me mortified!
Can anybody help?
Thanks,
hh

“algorithm” is just a fancy word for “program”. [The resemblance to “algebra” is purely etymological (the ancient mathematician Al-Khwārizmī wrote a book whose title included “al-Jabr”, providing the origin of both words), but that’s the only connection.]

College Algebra probably has nothing to do with your task. Just ignore the word “algorithm”, finish reading what you’ve been assigned to do, and then write a program that does it.

PS, I forgot to add that I haven’t even opened the book, because I won’t get it until Monday, or gone to class-it’s online.
Thanks,
hh

An algorithm is just a series of steps for completing a task. For example, consider the task: put a deck of cards into a random order.

Here’s an algorithm that works:

  1. Divide the deck into two equal piles
  2. Shuffle the two piles together
  3. Repeat several times.
    Bam, you’ve got an algorithm.

Think of it as “a procedure to accomplish a task.” Or a program.

They may want you just to outline the steps to take to solve a problem in pseudo-code, not necessarily in an executable language.

What’s the actual task about the 52-card deck?

Ahhh…a load off of my mind.
You’ve helped very quickly.
Thanks,
grateful hh

An algorithm is a set of instructions to accomplish a task.

You generally implement algorithms in a particular programming language to do what you want to do. Consider a simple math problem, and describe the specific steps you’d take to solve it.

For example, given a list of numbers how do you find the biggest one?

  1. start by assuming that the first number is the largest.
  2. Go through each element in the list and compare it to the largest value you’ve found.
    2a. If it’s larger, it becomes the new largest value found.
    2b. If not, continue.
  3. When you get to the end of the list, you’re done.

It’s a simple algorithm, but being able to break down the process into very specific steps is important for programming.

I don’t know what your algorithm needs to be for the deck of cards, but realize that a deck of cards is really just a list of numbers (and suits, but you can generalize those as numbers, too, depending on what you want to do with them).

Well, I can’t remember: I am away from the computer that I use, and I don’t have my password here. I was so shocked by the A word that I didn’t have a heart to actually read the rest of it, I just sort of glanced at the length of it, and at the next question, and THAT had algorithm in it, and I closed the window in panic, and headed out to go to work! (The word ‘idiot’ comes to mind here…I wonder why?:dubious:)
If I can get to it anytime soon, I’ll get back.
Thanks a lot!
hh

It all is not as scary as you might think. You must remember that computers are really, really simple-minded. They only seem smart because they can run a program so fast.

To tell a computer to do a task (write a program), you must remember that. You can tell a person, “Pick up a pencil and write your name,” and almost anybody can understand that instruction.

To tell a computer to do it, you have to do something such as:

001 See the pencil on the table
002 reach out your arm
003 when on top of the pencil, lower your arm over the pencil
004 stop just above the pencil
005 open your fingers
006 lower your fingers on each side of the pencil
007 close your fingers on the pencil
008 lift your arm with the pencil in your fingers
etc, etc.

The thing is, once the computer absorbs those commands, it will do the instructed task in in a fraction of a second or so.

Therefore, don’t let the thing intimidate you. You are much smarter than the computer. :smiley:

Maybe this won’t help, but if you ever cooked something following a recipe, you also dealt with an algorithm. If you ever built something using Legos and following the directions, that was an algorithm also. Don’t sweat it.

The old Babylonian algorithm for obtaining the square root of N with precission p:

1- S = N/2
2- a = N/S
3- S = (S+a)/2
4- a = N/S
5- IF ABS(S-a) > p GOTO 3
6- PRINT “Square Root of” N “is” S

I would disagree with this. The algorithm is the steps used to solve the problem. The program is the implementation of those steps.

For example, one of the simplest algorithms that is taught to beginning programmers is the “bubble sort”. For a bubble sort, the “algorithm” is that you repeatedly go through the list, constantly checking each item in the list with the next item in the list. If they are in the correct sort order, you leave them alone. If not, you swap the two of them. You have to go through the list as many times as you have elements. In other words, if you have 10 items in your list, you have to go through the list 10 times. Then everything is sorted. If you have 100 items, you have to go through the list 100 times.

You can implement this algorithm in numerous different ways. A C program that does a bubble sort could look like this:



for (i=0; i<n-1; i++) {
  for (j=0; j<n-1-i; j++)
    if (a[j+1] < a[j]) {  /* compare the two neighbors */
      tmp = a[j];         /* swap a[j] and a[j+1]      */
      a[j] = a[j+1];
      a[j+1] = tmp;
  }
}


A BASIC bubble sort would look like this:



2000 REM Bubble sort the list A of length N
2010 FOR I = 1 TO N - 1
2020 FOR J = 1 TO N - I
2030 IF A[J] <= A[J+1] THEN GOTO 2070
2040 LET X = A[J]
2050 LET A[J] = A[J+1]
2060 LET A[J+1] = X 
2070 NEXT J
2080 NEXT I
2090 RETURN


Even though the program is different in each case, the algorithm is the same for both programs.

You could do a bubble sort with a deck of cards in your hands. The algorithm would be the same, but the “program” would be significantly different than a computer program. The algorithm is the step by step method that you use to solve the problem. The program is the implementation of the algorithm.

Get it?

For the first assignment of an introductory programming class, it’s not worth making this distinction.

Giving the guy actual code may confuse him. These algorithm examples are simple only to those who are familiar with code.

And who the hell starts a BASIC program with line number 2000? I mean, come on!

Well, from my perspective as a business programmer, both of those things should scare the OP a bit. Computers are simple-minded. They do what you tell them to do. Exactly. Think that over. :smiley:

I don’t know that that is a standard terminological distinction, but it’s true that this assignment may just be asking for an English or pseudocode description of a program, and not actual source code in some computer language, as pointed out before. But whatever it asks for, I’m sure it’s something the OP will be capable of handling; they just needed to know not to be scared off by the word “algorithm”.

Don’t Panic!

Programming is not particularly about math. It’s more about orderly thinking. I bet you that you already know a bunch of algorithms, though you don’t know them by that name!

For example, you have to have to find the result of the multiplication

11x13 = ?

I dunno about you, but when I was learning multiplication, we did it with pencil and paper with this algorithm:

Put the numbers one over the other



     11
     13
     ==


Multiply the top number by the rightmost number on the bottom:

3 x 11 and write down the result so that the rightmost digit of the result is under the bottom number you just used.



     11
     13
     ==
     33


Now multiply the top number by the next number to the left on the bottom, and write down the result so that the rightmost digit of the result is under the bottom number you just used.



    11
    13
    ==
    33
   11


When you run out of numbers on the bottom, add up the results you put below by columns, just as if you were adding a list of numbers. Blanks become zeros:



    11
    13
    ==
    33
   110
  =====
   143


So 11x13 is 143, using an algorithm for multiplying two numbers. Of course there are others, but this is a simple one.

It’s not important why it works. What’s important is that it’s a good way to multiple together two numbers of a reasonable size (say, out to 3 digits) for any number!

In programming, you’ll learn some other algorithms for solving problems.

Computers are really really good at repeating things lots of times real fast, doing math calculations, and testing things. Mostly what you learn in studying computers is how to chop apart problems into bits that involve repetition, calculation, and testing; that is, how to create and use algorithms.

There’s data as well. That’s fun too, but it’s getting late. Hope this helps.
:smiley:

I think the OP already fell over in a dead faint.:eek:

Strongly disagree here. It is absolutely crucial that beginners learn the distinction ASAP. Computer Science is all about abstraction. And the difference between a program (concrete) and an algorithm (abstract) is lesson 1.

For noobs and such I make the following point: a program is what you tell a computer, an algorithm is what you tell a human.