In this post, I will share with you a C++ coding of game Tic Tac Toe.
You will be playing against computer by entering the coordinate of the box that you want to tick.
Click here for coding.
Enjoy it!
Monday, June 27, 2011
Tuesday, June 7, 2011
Post 6
Lets discuss about the last post where you experimenting with the for statement.
What happen when you initiate the variable "i" with 2?
The coding inside the loop will only be executed once. Since the variable "i" initial value is 2. Thats mean, in the beginning, the variable still fulfill the looping condition which is "i must less than 3" but then, after the first run of the coding, the variable "i" increased by 1 and thats mean the value now is 3 (duh). So, the coding will no longer be executed since it does not fulfill the looping condition. But if you modify the condition a bit, you can make it executed 2 times which is by changing to "i<=3" which mean "i less or equal to 3".
What happen when you change the looping condition with "i>4"?
Well, this is depends on the variable "i" initial value. If you set it to 0, then the coding will not be executed at all. "i" should be more than 4. Therefore, the variable does not satisfy the looping condition on it first run. So, no output is produced. BUT, if you set the initial value of "i" with a value that is bigger than 4, the coding will be executed in such way that it is a wise decision to already click the 'close' button without watching any longer. Let say the initial value of "i" is 5. After the first run it will increase to 6, and then 7, then 8,9,10,11 and so on (you already know the counting). No matter how many times the program run the repetition, the variable "i" is still more than 4. So, the coding will run a hundred, thousand, million times until your computer lose memory. In programming, this is what they called as INFINITE LOOP.
What happen when you change the increment with "i--" or "i+2"?
"i++" is equal to "i=i+1", therefore "i--" is equal to "i=i-1". If you applied this condition on the for statement without changing the looping condition and the variable "i" initial value, thats mean that it will also cause the program to experience the annoying infinite loop phenomenon(:p). "i" will be decrease like such: 0,-1,-2,-3,-4,-5........ So, no matter how much the coding run, variable "i" still satisfy the condition. Now, "i+2" means that "i" will be increased by 2 each time it runs the coding inside the for statement. So, how many times does the word "repeating" will be displayed?. Yup, only once.
And what happen when you declare the looping variable with data type "double" or "float"?
The repetition runs as usual. You actually can declare the looping variable with any data type as long as long as the variable is functioning mathematically with mathematical operation(+,-,*..etc). So, a number of type string or char will not do. Well, there is a trick to make it loop properly even if the data type is a char but theres no need to learn about that. And theres even a rumors said that there are also some compiler that will produce error message if the data type of the looping variable is other than int, thats mean, double and float are also not allowed. Now, just forget about this one, and continue using int for the looping variable. Much shorter to type(:p).
You can also declare the loop variable outside the for statement. Example:
int i;
for(i=0;i<10;i++)
{
cout<<"i declared the loop variable outside the for statement, so what? ";
}
This is an example of repetition and selection:
for (int i=2;i<24;i+4)
{
if (i<10)
{
cout<<"Monkey"<<endl;
}
else
{
cout<<"Monyet"<<endl;
}
}
Trace it.
What happen when you initiate the variable "i" with 2?
The coding inside the loop will only be executed once. Since the variable "i" initial value is 2. Thats mean, in the beginning, the variable still fulfill the looping condition which is "i must less than 3" but then, after the first run of the coding, the variable "i" increased by 1 and thats mean the value now is 3 (duh). So, the coding will no longer be executed since it does not fulfill the looping condition. But if you modify the condition a bit, you can make it executed 2 times which is by changing to "i<=3" which mean "i less or equal to 3".
What happen when you change the looping condition with "i>4"?
Well, this is depends on the variable "i" initial value. If you set it to 0, then the coding will not be executed at all. "i" should be more than 4. Therefore, the variable does not satisfy the looping condition on it first run. So, no output is produced. BUT, if you set the initial value of "i" with a value that is bigger than 4, the coding will be executed in such way that it is a wise decision to already click the 'close' button without watching any longer. Let say the initial value of "i" is 5. After the first run it will increase to 6, and then 7, then 8,9,10,11 and so on (you already know the counting). No matter how many times the program run the repetition, the variable "i" is still more than 4. So, the coding will run a hundred, thousand, million times until your computer lose memory. In programming, this is what they called as INFINITE LOOP.
What happen when you change the increment with "i--" or "i+2"?
"i++" is equal to "i=i+1", therefore "i--" is equal to "i=i-1". If you applied this condition on the for statement without changing the looping condition and the variable "i" initial value, thats mean that it will also cause the program to experience the annoying infinite loop phenomenon(:p). "i" will be decrease like such: 0,-1,-2,-3,-4,-5........ So, no matter how much the coding run, variable "i" still satisfy the condition. Now, "i+2" means that "i" will be increased by 2 each time it runs the coding inside the for statement. So, how many times does the word "repeating" will be displayed?. Yup, only once.
And what happen when you declare the looping variable with data type "double" or "float"?
The repetition runs as usual. You actually can declare the looping variable with any data type as long as long as the variable is functioning mathematically with mathematical operation(+,-,*..etc). So, a number of type string or char will not do. Well, there is a trick to make it loop properly even if the data type is a char but theres no need to learn about that. And theres even a rumors said that there are also some compiler that will produce error message if the data type of the looping variable is other than int, thats mean, double and float are also not allowed. Now, just forget about this one, and continue using int for the looping variable. Much shorter to type(:p).
You can also declare the loop variable outside the for statement. Example:
int i;
for(i=0;i<10;i++)
{
cout<<"i declared the loop variable outside the for statement, so what? ";
}
This is an example of repetition and selection:
for (int i=2;i<24;i+4)
{
if (i<10)
{
cout<<"Monkey"<<endl;
}
else
{
cout<<"Monyet"<<endl;
}
}
Trace it.
Friday, June 3, 2011
Post 5
We have finished learning about the basic programming structure which is sequential and selection(if, else, switch). Now, we are going to learn the next one which is repetition or looping. It is the most important element in programming which allow you to repeat the coding as many time as you(programmer) wish or also as many time as the users wish. The repetition save less time to code a program since it can help repeat the same command over and over again without having to write the same command repeatedly. You just need to tell the program to repeat it. Three type of syntax in C++ that can perform the looping is for, while and do...while. Lets learn the first one which is the for statement.
Now, always remember that the for statement need to be coded in the following manner:
for(int variable=0 ; variable < xx ; variable++)
{
//write your coding here
}
As you already know, you can replace variable, the looping variable with any word or character that you want and you must initiate it with a value (in the example and for this basic learning, lets initiate it with 0). Any looping variable should be declared with the data type int. xx should be replaced with the number of repetition that you want the coding inside the for statement to loop and I will explain later what is the purpose of variable++
Confused?
Alright lets imagine you code it like this:
for(int i=0;i<3;i++)
{
cout<<"repeating ";
}
Now, thats mean the variable "i" initial value is 0 and it will repeat the coding inside the for statement 3 times. "i<3" meaning that the coding inside the for statement(from the opening of the curly bracket until the closing of the curly bracket) will be repeated while the "i" value is still less than 3. How does the variable "i" becomes 3? Well, the "i++" command is the one that will do the trick(remember variable++?). It will increase the value of "i" by 1 each time the coding is being executed and when the variable "i" reach 3, the looping will stop itself since the condition to loop is "i" must less than 3.
P/S: "i++" is also equal to "i=i+1"
The output for the coding above is produced as below:
repeating repeating repeating
The command cout<<"repeating"; is executed 3 times.
Try compile this full coding using Microsoft Visual C++ 6.0
#include <iostream.h >
void main()
{
for(int i=0;i<3;i++)
{
cout<<"repeating ";
}
}
THINK ABOUT IT
What happen when you want to execute the code 100 times?
Do you prefer using for(int i=0;i<100;i++)?? OR you are much prefer to waste your time and your computer memory by typing the line cout<<"repeating " 100 times.
Try experiment with the syntax of the for statement to see different result. Be comfortable and swift with it.
Things you should try
-What happen when you initiate the variable "i" with 2?
-What happen when you change the looping condition with "i>4"?
-What happen when you change the increment with "i--" or "i+2"?
-And what happen when you declare the looping variable with data type "double" or "float" etc. ?
Now, always remember that the for statement need to be coded in the following manner:
for(int variable=0 ; variable < xx ; variable++)
{
//write your coding here
}
As you already know, you can replace variable, the looping variable with any word or character that you want and you must initiate it with a value (in the example and for this basic learning, lets initiate it with 0). Any looping variable should be declared with the data type int. xx should be replaced with the number of repetition that you want the coding inside the for statement to loop and I will explain later what is the purpose of variable++
Confused?
Alright lets imagine you code it like this:
for(int i=0;i<3;i++)
{
cout<<"repeating ";
}
Now, thats mean the variable "i" initial value is 0 and it will repeat the coding inside the for statement 3 times. "i<3" meaning that the coding inside the for statement(from the opening of the curly bracket until the closing of the curly bracket) will be repeated while the "i" value is still less than 3. How does the variable "i" becomes 3? Well, the "i++" command is the one that will do the trick(remember variable++?). It will increase the value of "i" by 1 each time the coding is being executed and when the variable "i" reach 3, the looping will stop itself since the condition to loop is "i" must less than 3.
P/S: "i++" is also equal to "i=i+1"
The output for the coding above is produced as below:
repeating repeating repeating
The command cout<<"repeating"; is executed 3 times.
Try compile this full coding using Microsoft Visual C++ 6.0
#include <iostream.h >
void main()
{
for(int i=0;i<3;i++)
{
cout<<"repeating ";
}
}
THINK ABOUT IT
What happen when you want to execute the code 100 times?
Do you prefer using for(int i=0;i<100;i++)?? OR you are much prefer to waste your time and your computer memory by typing the line cout<<"repeating " 100 times.
Try experiment with the syntax of the for statement to see different result. Be comfortable and swift with it.
Things you should try
-What happen when you initiate the variable "i" with 2?
-What happen when you change the looping condition with "i>4"?
-What happen when you change the increment with "i--" or "i+2"?
-And what happen when you declare the looping variable with data type "double" or "float" etc. ?
Monday, March 7, 2011
Post 4
Done with if...else statement ? Lets switch to the new statement of selection that are switch statement. Switch statement is very useful to replace nested if statement. For example, if we want to make a program to determine the performance of the student based on their current class.
Click here for coding.
This program will display whether the students is a genius, good, average or poor students based on their current class. The default statement is like else statement. It will be executed if the user didn't enter A, B, C or D class.
Click here for coding.
This program will display whether the students is a genius, good, average or poor students based on their current class. The default statement is like else statement. It will be executed if the user didn't enter A, B, C or D class.
Post 3
This is an extended post for selection. The first example is the one we always use in our daily lives, that is to calculate the BMI of a person.
Click here for coding.
It will display your current BMI based on your weight and height that you have entered and it also tell you whether you're underweight, normal weight or overweight.
The next example will show you how to use Boolean expressions along with selection statement.
This program is to determine whether the year that user input is a leap year or not.
Click here for coding.
Click here for coding.
It will display your current BMI based on your weight and height that you have entered and it also tell you whether you're underweight, normal weight or overweight.
The next example will show you how to use Boolean expressions along with selection statement.
This program is to determine whether the year that user input is a leap year or not.
Click here for coding.
Friday, May 14, 2010
Post 2
This post will show you how to using selection in C++.
Clik here for coding.The first example is to decide grade of student subject based on their marks.
We will declare grade as char and marks as int.
Then, we will prompts the users to enter their marks.
Then, the program will decide the grade of the student based on the marks that student
enter.
The next program is to decide how much bonus the salesman will get based on their sales.
The program will first prompts the salesman to enter their sales.
We declare sales and bonus as int.
Click here for coding.
The program then will print the bonus that the salesman will get based on their sales.
This next program will asks the user to prompt their bill and the program will decide how much discounts the user will get.
Click here for coding.
The program will then calculate and print the payment that the user need to pay based on
the bill that the user prompt and the discount they get.
Tuesday, April 6, 2010
Post 1
This program is the introduction to the world of programming.
It's a simple coding that you can do by yourself even you're new in programming.
Let's take a look at it.
Click here for coding.
This program will print the information that are contain in the double notation.
Next, I will show to you how to input the data in the program and then print it.
It will ask you to prompts the marks and then print it.
Next we will see how to use a string type variable.
You see that server is the string type variable, so when the program ask you to prompts
the "Server : ", you can enter the alphabet. For example like Abc and Doremi.
Subscribe to:
Comments (Atom)