Thursday, March 18, 2010

OOP344 Challenge on March 18th

Challenge: write the pntBits() function in one line.
Here is my solution.

Saturday, March 13, 2010

A class to perform like Linked list

Linked list is a common data structure type. FIFO (First In First Out) is its principle. Click here to see more about linked list.

Here is my solution:
A class named queue that can perform like a doubly-linked list.

It's really a basic version that only works for int right now, but it can be improved to associate with other data types and derived data types.

qsort() practice

The practice is actually a question from my OOP344 TEST1. Requirement is as follows:
$ Q2 hello i am writing a text
a
am
hello
i
text
writing
$_
Write a program to produce the output above, using qsort() fucntion.

The solution is here. It's really a simple function, however I didn't do it completely correct during the exam. I think there are two reasons: first, I am not familiar with the qsort() function, though the spec is given; second, I didn't practice how to pass a function pointer as a parameter to a function.

Thursday, February 25, 2010

Dynamic memory allocation in C and C++

Sometimes, when we want to declare a certain array, but we don't know the length of it or we want it to be specified during the running process, we can use the technique of allocating memory spaces dynamically.

In C we use it like this. (We need to include stdlib.h in front of the program)
Datatype* variable = (Datatype*) malloc ( maxindex * sizeof(Datatype));

Eg.
int* d = (int*) malloc (8 * sizeof(int));
Then we can treat d as a normal array. We can assign value to each element
int i = 0;
for(i = 0; i < 8; i++) d[i] = i * 10;

It is important to free the memory space to avoid memory leak;
To free memory we say:
free(d);


In C++ we have "new" and "delete", and the way to use is like this:
double* p = new double [5]; // created 5 objects for double
// delete [] p; to delete
Employee* e = new Employee; // created one objects for class Employee, the default constructor is called.
// delete e; to delete
using member function: e->display();

The other thing we need to consider is how to copy the content of an array to another, both of which are dynamically allocated.
1. Create a same datatype pointer (we call it destination here).
2. Allocate a proper amount of space for the destination.
3. Copy the content from the source to the destination.
4. Delete the content of the source. Get rid of the old one.
5. Make the older pointer (we call it source here) pointing to the destination.
6. Because the destination is local, so after function calling, it will be gone automatically.

During next week, I am going to create a class containning a void pointer as a data member that can be used like the dynamic array.

Thursday, February 18, 2010

OOP344 Challenge 2: myPrint function

The challenge can be found here.
Below is my solution.




#include "biof.h"
#include "<"string.h">"
#include "<"stdio.h>"
#include "<"stdarg.h>"
#include "<"stdlib.h>"

