Is This Algorithm for Finding the Largest of Four Numbers Correct?

  • Thread starter Thread starter jackson6612
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
The discussion centers on evaluating an algorithm designed to find the largest of four numbers, A, B, C, and D. Participants critique the algorithm's complexity, suggesting a simpler method that requires only three comparisons by first determining the largest of A and B, then comparing that result with the largest of C and D. The conversation highlights the efficiency of this approach and the minimum number of comparisons needed, drawing parallels to knockout tournaments. Additionally, there is a focus on the clarity and structure of pseudocode, with some participants sharing their own versions and seeking guidance on terminology. Overall, the thread emphasizes the importance of simplicity in algorithm design while ensuring clarity in communication.
  • #31
Not to go completely off track, but if all you want is to study algorithms, there's no reason you would have to start with one of the C based languages(C, C++, Java, ...). Basic was my first language, and the first of many other programmers I know. You can learn standard Basic, which as many implementations by many different organisations, or Visual Basic(VB), a M$ invention, that allows for making click and drag GUIs. If you want GUI from the beginning, VB is the way to go. Making a text box in VB is as easy as selecting from a list and dragging it to where, on the form, you want it, then double clicking to edit what happens when the text changes.
 
Technology news on Phys.org
  • #32
Thank you very much, everyone, RCG, JT, Edgardo, Grep, Tyler, Borek, Mark. I'm much obliged to all of you.

A compiler translates human readable code in some high-level language to machine code that can run on a specific processor (CPU).

Yes, it could be said it is readable but I don't think it's understandable unless you know what those extra detail and parameters would mean when pseudo-code (pseudo-code is readable and understandable by a non-programmer) is converted into language code (language code is only understandable by the programmer) which in turn is translated into machine code by the compiler. Am I right?

Even the specific case is just the general case with an unrolled loop, in a manner of speaking.
What is an "unrolled loop"?

In this case, you don't even have a list (array or vector) to begin with, just four individual variables.

Fair point, but I don't think it matters much. A teacher who would take marks away for using an array type of construct in this case would be a very poor one, IMO (and I really hope OP doesn't have one like that). Anyways, not much difference between assigning them letters or numbers. We could consider them stored in an array such as number[A], number, etc and use the letter as the index, as far as the logic is concerned.


What are you guys saying? Please let me know.

You can assume there are certain basic operations available to you. Comparing numbers like that is a basic operation in any computer. I doubt you're expected to work out an algorithm for that. It's implemented in the hardware of the computer, not the software.
As is BIOS software is part of the hardware? Does a processor have a fixed permanent memory chip which holds BIOS-like software in it for simple operations such as the difference between two numbers to be done by the processor?

Are there C++ compilers which could automatically convert pseudo-code into language code?

Okay, I don't want GUI feature. What simple C++ compilers would you recommend me then? I use Win XP Pro. It could be freeware or shareware but should be flexible and easy.

Any simple and straightforward book on C++ for a layman?

Thank you very much for all the help. I understand that it takes a lot of time and energy to help others, especially in such a kind way.
 
  • #33
jackson6612 said:
What is an "unrolled loop"?

What are you guys saying? Please let me know.

This is what happens when programmers start talking in programmer-speak around non-programmers. :rolleyes: Don't worry too much about it yet. This sort of thing happens in physics threads too... people come into a thread and start talking to each other without regard for the original questioner's level of expertise.
 
Last edited:
  • #34
1: High level languages, such as C++, as said to be human readable. To some they are not readable, but in contrast to machine language, they are to most programmers.

2: An unrolled loop is a complex compiler technique. It's nothing to worry about unless you learn assembly or write a compiler.

3: Arrays, lists, and vectors are just ways to store multiple variables in a list. Google "C++ array."

4: The BIOS is wholly unrelated. It prepares the mobo, cpu, and other components, gets them into normal operating modes, and other things. Then loads the OS.

4.5: The logic to test the relation of two numbers is literally built into the processor. It does it with circuitry.

