Thursday, August 26, 2010

Pull your Strings together!

3 <- post your comments here

In writing this article the author assumes that the reader already has basic knowledge regarding variable declarations and has an idea of what data types are. this article mainly is about some of the often neglected and unnoticed principles regarding string variables.

instruction: rewrite the given sentence into c++ code
"assign 35 to an integer variable named 'intense_freshness'"

If you understood what the above mentioned sentence asks of you then you may proceed in reading this text. If it made no sense, then it is recommended that you google up these things in the interweb first before proceeding.
  • variable declarations c++
  • variable datatypes c++
  • variable naming conventions c++
also include in your interweb surf this one, for in processing these strings we'll use it predominantly
  • the for loop c++

Have you got it already? Good. Let's proceed. Of course understand that this post is not a reiteration of what should've been discussed in the classroom. Think of it as parcels of tips and tricks that the author deems significant in furthering the understanding of strings (experience based).

Listed below are the often neglected principles about variables declared as string. One of the advantages it give us is with regards to formatting your program's output to either the console (terminal) or to a graphical user interface you've specified (i.e. message boxes and the like). Let's begin, here goes...

First, as a general rule, any variable declared as type string is automatically regraded as an array. Thus, it could be treated and accessed as an array (to some extent). take for example the code below.

1. #include <iostream>
2.
3. using namespace std;
4.
5. int main ()
6. {
7. string label = "afterschoolfreetime!";
8. cout << "GivenString: " << label << endl;
9. cout << "FirstCharacter: " << label[0] << endl;
10. cout << "LastCharacter: " << label[19] << endl;
11.
12. return 0;
13. }


The output of the above given code would result to:

GivenString: afterschoolfreetime!
FirstCharacter: a
LastCharacter: !

Significantly this could be used to address different problems such as:
  • looking for a particular letter in a given word.
  • replacing that particular letter in a given word.
  • reversing the arrangement of letters in a given word.
  • Converting particular letters in the word to uppercase (difficulty: 3stars)
Deciding to research on these things is left to your personal discretion.

Second, A variable declared as string would always be terminated (ends with) a NULL value. Focus on this code:

1. #include <iostream>
2.
3. using namespace std;
4.
5. int main ()
6. {
7. string label = "afterschoolfreetime!";
8. int counter = 0;
9.
10. while (label[counter] != 0)
11. {
12. counter = counter + 1;
13. }
14. cout << "StringLength: " << counter;
15. return 0;
16. }

The important thing to note is that having a null value at the end of each string variable enables us to "mark the end" of the processing method we wish to apply the the string. try it out.
  • using a loop to traverse the given string, change the o's into zero's (0). then print the output string.
Sometimes in some platforms NULL literally works instead of putting in 0 in the condition statement in the loop. I'm using ubuntu's gedit right now so for the record, the above given code is sure to work if you are to use ubuntu too.

Third and last is, Never forget that the one's who made c++ are very (major major) smart people. So they make things easier. Take this code for example.

1. #include <iostream>
2.
3. using namespace std;
4.
5. int main ()
6. {
7. string label = "afterschoolfreetime!";
8. cout << "StringLength: " << label.length() << endl;
9. }

notice the ".length()" appended to the name of the variable we've declared. This is the actual equivalent (not coded as is) of the second code we've discussed. Isn't it great, now we have this vague conviction that indeed all "manual codes" have their shortcut equivalents designed by very smart people.

The keyword is research. Never be satisfied of the ideas discussed in class. There are so many fragments of code waiting to be understood by you, dear reader. However, be convinced that KNOWING ONLY THE SHORTCUTs would get you nowhere. for in the real world, arrays would always be arrays, counters would always be counters, and variables would always be variables (Mr. Fritz Olanz)

Pull your strings together! I assure you it's worth your time.

Just a quick note: Congratulations to the first year batch of the department of Computer Sciences for the increased number of participation in the upcoming Ateneo Computer Programming Competition (ACPC). This is a major major increase. Goodluck to us.

-o0o-
nothing follows

single array

0 <- post your comments here

#include <iostream>

using namespace std;

int main()
{
int array[5] = {0}; //initialize to 0

for(int i = 0; i < 5; i++)
{
array[i] = i; //initialize to value of i
}

for(int i = 0; i < 5; i++)
{
cout << array[i] << " "; //output the value of array
}

return 0;
}


Output:

0 1 2 3 4

Tuesday, August 24, 2010

post your question.. tnx

4 <- post your comments here

question here...

Hello World!

4 <- post your comments here

The first thing you need to know is how to "empty yourself". In the course you've chosen ( IT ) you'll be dealing with a lot of definitions and syntax, some of which you might have opposing opinions about. Allow them to breach into your system and become a way of life. A good way to start maybe is by making your first ever C++ program.


1. #include <iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. cout << "Wassup world!";
8. return 0;
9. }

included in the lines above are the following important things which you may want to look up to get to know them more.
  • line (1) - preprocessor directives
  • line (3) - namespace
  • line (5) - the main function
  • line (7) - standard output operator
  • line (8) - return statement
  • lines (6) & (9) - curly braces
We welcome questions.

-oOo-
nothing follows