Archive

Archive for January, 2010

Week 3 Challenge

January 29, 2010 5 comments

By using only the function in biof.c you can print integers by using the function below

void bio_putint(int val){
bio_putch(val);
}
Categories: OOP344

Using && instead of “if statment” in C/C++

January 27, 2010 3 comments

C/C++ is a lazy language. It will only execute code when it has to!  When using AND if the first condition is false it will skip the second one.  The first example below uses an if statement and the second one uses AND which is a little faster.


#include <stdio.h>

int main(){
int number[10] = {5, 1, 8, 3, 6, 5, 3, 8, 4, 6};
int i = 0;
while(i < 10){ if(number[i] > 5)
printf(“%d\n”, number[i]);
i++;
}
return 0;
}

The above program takes 0m0.004s to execute.

The program below is using “AND” instead of if statement


#include <stdio.h>

int main(){
int number[10] = {5, 1, 8, 3, 6, 5, 3, 8, 4, 6};
int i = 0;

while(i < 10){ number[i] > 5 && printf(“%d\n”, number[i]);
i++;
}
return 0;
}

The program above executes in 0m0.003s

Using AND can have some performance advantage over if statements, but it is only faster by a tiny amount.

Categories: OOP344

Pointers in C/C++

January 26, 2010 1 comment

Pointers are sometimes confusing to C/C++ programmers at first, but they don’t have to be! Pointers are just like any other variable in C/C++ but instead of holding an actual value they hold an address. I will use some simple code to show you how pointers work. Take a look at the code below.


int number = 4;
int* pNumber = &number;

int* pNumber = &number creates an integer pointer and assigns number’s address to it. In C/C++ the character ‘&’ means the “address of”. The simple program below will give you a better understanding of addresses.


#include <stdio.h>

int main()
{
int number = 4;
int* pNumber = &number;
printf(“%u, %u\n”, &number, pNumber);

return 0;
}

The output would be:

3214459548, 3214459548

Categories: OOP344 Tags: ,

Assigning integers to char type in C/C++

January 19, 2010 Leave a comment

In c/c++ the char type can be used to store a single character or small integers.

Have a look at the program below and determine what the value of “ch” would be without running the program.

#include <stdio.h>

int main(){

char ch = 127;
ch++;

return 0;
}

What would the value of ch be when the program is executed? Most people would guess it would be 128, but the right answer is -128.

The reason behind it is because a signed char type in c/c++ only accepts values from -128 to 127 and when ch++ is executed 127 goes to the next avaialble number which is -128.

Categories: OOP344

Just left OOP344 class

January 13, 2010 Leave a comment

I just left my first class of OOP344,  my professor is Fardad and he is a brilliant teacher.  I look forward to the topics he will be covering in the course.

Categories: Uncategorized