5: Wouldn't that not be a C++ compiler anymore? A compiler that compiles pseudo-code would be a psuedo-code compiler. A pseudo-code compiler cannot exist. Pseudo-code, by definition, is meant for humans only, and if it is compilable it is no longer pseudo-code.

6: If you have 2+ GiB of RAM, get M$ Visual Studio Express Edition, if not, get CodeBlocks. Both are free.

7: I've never read any C++ books, so I don't know. I learned what I know from numerous sources throughout the internet. cprogramming.com has a set of tutorials that range in skill level from nonprogrammer-beginner to intermediate.
 
  • #35
What are you guys saying? Please let me know.[/quote]

This is what happens when programmers start talking in programmer-speak around non-programmers. :rolleyes: Don't worry too much about it yet. This sort of thing happens in physics threads too... people come into a thread and start talking to each other without regard for the original questioner's level of expertise.[/QUOTE]

Ya, you are quite right. That's the reason I keep on reminding you guys that I'm neither a student of science nor of... But it seems you forget it soon!:smile:
 
  • #36
jackson6612 said:
pseudo-code
pseudo code is normally used to document an algorithm, it's an option to using a flowchart and can be editted much more easily. There are some flow charting tools that input a very "pseudo code" like langage and produce a flow chart.

What is an "unrolled loop"?
This can be done by the programmer as well as the compiler. I haven't seen many C or C++ compilers that unfold loops, or at least unfold larger loops. What I have seen is loops used to move data replaced with move string instruction sequences. Example of unfolded loop:

original code

Code:
    for(i = 0; i < n; i++){
        a[i] = b[i] + c[i];
    }

unrolled loop when n is fixed

Code:
    a[0] = b[0] + c[0];
    a[1] = b[1] + c[1];
    // ...
    a[n-1] = b[n-1] + c[n-1];

Duff's device version for when n is dynamic

Code:
    switch(n){    // max value for n is 100
      case 100:
        a[99] = b[99] + c[99];
      case 99:
        a[98] = b[98] + c[98];
      case 98:
        a[97] = b[97] + c[97];
      // ...
      case 2:
        a[1] = b[1] + c[1];
      case 1:
        a[0] = b[0] + c[0];
      case 0:
        break;
    }

As is BIOS software is part of the hardware? Does a processor have a fixed permanent memory chip which holds BIOS-like software in it for simple operations such as the difference between two numbers to be done by the processor?
A BIOS is normally read only memory at a fixed location used to boot up a computer and provide some basic I/O services. For boot up on a PC, the BIOS reads the first sector of a bootable disk and branches to the location the sector was read into (7c00 hex in memory). That code moves typically moves itself to lower memory (0600 hex in memory) and repeats the process, eventually reading sectors using BIOS calls (INT 21h) to read in a loader, such as MSDOS.SYS or NTLDR, which reads in the actual OS code and branches to that.

Some IBM mainframes use micro-code to implement a common set of instructions that are not natively available on all versions of the mainframe. Older PC's used software to emulate floating point instructions if a floating point processor was not available.
 
Last edited:
  • #37
Okay, some questions. As it is obvious from your examples that there could be several ways to write an algorithm. Likewise, are there also numerous ways to write a pseudo-code? What about the language code, are there more than one way to write it? Please help me with these queries.
 
  • #38
There is not one standard form for pseudocode that I'm aware of. For a given style of pseudocode, it's very likely that two different people will come up with at least slightly differing pseudocode. When it's time to write what you're calling language code (and what programmers just call code), different people will usually come up with different implementations (code) for a given algorithm.

There is almost always more than one way to write the code that implements some algorithm. Some of these ways will be better, by some measure (code speed, code size, maintainability, etc.) and some will be worse. In many companies, code is reviewed by peers and others in an effort to improve code that needs it.
 
  • #39
I offer my thanks and best wishes to everyone who has helped me. Thank you.

