Monday, December 13, 2010

csci135c3.cpp

This is the second kind of loops, the while loops. In this tutorial, I am also going to demonstrate a variation of the while loop, the do-while loop.
/*
 *  csci135c3.cpp
 *  This program shows the useage of the while loop.
 *
 */

#include <iostream>
using namespace std;

int main()
{
   int counter = 5; // our integer counter variable
   cout << "While loop #1, while counter > 0" << endl;
   while (counter > 0)
   {
      cout << "Counter is now: " << counter << endl;
      counter--;
   }
   
   counter = 10; // reset counter back to 10
   cout << endl; // just for the display, this skip one line 
   
   cout << "Do While loop, while counter > 0" << endl;
   do
   {
      cout << "It still run one time although it does not satisific the "
      << "condition counter > 10 " << endl;
      counter--;
   } while (counter > 10);
   
   return 0;
}
The format of the loop is like this:
while (condition)
{
   // do something here
   // some kind of counter adjustment
}
The concept of the while loop is just like how it sounds, while condition is true, do the execution in the block. But unlike the for loop, the counter adjusting is do in the loop. See the example, line 17 is counter--. Of course, you can increment as well. At this point, you might be asking: “What happen if we do not know in advance how many times we need to run?” This is not true, because we can use variable in the condition. You can do something like this.
while (counter > whereToStop)
{
   // do something here
}
However, something important need to be clear. Be careful that your loop, whether it is a for loop or while loop, it is not an infinite loop. An infinite loop is a loop that will not terminate, something it is because the condition is always possible to satisfy, or more frequently, remember to include the counter adjustment. Below are some examples that show what possible mistake one can make.
int counter = 0;
while (counter < 10)
{
   // No decrement or increment, it won't be ever greater than or equal
   // to 10. So it will be an infinite loop.
}

int counter = 10;
while (counter > 10)
{
   // the counter is going the opposite direction, and it will
   // never be greater than 10. This is an infinite loop.
   counter--;
}
Lastly, there are times when you do want the loop be infinite. I remember it the canvas GUI program, the screen requires to be refreshed until the user click the close button. You do not need to use the above examples, you can simply do the follow.
while (true)
{
   // do something here
}
This is a very well know way to create an infinite loop, all the programmers understand this is a loop that wanted to be run forever. There are some usage of this, just take know of such a thing.

Finally, let’s look at the do while loop at the last part. This loop is interesting because, even though the condition of the loop does not satisfy, it will still run for the first time. Let me give you a real world example, let say everyone can get a free cookies from the school’s café, and you can get more if you have a ticket, each ticket you have allow you to get one more cookie. So everyone get to the school café, the loop in this case, will get to run once.
int ticket = 5; // the amount of cookie tickets they have
do 
{
   cout << "Give you a piece of cookie. " << endl;
   ticket--;
} while (ticket >= 0);
Note, this case the student will get 6 pieces of cookies. The first one that everybody gets just by showing up, and then 5 more cookies (from the exchange of tickets). There are many situations too, this is just one of the example I come up with. And plus who doesn’t like cookie? :9
Note: This example has some problems, edit or remove before publish. 

Practice Problem

Write a program that will count from 0 to 10. The output should be like the following.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Sunday, December 12, 2010

csci135c2.cpp

A program is good for doing things thousands of times and accurately. What if we want repeat doing something many times? How should we program this? It is okay for us to type it out line by line if the number is small, but what if we want to write from 1 to 1000? We need some kind of looping function to do this!
/*
 *  csci135c2a.cpp
 *  This program count from 1-20, by hand, typing it out one by one. It might
 *  taking a little bit longer time than using a loop. But let's try it out.
 *
 */

#include <iostream>
using namespace std;

int main()
{
    cout << "1 " << "2 " << "3 " << "4 " << "5 " << "6 " << "7 " << "8 " << "9 " 
         << "10 " << "11 " << "12 " << "13 " << "14 " << "15 " << "16 " << "17 " 
         << "18 " << "19 " << "20 " << endl;
    cout << "I am tired, we need to write a loop to do this." << endl;
    return 0;
}
Not only this takes a lot of time to write, but the size of this program is also much larger than what it will take if it is written in a loop. Now let's look at how to do it with a for loop, it require much less typing, and the chances of making a typo error is also much smaller. Remember what does int mean? (answer, int means integer, 0,1,2,3)
/*
 *  csci135c2a.cpp
 *  This is the for loop version of the previous program, this is much easier
 *  to write a loop that count a few thousands time. Rather than writing it 
 *  line by line by hand. Just need to change the int i = x part.
 *
 */