char* toUpperCase(char* str){
int i = 0;
for(i = 0; i < strlen(str); i++)
if(str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
return str;
}

char* myItoa(int val, char* str){
int i;
int j;
int length;
int scale;

if (val == 0){
str[0] = '0';
str[1] = '\0';
}
else{
for (i = 1, length = 0; val / i != 0 ; i *= 10, length++);
if (val > 0) {
for (i = 0; i < length; i++){
scale = 1;
for (j = 0; length - j > i + 1; j++)
scale *= 10;
str[i] = (val / scale) % 10 + 48;
}
str[i] = '\0';
}
else {
val *= -1;
str[0] = '-';
for (i = 0; i < length; i++){
scale = 1;
for (j = 0; length - j > i + 1; j++)
scale *= 10;
str[i+1] = (val / scale) % 10 + 48;
}
str[i+1] = '\0';
}
}

return str;
}

char* myFtoa(double val, char* str){
int decPart = ((int)(val * 100)) % 100;
int intPart = (int)val;
int roundPos = ((int)(val * 1000)) % 10;

char dot[2] = ".";
char decStr[3] = "";
char intStr[20] = "";

if(roundPos >=5)
decPart++;

myItoa(decPart, decStr);
myItoa(intPart, intStr);

strcpy(str,"");
strcat(str, intStr);
strcat(str, dot);
strcat(str, decStr);

return str;
}

void bio_putint(int val){
char temp[20] = "";
bio_putstr(myItoa(val, temp));
}

int myPrint(const char* msg, ...){
int i = 0;
int num = 0;
char buf[100];
int dTemp;
int hTemp;
double fTemp;
char cTemp;
const char* strTemp;


va_list args;
va_start(args, msg);


for(i = 0; i < strlen(msg); i++)
if(msg[i] == '%' &&
(msg[i+1]=='b' || msg[i+1]=='c' || msg[i+1]=='f' ||
msg[i+1]=='s' || msg[i+1]=='x' || msg[i+1]=='X') )
num++;

for(i = 0; i < strlen(msg); i++){
if(msg[i] != '%')
bio_putch(msg[i]);
else if(msg[i] == '%'){
buf[0] = 0;
switch(msg[i+1]){
case 'd': dTemp = va_arg(args, int);
bio_putint(dTemp);
i++;
break;
case 'c': cTemp = va_arg(args, char);
bio_putch(cTemp);
i++;
break;
case 'x': hTemp = va_arg(args, int);
itoa(hTemp, buf, 16);
bio_putstr(buf);
i++;
break;
case 'X': hTemp = va_arg(args, int);
itoa(hTemp, buf, 16);
bio_putstr(toUpperCase(buf));
i++;
break;
case 'f': fTemp = va_arg(args, double);
bio_putstr(myFtoa(fTemp, buf));
i++;
break;
case 's': strTemp = va_arg(args, const char*);
bio_putstr(strTemp);
i++;
break;
default:
bio_putch(msg[i]);
break;
}
}
}

va_end(args);
return num;
}

int main(){
int howMany;
bio_init();
howMany = myPrint("int %d, char %c, string %s, hex %X, float %f.\n", -56, 'M', "OOP344", 12, 12.34567);
myPrint("Printed %d parameter(s)", howMany);
bio_getch();
bio_end();
return 0;
}






Tested under VCC and BCC.
output is:
int -56, char M, string OOP344, hex C, float 12.35.
Printed 5 parameter(s)

I add a logical control that can round the decimal value. For example: 3.675 will be printed 3.68, while 5.321 will be printed 5.32.

Monday, February 15, 2010

About blogging a snippet of code in Blogger

Today I reviewed those blogs I've posted before. I found the one called "OOP344 Challenge". It is the challenge from my OOP344 class. I did it at night on the same day the challenge was announced. However, it took me more time to figure out how to blog code in Blogger than to do the challenge.

I remembered I posted my blog and fell asleep. To my surprise, when I checked the blog the next morning, it wasn't the code I'd posted. There is no indentation any more, and unbelievably, some line had changed.

It took my the whole morning to fix this. No matter how I edit and post it, I just cannot get the proper indentation. Finally, I yielded. I did it without indentation, at least, the code remained unchanged.

Because of editing, my blog post time was postponed. And I am afraid that I might not be the first two who solved the challenge. :(

Anyway, I still want to know how to post code here properly. *_*

Saturday, February 13, 2010

IRC meeting

Fortunately, we had our group IRC online meeting with Fardad this Thursday. However, not all members attended, because we have eight people and it is extremely difficult to schedule a period of time that is good to everybody. So I hope my blog can be a helpful reference to those who missed the meeting.

Actually, it is a short meeting. The log file can be found here.

I will pick some important points from the meeting.
  1. While someone ping you, you can reply s/he pong to show you are there.
  2. While someone is talking, it is very rude to crosstalk with other people. Use /query nickname to open a private chat.
  3. Small jokes are allowed, however, do not overdo it.
  4. Every now and then put an ok or 'got it' or something like that to indicate you are there.
  5. If you want to show a certain piece of code or share a url, here are some good tools: tinyurl.com or pastebin.com

That is it. And I am expecting the next IRC online meeting, because it's a little bit exciting and it feels like I am doing real business meeting. lol