especially for those who study computer programming to facilitate them in making a good program.
In creating a program, there are some important things, at least in my opinion, namely:
1. Goal programming,
2. algorithm,
3. Programming language itself
In this opportunity, I would like to discuss about the programming algorithm. Things - things that will be presented here is an example - the example programs written in C + + which will then be described in more detail. More precisely, if I say that this paper is a collection of examples of algorithms that have been discussed and quite "popular" as an example - an example algorithm.
The discussion contained herein is designed more to people - people who already know a little C + + programming language that will facilitate the understanding of the concept of the algorithm described here.
Regarding Algorithm at a Glance
Algorithms such as the word most people, is not something that is only associated with the computer world only. The algorithm is also applicable in everyday life. This time I want to discuss an example which might not sound "normal" is used to give an idea of the algorithm: D.
Some simple examples of algorithms that can be encountered in daily life - today for example:
1. Cook instant noodles.The process is as follows: heating water, open the wrapper of instant noodles (of course), put the noodles into the water, put spices on a plate, lift the noodles if it is cooked, mix with seasonings that are already on the plate with noodles, eat ...: D
2. CallThe process is as follows: pick up the phone, press the phone number:• if appointed will start talking after hang up• if not removed, then hang up
And many more examples - a simple example of an algorithm that can be encountered in daily life - today.
Programming AlgorithmNow we start talking about the programming algorithm. First - first, I will quote first feature - a hallmark of the algorithm as described by Donald E. Knuth:• The algorithm has a beginning and an end.• Each step must be defined properly so it does not have a double meaning (ambiguous).• Having input (input) or initial conditions.• Has the output (output) or the final condition.• The algorithm should be effective; when used properly - really solve the problem.
I myself was not really agree with it - things like that that seem too restrictive. Because there are actually many ways to produce something without having to follow the rules of the "raw" / one rule are impressed binding, right? But if one is willing to "act" as has been described above, it is legitimate - legitimate only.Now, we will see several programs, ranging from easy to fairly difficult, and an explanation.
Biggest Value Of 3 Fruit NumbersTo find the greatest value of 3 numbers, in C + +, the code that I use is as follows:
# Include <iostream>using namespace std;
void main () {int a, b, c, d;court <<"value 1:";cin>> a;court <<"value 2";cin>> b;court <<"value of 3:";cin>> d;
c = (a> b? a: b);
court <<"The biggest value is:" <<(c> d? c: d) <<"\ n";}
Logic:Compare the first with the second value. Then the larger of these values in comparison with the next value (the value of the three), so that in can be the greatest value among the three variables.
As we saw above, the first - of all, we create three variables, the variables a, b, c, and d. Then, we ask the user to enter values for the variables a, b, and d. After that, we compare the value of each - each variable. Variable c is used here as a "tool". The variable c itself store the largest value between the variables a and b. Then shown that the greatest value obtained after comparing the variables c and d.
Number of Runs
What is meant by the number of runs here are for example only the requested rate is 3 series. Then calculated as:
1 - 1 / 3 + 1 / 5 - 1 / 7
Note that in the process of calculation is the + and - changes every time a process of calculation. Okay, now we discuss the code:
# Include <iostream>
using namespace std;
void main () {
mp int = -1;
double one = 1, reset = 3;
int in;
court <<"input value:";
cin>> entry;
for (int ulang2 = 1; reset <= entry; re + +) {
a = a + ((1 / reset) * mp);
mp *= -1;
re + = 2;
}
court <<a <<"\ n";
}
Logic:
Doing the calculation of the first tribe to tribe to - n (number of terms requested by the user). Perhaps the most "disturbing" your thinking is how to make a + sign and minus change - change every time, right? Though it could have been resolved simply by multiplying -1 to -1 so that the result could be positive, right? If it were so, "business" right next to J.
Explanation of code:
The code above is also quite obvious if observed properly. Initially we made some "fruit" variable:
· The will be the input to / from the user, that entered
· The will "accommodate" the results of calculations, ie one
· The will be a divisor and continue to be raised in value, which is re-
· The will "change" signs and kind of computation (from addition to subtraction and vice versa), the mp
Then we perform one iteration variable by summing the value 1 (at first) with the results of a variable divided by the initial re-where the value is 3, and then plus 2 for each iteration is performed and then multiplied by the variable mp value -1 in the beginning so that yield calculation: 1 - (1 / 3). Variable mp then multiplied by -1 to produce a positive value so that the subsequent calculations to be as follows:
1 - (1 / 3) + (1 / 5)
And so on until the calculation reaches the tribe to - n, and the calculation process was performed once again and stopped. The result is then indicated by a single variable. Obviously, no?
Array
Ok, now, go to "game" array. In the "game", we will require the user to enter a number stambuk yangterdiri over 7 figures. Each - each digit (from stambuk numbers) are stored in an array that contained the variable a. The value of each - each array will be multiplied if the value is not 0 (zero) and "stored in a variable name it x. Then another call is made variable b which has 7 arrays as well. "Fill" the variable b is the result of each - each array variable a plus x. Here's the code:
# Include <iostream>
using namespace std;
void main () {
int a [7], x = 1;
for (int i = 0; i <7; + + i) {
court <<"number" <<i + 1 <<":";
cin>> a [i];
}
for (int j = 0; j <7; + + j) {
if (a [j] == 0)
continue;
else {
x *= a [j];
}
}
int b [7];
for (int k = 0; k <7; + + k) {
b [k] = a [k] + x;
court <<b [k] <<"\ n";
}
}
Logic:
Of all the array (number - number) that is (already included), we do a "search". If the array has value 0 (zero) then, the array is passed. If the array is not equal to zero (because it's certainly worth more than zero, because logically, there is no stambuk a minus number, right?), Then we multiply by the next array that is also not zero. Then after that, we create another array of a variable to another (of course) for "occupied" respectively - each by the results of each - each array variable that only added to the value before the multiplication.
Explanation of code:
First - first, we created a variable (a variable) to accommodate an array of 7 pieces, then the variable x to accommodate the whole multiplication array variable a. Initial value of x is 1, assuming that there is no wholly stambuk number 0 (zero),
with a whole array of variables having the smallest possible value is 1. Do loop for each array variable a, by "inserting" the condition that if the array value is 0 (zero) then pass (continue), and if the value is not zero, then multiplied by the variable x. Once completed, created a new variable with the same array, ie 7. Then looping done more to fill all of the array with the results of x plus the individual - each array variable a.
Triangle Elbow - ElbowPicture a triangle to be made are as below:
****************************
As we see, that there is a triangle elbow - elbow made by making the symbol "*" ranging from one to the line to - n. For example, the above example, the user wants to display a triangle with the number of lines 7, then the image is displayed as above. And so on. Here is a sample code:
# Include <iostream>using namespace std;
void main () {int segi1;court <<"";cin>> segi1;
for (int j = 1; j <= segi1; + + j) {for (int i = 1; i <= segi1; + + i) {if (i <= segi1 - j)court <<"";
elsecourt <<"*";}court <<"\ n";}}Logic:When observed either - well, it is known that, for example, if there are 7 rows requested, then there are 6 pieces of space are made and then printed with an asterisk "*", and on the next line there are 5 pieces of space and two asterisks are printed. Then later on the next line there are 4 pieces spaces, on the next line there are 3 pieces of space again, and so on. It can be concluded that the number of spaces are created on the first line is:Total lines desired - 1
Then later on the second line is:Total desired row - 2
And so on.
Thus, note that for the printing of simple space using the formula:Total desired row - the row to be printed
So, for example, the row being "done" is the first line then, the formula becomes: total desired row - 1, as well as the second line be: total desired row - 2, etc.. If you've "passed" the situation, print "*".
Explanation of code:In the code above, first - first we create a variable / identifier with the name segi1. Where segi1, is input from the user to specify how many lines of the triangle to be printed. After that, do loop to print the number of lines, here we use the variable j. To print a space and a "*" alone is used variables i, with conditions:• If i is less than or equal to segi1 - j, printed spacesThis is because the terms that have been mentioned previously, the number of spaces are printed per line is the result of segi1 - j, where j is the line that the actual (current) at the time.• If i is greater than segi1, then prints a "*".Actually, if you can observe closely enough, then you will find that we always score the same amount. For example, the desired number of rows is 7, then in fact in every line of the first row to row 7, we always print as much as 7 times on each line. It's just that there are "variations" on each line, ie if the reduced segi1 with j, then the premises itself the number of "*" is printed will be more numerous and in the end equal to the desired number of rows. So, try to imagine that what we print is a square / rectangle with the picture as it has been
mentioned above (do not mind ).
This continues on each line and at ahirnya stop the condition in which j is greater than 1. The result you can see on the screen shot below:
Double rowThis time you are required to create / display how many rows were requested by users but by doubling each line is printed on the screen. For example, the requested number of rows is 5, then it is printed:112222333 333444444445555555555
So the code that I make is as follows:
# Include <iostream>using namespace std;
void main () {int in;court <<"number of rows:";cin>> in;
for (int i = 1; i <= in; + + i) {for (int j = 1; j <= i * 2; + + j) {court <<i;}court <<"\ n";}}
Logic:The logic used here is quite similar to the way we print the triangle to the screen. Maybe you could say there are still "family relationship" with the earlier . Here we do looping to print the lines requested by the user. In looping, we show a line is printed at the time. For example if the line is being printed at the time was the second line, then that is shown is figure 2. And so on. Note that the numbers - the numbers have a pattern printed. On each row being printed pattern of the numbers are: row being printed multiplied by 2. For example in the first row, the number of digits / number of digits printed is 1 X 2 = 2 times in print. In the second row, the number of lines in print are 2 X 2, and so on. This continued until the lines desired by the user are printed.
Explanation of code:In the code above, is made variable to hold how many rows are desired by the user (in), just like usual . Based on input from the user, we create a loop to print the lines - lines of the desired user. In this iteration, we "insert" a looping again to print the figure to the screen (using the variable j). Looping is done with the condition j less than or equal to i multiplied by 2. Where, i is the row that is being done and how much iteration j is undertaken in condition j is less than or equal to i.Here is a screen shot of it:Calling Numbers Reversed
We begin entering one of my favorite parts of "convert" numbers into sentences / words - words . We entered first the easy part, calling the numbers upside down. This time, we require the user to reduce any number (which is less than 10,000) with 10,000. For example, users enter the numbers 3456, then 10000-3456 certainly result = 6544, but we convert it to make it as a sentence but spelled backwards. Thus, the appearance is: four four five six .
The code is:
# include <iostream>
using namespace std;
sntce void (int n) {
switch (n) {
case 1: court <<"one"; break;
case 2: court <<"two"; break;
case 3: court <<"three"; break;
case 4: court <<"four"; break;
case 5: court <<"five"; break;
case 6: court <<"six"; break;
case 7: court <<"seven"; break;
case 8: court <<"eight"; break;
case 9: court <<"nine"; break;
case 0: court <<"zero"; break;
default: break;
}
}
void reverse (int n) {
int zero, mod;
zero = n / 10;
mod = n% 10;
if (zero == 0 & & mod == 0)
exit;
else {
sntce (mod);
reverse (zero);
}
}
void main () {
int in, reduce;
const int stay = 10000;
do {
court <<"";
cin>> in;
} While (in> 10000 | | in <1);
Reduced = stay - in;
reverse (reduce);
court <<"\ n";
}
logic:
Of input which is input by the user, reduced to 10,000 and then in turn starts from the most recent figures up to the first number is converted into the form of numbers. The way to "take" the last digit is by taking modulo / remainder of the results of these reductions divided by 10. For example, 6544 results are of course the rest is divided by 10 is 4. Then the round of 10 which is 6544 divided by 654 will be in for another with 10 and will result in item 4. And round the result of 654 divided by 10 which is 65 divided by 10 and return the remainder is 5. Then the latter, because the value of 6 is stored and then divided by 10, and the rest must be 6, then 6 is taken.
Explanation of code:In the main part of the program, we create some variables: reduce, in, and stay. Reduced variable itself is useful to accommodate the reduction of the stay, which we have given a constant value of the initial 10,000. But, we want to prevent the user does not enter a number that is "not unusual" for example the number 0 or 10 001 and so on. So, we use the do ... while (this is actually a "favorite" because I want to always prevent the input that is not normal) with the "rules" that the display ("") will always happen when a user enter a number greater than 10,000 or smaller than 1. We then insert Reduced to reverse, that's the end of the main program. Then, as we saw above, there are two functions that we created from the beginning: sntce and reverse. sntce serves to convert the numbers into a word with a function switch, which I think it's pretty obvious, that if the input (in this case is for the remainder of 10,000 minus the input from the user and then divided by 10) is 1, then the displayed word "one" , when 2, then the display is "two", etc.. While the reduction in reverse to calculate the results of 10,000 reduced by input from the user (in). Here we use the two conditions, namely if the rounded result of n (reduce) divided by 10 is equal to 0 and also for the remainder (mod) equals 0 (zero), and the conditions in Luat it (of course this means that there are still numbers that should be in for 10 or could have been there for the rest need to be put into sntce). If yag happens is the second condition, we "throw" variable to sntce mod (already know it function?) And Dial back the reverse by using zero till the end no more to be divided and no longer the rest of the division
Refer to FiguresOk, finally got here, too. The following program is a program to call numbers in the form of a sentence ranging from one to millions. For example number 123 to be "one hundred twenty three", etc.. Here's one example of the code:
# Include <iostream>using namespace std;
void first (int n) {switch (n) {case 1: court <<"one"; break;case 2: court <<"two"; break;case 3: court <<"three"; break;case 4: court <<"four"; break;case 5: court <<"five"; break;case 6: court <<"six"; break;case 7: court <<"seven"; break;case 8: court <<"eight"; break;case 9: court <<"nine"; break;case 10: court <<"ten"; break;case 11: court <<"eleven"; break;default: break;}}
void second (int n) {int bul, the rest;bul = n / 10;remainder = n% 10;
if (bul == 0)first (rest);
else if (bul == 1) {if (remaining <= 1)first (n);
else {first (rest);court <<"thirteen";}}
else {first (bul);court <<"twenty";first (rest);}}
void third (int n) {int bul, the rest;bul = n / 100;remainder = n% 100;
if (bul == 0)second (remaining);
else if (bul == 1) {if (remainder == 0)court <<"one hundred";
else {court <<"one hundred";second (remaining);}}
else {first (bul);court <<"hundred";second (remaining);}}
void fourth (int n) {int bul, the rest;bul = n / 1000;remainder = n% 1000;
if (bul == 0)third (residual);
else if (bul == 1) {if (remainder == 0)court <<"a thousand";
else {court <<"a thousand";third (residual);}}
else {third (bul);court <<"thousand";third (residual);}}
fifth void (int n) {int bul, the rest;bul = n / 1000000;remainder = n% 1000000;
if (bul == 0)fourth (residual);
else if (bul == 1) {if (remainder == 0)court <<"one million";
else {court <<"one million";fourth (residual);}}
else {third (bul);court <<"million";fourth (residual);}}
void main () {int n = 1;while (n == 1) {int num1;
do {court <<"";cin>> num1;} While (num1 <1);
fifth (num1);court <<"\ n";}}
Logic:We want to test whether the numbers entered are millions, thousands, hundreds, tens, or units. Because basically that's just kind of - kind of numbers there (not including the billions and trillions and the larger, due to the int type can not reach the numbers - these numbers).At the time of our first "test" these numbers, we examine:1. Whether it is the number of millions?First - first, there may be asking why this section is entered first? In logical, from the pronunciation, in pronouncing the name of numbers, of course that would diucapan is a sequential number and greatest to the smallest. For example, 1234 is read one thousand two hundred and thirty-four. Thus, from the large numbers that can be incorporated into the program, millions are "portions of" the greatest. So the very first test is "whether the numbers are the millions?".• if not, then the number that tested whether the number it was thousands.• if yes, then "the millions" numbers were translated into words - words and then the rest (hundreds of thousands to the bottom) is checked again at a function to number thousands.2. Are the numbers of thousands?• If not, then the number that will be tested whether the number is hundreds.• If yes, then "the thousands" of numbers that is translated and the remainder included in the hundreds.3. Are the numbers of hundreds?• If not, then the number that will be tested whether the number it is tens. • If yes, then "the hundreds" of numbers that is translated and the remainder put in dozens of functions.4. Is that number tens?• If not, then the numbers will be inserted into the last function, the function unit.• If yes, then "the dozens" of numbers that will be translated and then the rest is included in the function unit.
Explanation of code:First - first, we need to note that it was "prepared" a loop in there (while). It's actually "kerjaaan" I, which makes it so the program can be repeated continuously (Please edit yourself if you want to change it) . In the main function, as we saw, we wish that our input has a value at least equal to one. Then after that, we put the input in the millions. The trick is similar to how we translate the numbers - the numbers into words - words. The process can take a look at the conditions - the conditions on these functions.• If the result rounded to 1,000,000 of the distribution of n is 0 (zero), it stands to reason that n is not the number of millions.• If the result rounded to 1,000,000 of the distribution of n is 1 and the remainder is 0 (zero), then n must be 1,000,000 (one million).• If the result rounded to 1,000,000 of the distribution of n greater than 1, then these numbers must be greater than or equal to 2,000,000. Then the results of the division is rounded, inserted into the third (because the maximum value of number is hundreds of millions of millions), then it prints the word "million", and then the rest is put in a fourth (because there is the possibility of remaining worth thousands)At the function - other functions are also made in this way until finally on the part of the unit number. Only, if we look at the second function, there is a condition where if the result is a round of division, and if the remainder is greater than 1, then the rest will be put in first and later in the printed word "twelve". So for example, the figure is 19, the rest of course is 9. Number 9 in the first, will produce the word "nine" on the screen. Then coupled with the word "twelve", then the result is "nineteen". So, we do not have to make a case for "twelve - twelve" other than 11 (eleven) cover
Phew, finally finished this paper as well (actually a few more minutes before Van Helsing action aka the middle of the night: D). Tau guns, how much time I spend in front kompie to improve this paper. Moreover, because this is my first post, it certainly does not make mistakes - mistakes (actually writing to any human being there must be a mistake because nothing is perfect.
0 komentar: On Programming Algorithms Using C + +
Posting Komentar