Saturday, January 31, 2009

Lab Exam

Visit this site - http://www.coders2020.com/what-is-infix-prefix-postfix-how-can-you-convert-from-one-representation-to-another-how-do-you-evaluate-these-expressions. This will serve as your guide for your upcoming midterm lab exam this monday.

Saturday, January 24, 2009

Stack and Queue

The codes for stack and queue are already available under Course Materials-> Handouts/ Learning Modules in elms.

Monday, January 12, 2009

Top Students for Prelim

1. EMAN, ROÑARD C. (BSIT2B) 96
2. PAASA, PATRICK ANGELO P. (BSIT2C) 93
3. LASAY, NORRIS LEMUEL C. (BSCS2) 90
4. ZABATE, MARVIN JOHN B. (BSCS2) 88
5. LUI, ADRIAN E.(BSCS2) 88
6. JAMIO, PAUL JOHN I. (BSIT2B) 88
7. MEDILLO, DARWIN I. (BSIT2A) 88
8. REANCHO, LIRALYN S.(BSIT2A) 88
9. AMARANTO, MAYLYN T. (BSIT2C) 88
10. BERNARDINO, JAMES CARL H. (BSIT2C) 88
11. CARILLO, RAFFY D. (BSCS2) 88

Saturday, January 3, 2009

Templates

Function templates
Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.

The format for declaring function templates with type parameters is:

template function_declaration;
template function_declaration;

The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.

For example, to create a template function that returns the greater one of two objects we could use:

template <class myType>
myType GetMax (myType a, myType b) {
return (a>b?a:b);
}


Example
// function template
#include
using namespace std;

template <class T>
T GetMax (T a, T b) {
T result;
result = (a>b)? a : b;
return (result);
}

int main () {
int i=5, j=6, k;
long l=10, m=5, n;
k=GetMax<int>(i,j);
n=GetMax<long>(l,m);
cout <<>return 0;
}


Class templates
We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example:
template <class T>
class mypair {
T values [2];
public:
mypair (T first, T second)
{
values[0]=first; values[1]=second;
}
};

Example:
// class templates
#include
using namespace std;

template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
T getmax ();
};

template <class T>
T mypair::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}

int main () {
mypair <int> myobject (100, 75);
cout <<>return 0;
}


Template Specialization
// template specialization
#include
using namespace std;

// class template:
template <class T>
class mycontainer {
T element;
public:
mycontainer (T arg) {element=arg;}
T increase () {return ++element;}
};

// class template specialization:
template <>
class mycontainer <char> {
char element;
public:
mycontainer (char arg) {element=arg;}
char uppercase ()
{
if ((element>='a')&&(element<='z'))
element+='A'-'a';
return element;
}
};

int main () {
mycontainer<int> myint (7);
mycontainer<char> mychar ('j');
cout <<>return 0;
}
Observe the results of the codes above. A short quiz will be given about this during lecture.

Tuesday, December 9, 2008

U.S. Capitals Lab

Objective: to maintain an ordered linked list

Write a program that maintains a linked list of U.S. capitals in alphabetical order. First of all, create a text file containing a few names of state capitals. Your program should get the names from the file, inserting them in the linked list and then call the sorting function and finally display the results.

Create a text file and name it as “stateCapital.txt”

Here is the content of your text file: State Capitals

Montgomery
Juneau
Phoenix
Little Rock
Sacramento
Denver
Hartford
Dover
Tallahassee
Atlanta
Honolulu
Boise
Springfield
Indianapolis
Des Moines
Topeka
Frankfort
Baton Rouge
Augusta
Annapolis
Boston
Lansing
St. Paul
Jackson
Jefferson City
Helena
Lincoln
Carson City
Concord
Trenton
Santa Fe
Albany
Raleigh
Bismarck
Columbus
Oklahoma City
Salem
Harrisburg
Providence
Columbia
Pierre
Nashville
Austin
Salt Lake City
Montpelier
Richmond
Olympia
Charleston
Madison
Cheyenne

Refer to http://www.lakesparadise.com/education/elearning/show_tutorial.php?id=25 for C++ file handling.

http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html for C++ String class Examples and Tutorial

Tuesday, December 2, 2008

PRELIM EXAM DETAILS

Date: December 4, 2008
Time --------------------- Location ----------------- Class
8:00 - 10:00 AM -------- L204 --------------------- IT2A
8:00 - 10:00 AM -------- L203 --------------------- CS2
10:00 - 12:00 AM -------- 302 --------------------- IT2B
10:00 - 12:00 AM -------- 303 --------------------- IT2C

Note:
  • Irregular students from IT2A/CS2 may take the exam together with the IT2B/IT2C.
  • Be on time. The exam is good for 90 minutes.
  • Failure to attend to one of these schedules may cause failure for prelims.

Sunday, November 23, 2008

Sorting items in linked list


Please do not ask regarding the algorithm if your having problem translating it. Learn to read algo. If your sure something is wrong with it, call my attention.

Open your previous devC files and add this algorithm I'm about to give in Node.h.

step 1: Create a function named as Sort (lets assume you want to sort your nodes/items in ascending order) with void as its return type.
step 2: Inside your function, transform this algorithm into a C++ code.

1. Create a pointer (current) of type Node

2. Equate current to head

3. Iterate using while loop until current is null

a. Create another pointer (next) of type Node

b. Equate next to current->link

c. Iterate using a while loop until next is null

i. Test using an if statement whether current’s data value is greater compared to next’s data value (if true, proceed with the bullets list)

Ø Create a pointer (temp) of type Node

Ø Equate temp to a new Node

Ø Assign the values of current (data) to temp (data)

Ø Assign the values of next (data) to current (data)

Ø Assign values of temp (data) to next (data)

Ø Delete temp using the keyword temp

ii. move the next pointer to next->link

d. Move current pointer to current->link