#include <iostream>
using namespace std;

int main()
{
   // this the for loop, that count from 0 = 20, i+1 is the offset, because
   // this count from 0-19. But we want to print from 1-20, so offset is 1
   for (int i = 0; i <= 20; i++){
      cout << i+1;
   }
   cout << endl; // this is the space to skip to the next line
   cout << "I am tired, we need to write a loop to do this." << endl;
   return 0;
}
First we take a look at the structure of a "for" loop:
for (set up counter; condition of the counter; counter adjustment){
   // do something here
}
In pain English, this can be view as the following: "for an integer start at 0, i is less or equal to 20, i plus plus. Then in the loop we can print the value of i (offset is 1 in this case)." Usually (for general usage), the counter will be an int, and it can be named anything you like. But a single letter i, j, k, will more popular, because it is also used as index for array and vector. But nothing is wrong with a name called "counter" too, the condition of the counter, there are many examples. i < 10, means it goes up to 9. i <= 10, means it goes to 10. As for the adjustment, you can let the counter count up to 10, or count down to 10. Both is correct usage depend on what you do, there is no right or wrong answer here.
// Increment from 0 to 9 (total of 10 times)
for (int i = 0;  i <= 10; i++)
{
   // do something here
}

// Decrement from 10 - 0 (total of 10 times)
for (int i = 10; i <= 0; i--)
{
   // do something here
}
This two function will both execute the statement within 10 times. But to be sure, if you want to print out from 0-10, you need to do some adjustment to the decrement version. Because the counter i is going from 10 to 1, and if you want to print from 1 to 10, then you can do the following.
// Print 0 to 10 in the decrementing fashion of for loop
for (int i = 10; i <= 1; i--)
{
   int number = 0; // declare a integer variable for displaying our value
   cout << number << endl;
   number++; // increment the printing number
}
Practice Problem:
Write a program that display the first 10 odd number using the for loop, they should be in the same format as below. Separated by a comma. Tips: You can use the offset to display the odd number, try different offset and see what happen.
1, 3, 5, 7, 9, 11, 13, 15, 17, 19

Thursday, December 2, 2010

Practice Solution for C1

Write a program that print Good morning, Good afternoon, Good night in 3 separate lines. Tips, to go to next line, use endl. The output of the program should look like this.
Good morning
Good afternoon
Good night
The solution of the practice problem is below. If you type the keywords correctly, you should get the program to compile and finish in about 10 to 15 mins. The idea is to test if you know what does endl do. Test it out, try to see what happen if you don't add endl like we did in the solution. You will get everything on the same line.
Good morningGood afternoonGoodnight
So what you want to do is to add a space at the end of the each word, like below.
cout << "Good morning " << "Good afternoon " << " Good night " << endl; 
// output will be like this: Good morning Good afternoon Good night
Finally, here is the solution to the question above. If you have any question, please feel free to email me or post question in the comment section below. Happy coding! :)
/*
 *  csci135p1.cpp
 *  Solution to the practice, just make sure to type endl for the next line
 *
 */

#include <iostream>
using namespace std;

int main()
{
   cout << "Good morning" << endl;
   cout << "Good afternoon" << endl;
   cout << "Good night" << endl;
   return 0;
}

Wednesday, December 1, 2010

csci135c1.cpp

Since this is the first tutorial, and I am still trying to figure out my way of how to do this. Let's do a easy example, the well know "Hello World" example. This program will display a message to the terminal.
/*
 *  csci135c1.cpp
 *  The classical program that display a message to the screen.
 *  The basic structure of the c++ program, and the main function.
 *
 */

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! " << endl;
    cout << "I like programming, programming is fun. " << endl;
    return 0;
}
The first section of the code is the comment, there are a few ways to include comments in our source code. The first way is only for a single line, it seems to be the easiest way. It is useful for variable descriptions or function's comment. The second one is good for comments that is a few lines. But for the second way to display comment, please remember to end the comment with */. Or else it will give you a compiler error. Of course, you can go fancy with that method. But some of the professors might not like it. They might prefer the first way, just add // to every single line.
// This is a comment

