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

No comments:

Post a Comment