Archive

Archive for the ‘OOP344’ Category

Installing Oracle Java 7 on Debian

March 13, 2013 Leave a comment

I’ll just get to the point. First you need to download it and transfer it to your Linux box.

Extract the package

tar -xzvf jdk-7u17-linux-i586.gz -C /usr/jvm

Now you can make the java and javac available like an alias by the following

update-alternatives --install /usr/bin/java java /usr/jvm/jdk1.7.0_17/bin/java 1065
update-alternatives --install /usr/bin/javac javac /usr/jvm/jdk1.7.0_17/bin/javac 1065

and you are done ūüôā

You can check your installation by typing the following

java -version

Categories: OOP344

Dynamic Memory Management Overview with C++ 1

April 6, 2010 Leave a comment

Allocating memory

Dynamic Memory allocation is done by using the keyword “new” in c++. When creating dynamic memory a pointer is always needed. ¬†Let us start by looking at one very simple example. (the code below does not deallocate memory which I will get to next.)

main(){
int* iPtr;
iPtr = new int;
*iPtr = 10
cout << *iPtr << endl
}

so there you have it, a simple dynamically allocated int. The program above does not give us any advanges, but I used it since its a good example for learning purposes.

Deallocating memory

It is very important to give the memory back to the OS after we are done with the memory we need. Memory is given back to the OS by using the keyword “delete”. It is very simple to use, in the example I did not deallocate memory back to the OS, which makes it unusable¬†until¬†the OS is restarted. ¬†Here is an modified version of the program above which deallocates memory.

main(){
int* iPtr;
iPtr = new int;
*iPtr = 10
cout << *iPtr << endl
delete *iPtr //simply remove the allocated memory pointed by *iPtr
}
Keep in mind that “delete *iPtr” does not delete the pointer, but it deletes what is it pointing to.
Categories: OOP344

Function Templates Example

April 6, 2010 Leave a comment

It is common to see functions that do almost the same thing but for different data types. We don’t need to write that code many times and just change the data types. The correct solution is to use function templates. I have written a swap function to demonstrate the use of function templates.

template <template T>
void swap(T a, T b){
T temp;
temp = a;
a = b;
b = temp;
}

we the function template above, we can pass it an int or float and the compiler will automatically create separate functions as needed.

Categories: OOP344

C++ Reference Operator

March 19, 2010 Leave a comment

Reference is a C++ feature, it is not available in the C programming language. A reference is less powerful but safer than the pointer. Reference is having two names for the same variable. Let us start by looking at some examples.

int A = 5;
int& rA = A;

cout << rA << endl;

A = 7;

cout << rA << endl;

Output:

5
7

No matter which variable’s value gets changed both A and rA change.

References can be used to replace pointers in some cases. As you may recall functions in c/c++ can only return one value and programmers use pointers in order to get around that! References can be used instead of pointers and you do not have to use the * operator in your functions.

Categories: OOP344

Passing arguments to a C program from terminal

February 21, 2010 Leave a comment

Have you ever wondered how some UNIX commands take arguments? It can be done very easily with C.

You can code the rm, cp, ect commands in no time. Here is a sample code which you can use to understand passing arguments.

int main(int argc, char* argv[]){
  int i;
  for(i = 0;i<argc; i++){
    printf("%d: %s\n", i+1, argv[i]);
  }
  return 0;
}

argc is the number of arguments being passed in, and argv is a char pointer array which has the arguments.

Categories: OOP344

enum intro in C

February 20, 2010 Leave a comment

Enums can be used to reduce #define statements in a C program, although they can have more uses.

Here is an example of enum.

#include  
int main()
{
enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

Days TheDay;
int j = 0;
printf("Please enter the day of the week (0 to 6)\n");
scanf("%d",&j);
TheDay = Days(j);

if(TheDay == Sunday || TheDay == Saturday)
printf("Hurray it is the weekend\n");
else
printf("Curses still at work\n");

return 0;
}

One thing to keep in mind is that enums start from 0 so in the following line of code

enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

Sunday is assigned 0, Monday is assigned 1 and so on..

It is possible to make them start at a different number if we assign a number to it as shown below.

enum Days{Sunday = 1,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

In the example above, Sunday would have the value 1, Monday would have 2 and so on….

Feel free to ask any questions in the comment box below.

Categories: OOP344

Pointers to functions in C/C++ intro

February 10, 2010 Leave a comment

Pointers to functions are a very interesting and elegant programming technique. They can be used to replace long switch or if statements!

Anyone can start using pointers to functions all you have to do is to understand what they are and to learn its syntax.

What is a Function Pointer?

Function pointers are simply pointers that point to a function! Functions have addresses in memory just as variables do.

Syntax

The syntax can be a little strange if you are seeing for the first time but its very easy to grasp.

Suppose we have the following function


int add(int a, int b){
return a+b;
}

we can start creating a pointers to function by writing

int (*fptr)(int, int);

int is the return type of the function and (int, int) is the arguments the function accepts.

The next step is to give our pointer to function the function’s address. We do that with the following code

fptr = add;

Now that fptr points to the “add” function we can actually start calling out function using the fptr pointer. We use the code below to do it.


int c = 0;
c = (*fptr)(a, b);

As you can see pointers functions are easy to learn ūüôā I will make more posts soon to show how you can get rid of a long switch statement by using pointers to functions. I have also posted a compilable example below so you can experiment with.


/* Author: Fardad Soleimanloo */
#include
int add(int a, int b){
return a+b;
}
int main(){
int (*fptr)(int, int);
int a = 10;
int b = 20;
int c;
fptr = add;
c = (*fptr)(a, b);
printf("%d\n", c);
return 0;
}

Categories: OOP344

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: ,