/* This comment can
   be written over
   a few lines, just remember
   to close it! */

/* 
 * Look at this
 * beautiful
 * comment style.
 *
 */

// One line of comment
// Another line of comment
// You can type as many single lined comment as you want
The first thing you see after the comment section will be the include statement, we will discuss it more in the later tutorial. For now just remember to type it when you want to do any kind of displaying messages, the include is to let the linker know I am including part of the codes from different header files. 
#include <iostream>
using namespace std;
The main function's name... is called main. And int represents integer, therefore main function will return an integer. The integer is used to determine the state of the function, whether it is not working, it crash, or anything important the program is going to telling the operating system or user. Usually 0 means it is normal, and the rest other is considered to be not "normal". But what it returns is purely based on the what the programmer wants it to be. The meat of this program is the following line.
cout << "Hello World! " << endl;
cout means the stream that the data is sending to, << is called the insertion operator, it will send the data into the stream. And endl; is a symbol to let the program knows it is going to go to the next line. It is possible to link the input data like the following to create something like this. Notice the last example, show you how to control how many line to skip.
cout << "Regular style, outputting message. " << endl;
cout << "There " << "are " << "more " << "messages. " << endl;
cout << "Let's skip 5 lines? " << endl << endl << endl << endl << endl;
Thoughts: This first tutorial is a lot longer than what I thought it will be... I did not expect it to be so lengthy. most programmers can write this piece of code within 20 seconds, when trying to explain it without any prior knowledge is a different story. And I will have to double check the facts of my explanation.

Practice Problem 1:
Write a program that print Good morning, Good afternoon, Good night in 3 separate lines. Tips, to go to next line, use endl. The output of the program should look like this.
Good morning
Good afternoon
Good night
See here for solution

csci135c1.cpp

Since this is the first tutorial, and I am still trying to figure out my way of how to do this. Let's do a easy example, the well know "Hello World" example. This program will display a message to the terminal.
/*
 *  csci135c1.cpp
 *  The classical program that display a message to the screen.
 *  The basic structure of the c++ program, and the main function.
 *
 */

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! " << endl;
    cout << "I like programming, programming is fun. " << endl;
    return 0;
}
The first section of the code is the comment, there are a few ways to include comments in our source code. The first way is only for a single line, it seems to be the easiest way. It is useful for variable descriptions or function's comment. The second one is good for comments that is a few lines. But for the second way to display comment, please remember to end the comment with */. Or else it will give you a compiler error. Of course, you can go fancy with that method. But some of the professors might not like it. They might prefer the first way, just add // to every single line.
// This is a comment

/* This comment can
   be written over
   a few lines, just remember
   to close it! */

/* 
 * Look at this
 * beautiful
 * comment style.
 *
 */

// One line of comment
// Another line of comment
// You can type as many single lined comment as you want
The first thing you see after the comment section will be the include statement, we will discuss it more in the later tutorial. For now just remember to type it when you want to do any kind of displaying messages, the include is to let the linker know I am including part of the codes from different header files. 
#include <iostream>
using namespace std;
The main function's name... is called main. And int represents integer, therefore main function will return an integer. The integer is used to determine the state of the function, whether it is not working, it crash, or anything important the program is going to telling the operating system or user. Usually 0 means it is normal, and the rest other is considered to be not "normal". But what it returns is purely based on the what the programmer wants it to be. The meat of this program is the following line.
cout << "Hello World! " << endl;
cout means the stream that the data is sending to, << is called the insertion operator, it will send the data into the stream. And endl; is a symbol to let the program knows it is going to go to the next line. It is possible to link the input data like the following to create something like this. Notice the last example, show you how to control how many line to skip.
cout << "Regular style, outputting message. " << endl;
cout << "There " << "are " << "more " << "messages. " << endl;
cout << "Let's skip 5 lines? " << endl << endl << endl << endl << endl;
Thoughts: This first tutorial is a lot longer than what I thought it will be... I did not expect it to be so lengthy. most programmers can write this piece of code within 20 seconds, when trying to explain it without any prior knowledge is a different story. And I will have to double check the facts of my explanation.

Practice Problem 1:
Write a program that print Good morning, Good afternoon, Good night in 3 separate lines. Tips, to go to next line, use endl. The output of the program should look like this.
Good morning
Good afternoon
Good night
See here for solution