Algorithm: largest of the four

  • #26
33,724
5,419
Hi

Please remember that I have no knowledge of computer programming and not a student of science or math.

1: I was wondering how in the first place a computer would determine which number is the largest because it has no intelligence of its own. So, there should be some second algorithm or some procedure which helps computer to know the number '9' is smaller than '10'.
A compiler translates human readable code in some high-level language to machine code that can run on a specific processor (CPU). Processors generally have a comparison instruction that enables them to determine whether one number is larger than another, smaller than another, or equal to another. The comparison of two values a and b is usually done by subtraction; if the result of a - b is positive, a is larger than b; if a - b is negative, a is less than b; if a - b is zero, a is equal to b.
2: Where do I get this C++ compiler? Doesn't C++ let you create a software with GUI where you can enter your four numbers and clicking some button tells you the largest one? How do I learn some VERY basic "scaffolding" and 'extra details' of C++ to do simple software such as the one to find the largest number?
There are lots of C++ compilers out there. Some are free and some you have to pay for. When you choose one you will need to get one that runs on whatever operating system you're using - Windows, or some variant of Linux, or one of the Apple OSes.

You can use C++ or C# or Java or other high-level languages to create a GUI that displays a form with four text boxes and a button that the user can click to make the form display the largest of the four numbers. This sort of program is typically implemented using events - i.e., when the button is clicked, an event is raised, and a method/function in your program responds by finding the largest number and displaying it in your form.

For GUI-type programs, I would recommend C# or Java over C++, as they would be simpler to write, IMO.
 
  • #27
297
2
Borek said:
There are situations when what you really need is just to deal with a list of three or four numbers. Using general method in such a case is an overkill.
Like I said, nothing wrong in any way with the specific case. I just think more is learned by looking at the general case. Even the specific case is just the general case with an unrolled loop, in a manner of speaking. I figure it's good for the OP to at least be aware of it.

jtbell said:
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.

1: I was wondering how in the first place a computer would determine which number is the largest because it has no intelligence of its own. So, there should be some second algorithm or some procedure which helps computer to know the number '9' is smaller than '10'.
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.

So the logic is simply something like:
If (9 < 10) Then DO SOMETHING

Without getting into too many details, the 'If ... Then ...' is a very important statement, and the (9 < 10) stuff is called an expression (you'll want to learn about expressions for sure). This one evaluates to true or false (well, in this case always true, but you get my meaning I hope). If it's true, it does the 'Then' stuff. Otherwise, it goes on to whatever is next.

In a flowchart, it would be the diamond shape for a decision point.

2: Where do I get this C++ compiler? Doesn't C++ let you create a software with GUI where you can enter your four numbers and clicking some button tells you the largest one? How do I learn some VERY basic "scaffolding" and 'extra details' of C++ to do simple software such as the one to find the largest number?
There's a lot of possibilities there. I use g++ on a Linux system. Assuming you're on a Windows system, you could get Visual Studio. That's one possibility.

Another that I like, if you want to make GUIs, is QtCreator, which is an text editor and a form editor, that allows you to write GUI programs in the QT GUI Toolkit. But it depends on what GUI you wish to write for. And I wouldn't worry about GUIs until you get farther along in your development as a programmer. Simple text programs are the place to start.
 
  • #28
724
0
Codeblocks is a nice IDE(integrated development environment) for Windows and Linux. The cool part about using IDEs like Visual Studio or Codeblocks is the nearly instant feedback. You write a simple program, click the "run" button to run your program. This can expedite the learning process.

I would seriously caution a beginner considering starting in GUI. Learning to implement simple algorithms in a new language is a lot easier when you can concentrate on learning the language alone, rather than the GUI library you're using and the language. IMHO, you should stick to console programming, at least for a while.
 
  • #29
rcgldr
Homework Helper
8,697
528
Where do I get this C++ compiler?
If you're running windows, you can download and run Microsoft Visual C++ Express for free.
gui
At this point, I would recommend starting with dos console window programs first.
 
  • #30
jtbell
Mentor
15,584
3,561
How do I learn some VERY basic "scaffolding" and 'extra details' of C++ to do simple software such as the one to find the largest number?
There are many introductory textbooks for C++ programming, aimed at people who have not programmed before. I haven't taught a C++ course in several years, so I'm not familiar with what is currently available. It would be best to ask about these in the "Science Books" forum here (sub-forum of "Academic Guidance".

These books generally teach only console-mode programs that you run from a Unix or DOS command line, like my example. As others have noted, programming a GUI with windows and data-entry boxes is much more complicated and not for raw beginners to attempt.

Also keep in mind that C++ is not the only possible choice for a language to learn. In the USA, most introductory programming courses nowadays use Java, and there are now probably more introductory Java books than C++ books. (I don't know Java myself, so I can't help along those lines.)
 
  • #31
724
0
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.
 
  • #32
332
1
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
jtbell
Mentor
15,584
3,561
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. :uhh: 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
724
0
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
332
1
What are you guys saying? Please let me know.[/quote]

This is what happens when programmers start talking in programmer-speak around non-programmers. :uhh: 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
rcgldr
Homework Helper
8,697
528
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
332
1
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
33,724
5,419
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
332
1
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
33,724
5,419
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
rcgldr
Homework Helper
8,697
528
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
724
0
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.
 

Related Threads on Algorithm: largest of the four

Replies
9
Views
1K
Replies
3
Views
1K
  • Last Post
Replies
13
Views
2K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
1
Views
531
  • Last Post
Replies
11
Views
9K
  • Last Post
Replies
2
Views
14K
Replies
7
Views
3K
Top