Please remember that I'm neither a student of comp. science nor of science/math. So, I request you to keep your reply simple.

Before I go on to ask some follow-on questions related to the already discussed material, I would like to make new related queries. What is an 'assembler' or 'assembly language'? What is a 'high-level language', is there also some 'low-level language'? Please keep your replies simple. Thanks.
 
  • #40
jackson6612 said:
What is an 'assembler' or 'assembly language'? What is a 'high-level language', is there also some 'low-level language'? Please keep your replies simple.
Assembly language is a low-level language that is one step up from machine language, the strings of 0's and 1's that the central processing unit can work with. There are many different kinds of assembly language, one for each processor type. For example, x86 assembly language is used on the Intel family of CPUs. A completely different kind of assembly language was used on the Motorola 68x00 family of processors that were the heart of Apple Mac computers for a long time. MIPS assembly language is yet another one.

Each assembly language uses a set of instructions (op codes) to carry out a variety of operations such as moving a value from a memory location to a register in the CPU, jumping to a memory location, incrementing the value in a register or memory location, and so on. Some of the instructions used in x86 assembly are MOV, JMP, and INC.

An assembler is another kind of translater, similar to compilers and interpreters. An assembler (for a particular assembly language) translates the op codes and their operands (some instructions take one or more operands) into machine code (also called object code) that can be executed by the CPU. There is usually another step that must be taken - using a linker, which combines one or more object code files into an executable program.
 
  • #41
Assembly language at it's lowest level is machine language but using text strings to represent instruction opcodes, names for variables, and labels for instruction locations. At assembly time, the variables and labels are assigned specific addresses or offsets, and those addresses or offsets are combined with the generated instruction opcodes to create actual machine language. Some assemblers have a macro feature that is similar to a defined function in higher level languages. Some assemblers include "directives" which incorporate low level language features, such as conditionals (.if ... .elseif ... .endif).

A high level language is more human friendly and generally cpu independent, although some features may be cpu oriented, such as declaring parallel operations. Cobol is a classic high level language, with some powerful operations such as "move corresponding" which moves and formats fields of data based on input and output structure descriptions, on a common name by name basis. Fortran is another example of a high level language, but doesn't have the high end operators of Cobol. APL is also a high level language, but uses single greek symbols for it's operators and is difficult to read. Java and Python would be examples of more modern high level languages. Matlab is more of a mathematical interactive tool than a typcial high level language and includes a very large set of features.

Classic C can be considered a low level language compared to Fortran, although the main missing mathematical operator is exponentiation, which is implemented as a function (pow(...)) in C. Other common operations like input and output are also implemented as functions in a common library as opposed to being part of the native language. Mathematical expressions can be written on a single line and C will convert this into a series of machine instructions. Almost all C compilers support a common set of libraries for math, memory allocation, and I/O operations.

C++ adds enough functionality to consider it a high level language. Basic input and output operations are part of the language. The enhanced features in structures (called classes in C++) also add a lot of functionality. C++ normally includes the standard template libary, which is implemented as an include as opposed to a linked library. It allows the creation of certain types of data structures like vectors, and includes operations like sorting.

There is a step up from high level language, code generation based on user interfaces created with a tool that includes drag and drop features. The first common one I can remember was Prototyper for the Macintosh. Visual Studio also has the same feature. You "draw" the user interface by dragging components like a menu or dialog box into the intended user interface window for an application, and the tool will generate the code (operations and data) to support the interface. The user then inserts code to the generated code to complete the operations. Special comments are used in the generated code to allow the tool to distinguish generated code from programmer added code, to make updating easier. There are also some similar code and file generators for database type applications.
 
Last edited:
  • #42
Not to be mean, but most of your questions can be answered with a simple search of Google or Wikipedia. Wikipedia has a very in depth, yet approachable page on assembly.
 

Similar threads

Replies
9
Views
3K
Replies
14
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
6K
  • · Replies 13 ·
Replies
13
Views
5K
Replies
17
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
2K