Monday 4 April 2011

Exam Cheatsheet

So my exam for Computer Programming II is this Thursday, and having looked at the course outline my prof made for us, it clearly states that we are allowed to bring in "...one page during the exam time to assist you in memorizing some syntactic matters." So basically this means that we can bring in a page full of examples of everything we learned this semester. Hopefully nobody reminds her about this and makes her change her mind... She's pretty cool though, so I'm sure if we all show up with them, she'll be cool with it. 


Anyway, I decided to share mine with everyone here! Feel free to use it as a reference, or even as study help for your own class!


Download Link 1


Calling by reference:

int x=0;

int *ptr;
ptr=&x;           //ptr now points to x

void function(int *y)
{
       *y++;   //y points to x
}

int arr[5];
arr[3]; //same as *(arr+3)

pointer to function:
double (*fp)(double,double);

fp=pow; //fp now acts like pow

Useful String Functions:
int atoi( const char *nPtr )
char *gets( char *s );
char *strcpy( char *s1, const char *s2 )
char *strcat( char *s1, const char *s2 )
char *strstr( const char *s1, const char *s2 );

Structs:
typedef struct dummyname
{
       int x;
       //elements
}StructName;

StructName identifier;

indentifier.element=value;

Dynamic Memory:
ptr = (int*)malloc(5*sizeof(int));

Linked Lists:
typedef struct node
{
       char data;
       struct node *next;
}Node;

Node *p;
p=(Node*)malloc(sizeof(Node));
p->data=’a’;
p->next=head;
head=p;

Enum:
typedef enum dummyname
{
       sun,mon,tues...
}Weekday;
(sun=0,mon=1,....)

force values:
enum{sun=5,mon=8,...};


Union:
As large as its largest element.
Holds one at a time.
Declare like a struct.

Preprocessor:
#define SQUARE(x) ((x)*(x))
#undef
#if, #ifdef, #else, #endif

#define TOKCAT(x,y) x ## y

Variable Length Args:
int funct(int n,...)
{
       va_list args;
       va_start(args,n);//n is first param
loop()      variable=va_arg(args,type);
       va_end(args);
}

Command Line Args:
int main(int argc,char *argv[])

iostream:
using namespace std;
cout<<”stuff”;
cin>>variables;

Default Args:
int func(int x=1);

Templates:
template <class T>
T square(T value)
{
       return value*value;
}

int x=square(5);
float y=square(5);

Classes:
Class ClassName
{
public:
void func();
       const int x;
private:
      
       void inlineFunct(){return 0;};
       //private ones
};

void ClassName::funct()
{}

//constuctor
ClassName::ClassName()
{/*default values*/}

//destructor
ClassName::~ClassName(){}
Data Initializer:
ClassName::ClassName()
       :x(0)
{/*default values*/}
Composition:
Class1::Class1(Class2 &identifier)
{}

Friend Functions:
class ClassName
{
       friend void func();
public:
private:
}

void func(ClassName name)
{
       //access private stuff
}

new and delete:
int ptr = new int(3.141592654);
int arr = new int[10];
delete []arr;

Static Class Members:
ClassName::var
InstanceName.var

ClassName::ClassName()
{
       var++;
}

Operator Overloading:
class ClassName
{
friend ostream& operator<<( ostream&, const ClassName&);

ClassName& ClassName::operator=(const ClassName x)
{
       var1=x.var1;
return *this;
}

ostream& operator<<(ostream &o, const ClassName x)
{
       o<<x.var1<<etc;
return o;
}

Sunday 3 April 2011

The Basics of Pointers - Part 1

Now, we've learned to deal with variables. In most programming languages, you'll be able to deal with "references" to variables (more of why we would want to do this in the next post), such a thing in C is called a pointer. Let's just jump right into the code, shall we?

int x;
int *ptr;

Here, we are declaring an integer called x, and after that, we declared what is called "a pointer to integer". A pointer is declared by specifying the type, and then putting an asterisk before the desired identifier. Note that both of the following are correct syntax which do the exact same thing.

int *ptr1;
int* ptr2;

It doesn't matter is the asterisk goes before or after the space.

Last time, we learned about memory addresses. Basically, a pointer can be thought of as a variable which holds a memory address of a specific type of variable. Remember how we used "&" to access the memory location of a variable? Well here's where it comes into play. Let's say we have:

int x=5;
int *ptr;
ptr = &x;

This would make ptr point to x. In a way, this means that we can indirectly access the content of x. To do this, we put an asterisk in front of the pointer's identifier. For example, using the code above...

printf("%d",*ptr);


Result: 5

In the same way, we can modify the content of x by using the pointer.

*ptr = 42;
printf("%d",x);

Result: 42

By putting the star in front of your pointer's identifier, it is like saying: "The content of..."

Note that in order for a pointer to work properly, it must be declared properly as well. By this, I mean that you cannot reference a float from a pointer to integer. This:

float y;
int *ptr3;
ptr3 = y;

will cause a compilation error. Why?

When you declare variables, bytes of memory are taken up. The amount of bytes is determined by what type of variable it is. An integer will typically take up 4 bytes, while a floating point number will take up 8. This means that when we have a pointer to integer, the pointer is expecting a memory address such that 4 bytes after it are taken up. In this example, it sees that it would need 8 bytes, that is, a pointer to integer would only hold half the data, and would be full after "filling up" half way. The compiler sees that this is a problem, and reports and error.

So we know about using pointers now. Great. So why would we ever want to do this?
Stay tuned.

Thursday 31 March 2011

Memory Addresses

When you declare a variable of any type, memory is taken up. Each variable then has what is called a "memory address" which is an indication of where it is stored in memory. The memory address of any variable is indicated in hexadecimal (or honestly, some other number base, it may be octal, but that's not really important for now). Accessing the memory address of any variable is easy.

int x;

&x  //is the memory address of x

So you may be asking yourself why you would ever need to know things like this.
WELL
A simple application in which you're basically required to use memory addresses is the use of  scanf().
scanf() can be thought of doing the opposite of printf(). Instead of outputting information to the user, it takes information from the user and stored it in variables. Here's how we would ask the user to store a value for an integer x;

int x;
printf("Input a value for x: ");
scanf("%d",&x);


Notice how the format string for scanf() is exactly the same as printf(). The main difference here is that the second parameter is asking "where do you want to store this value". The obvious answer is "in x," but really, you're going to want to be storing it in the space that x is occupying in memory - its memory address. Also notice how we cannot print to the screen using scanf. In order to let the user know what he is supposed to input, we must use printf() to print instructions as the program executes.

Monday 28 March 2011

Nested Loops

Just a quick post today on nested loops.

The basic concept is that you will have one (or more) loop inside of another loop. Each internal loop will run and terminate, and then any external loops will then run. Here's an example:

for(int i=0;i<5;i++)
{
    printf("i = %d\n",i);
    for(int j=0;j<5;j++)
    {
        printf("j = %d\n",j);
    }
}


The output would be:

i = 0
j = 0
j = 1
j = 2
j = 3
j = 4
i =1
j = 0
j = 1
j = 2
j = 3
j = 4
i =2
j = 0
j = 1
j = 2
j = 3
j = 4
i =3
j = 0
j = 1
j = 2
j = 3
j = 4
i =4
j = 0
j = 1
j = 2
j = 3
j = 4

So you see that it is really much like a normal loop. Each time a loop runs, it must execute all the code contained within its scope before looping again - even if it contains another loop.

This is just a quick basic concept that should be understood. This is very useful when dealing with multi-dimensional arrays (matrices), but I'll come to that in another lesson.

Sorry for the short post! I need to get back to studying for exams.

Friday 25 March 2011

Loops

Loops are useful for running one or more lines of code repeatedly. The basis of how a loop works is similar to an if statement: There is a condition, and if the condition fails, the loop stops. The first loop I'll show you is a called a while loop, here's its syntax:

while(condition)
{
    //code
}

Pretty simple. Let's see this in an example

int i=0;
while(i<10)
{
    printf("%d\n",i);
    i++;
}

(For those who don't know, i++ is the same as saying i=i+1 or i+=1. It means "increment i")
This will print out the numbers 0 to 9.
Basically we start by initializing an integer to 0. We tell our while loop "Loop the code while the value of i is less than 10". In the first pass, the condition passes the test because 0<10. Good! So we go ahead and call our printf function. Here's the important part: we increment i by 1. So now the value of i is equal to 1 and we return to the beginning of the while loop and run the condition again. Now we're asking "Is 1 less than 10?" Yes it is. We run the loop again. printf, increment, loop again: "Is 2 less than 10" Yes....you get the idea.
Let's look at the last pass through the loop, when i is equal to 9. "Is 9 less than 10?" Yes, so go into the loop, call printf and increment i. Now we go back to the top yet again, but this time we ask "Is 10 less than 10?"
The answer of course is NO, so we skip the loop entirely, just as we would if a condition in an if statement failed.

This is a simple use for a while loop, and in fact, while loops using a simple incrementing variable can be rewritten as another type of loop: a for loop. The syntax is a little more complicated, but in the end, for loops generally end up being easier to manage.

for(initial state;terminating condition;increment)
{
    //code goes here
}

Let's look back at our while loop example for a sec. See how we declared an integer i before the loop? That's our initial state. We then had a condition in the while loop. That of course is the terminating condition. At the end of the while loop, we increased the value of i. That's our increment.

Here's the same loop above rewritten as a for loop:

for(int i=0;i<10;i++)
{
    printf("%d\n",i);
}

Both loops essentially do the exact same thing. The real difference for you is that in a for loop, all the useful loop information is stored in the parentheses after the word "for".
Note that in the for loop, I declared my incrementing variable inside the parentheses. This is a matter of preference, but I do it whenever possible. Basically, I'm saying that the following is still correct:


int i;
for(i=0;i<10;i++)
{
    printf("%d\n",i);
}


While this is still correct, I find it very beneficial to declare your incrementing variable inside your loop. This way, the variable "dies" after the loop ends. This means that you can reuse that variable name for other loops and you don't have to worry about this loose variable floating around your program. This means that I could do this:


for(int i=0;i<200;i++)
{
    //run some code
}

for(int i=0;i<42;i++)
{
    //run some different code
}

And I won't have to worry about which variable I used before. Again, it really is a matter of preference. I showed you mine.

When writing loops, you'll likely accidentally code an infinite loop at some point. For example, take a look at this:

int x=5;
int y=2;
while(x==5)
{
    printf("...");
    y++;
}

With a quick look at the code, you'll see that our condition is always true! This means that the loop will never end. Likewise, the following will be an infinite loop.

while(1)
{
    //code
}

Thinking back to our logical operators, a logical statement return a value of 1 if true or 0 if false. Thus, our condition is interpreted as "While 'a true statement', run this code." No wonder it will loop forever!

It is also important to check your for loops for infinite loops, since for loops especially are usually not meant to be infinite.

for(int i=0;i>-1;i++)
{
}

will be an infinite loop since we are looping as long as i is greater than -1 and each time the loop runs, i is incremented. An infinite for loop can also be forced by writing:

for( ; ; )
{
}

No initial state, no terminating condition, no increment. The loop has noting to worry about and will go on forever.

Next time, I'll talk about nested loops

Wednesday 23 March 2011

More conditions

So yesterday, we looked at if statements and how to use them with simple conditions. To make our conditions more complicated, we need to make use of more of the logical operators. I'll give a real life example:

"It is sunny and I am not indoors."

A pretty simple statement. Not thinking about coding right now, let's let:
p = It is sunny
q = I am indoors

If we wanted to write this as a condition for an if statement, we would have:

p && !q

Pretty simple, right? Now let's look at the statement:

"It is sunny or cloudy, but not raining."

An important note about logic: BUT has the same logical meaning as AND. So we have:

"It is sunny or cloudy, and not raining."

Now, an important point about how this statement is interpreted. Do you remember BEDMAS, the order of operations when doing arithmetic? This states that certain operators must be evaluated before others (the order is: brackets, exponents, division/multiplication, addition/subtraction). Well logical operators also possess a precedence factor as you'll see. Let's look at our statement again. It states that it is sunny or  cloudy, but not raining. At first glance, it may seem at though the condition first checks to see whether it is sunny or cloudy, and then checks to see if it is raining. i.e. this:

(sunny or cloudy) and raining

In fact, when dealing with logic, I like to think of AND as a multiplication symbol and OR as addition (there's a discrete mathematical property that actually says this, but I won't get into that). In other words, AND will precede OR when dealing with operations, so our original statement actually means this:

sunny or (cloudy and raining)

See how it completely changes the sense of our condition? It's like comparing:

(5 * 5) + 10 = 250
and
5 * (5 + 10) = 75

Just a note to be careful when coding.
Great, now that that's out of the way, you are legally entitled to see some code.

Let's say we are writing a program in which we are asking the user for a time in hours and minutes. Obviously, we don't want to deal with hours greater than the number of hours in a day or minutes greater than 60, so a common fix is to set a default value if the user enters invalid data. We'll work from hours 0 to 23

int hour,min;
//prompt user for data
if(hour<0 || hour>23)
{
    hour=1;
}
if(min<1 || min>60)
{
    min=0;
}

Notice how we cannot use mathematical notation like: 0<hour<23. Instead, we must explicitly show each condition we want to include.

I hope this has been useful!

Next time, I'll talk about loops.

Tuesday 22 March 2011

Boolean logic and if statements

Without the need to make "decisions", your program will just be a linear stream of code that always outputs the same type of data. To check conditions, we use the if statement.

Before even talking about the statement itself, it is necessary to cover certain aspects of boolean logic. There are certain operators that you must know when dealing with logic.

Greater than >
Less than <
Negation !
Equality check ==
Inequality check !=
Greater than or equal to >=
Less than of equal to <=
And &&
Or ||

Standard parentheses will also come in handy when dealing with large conditions.

This seems like an eyesore right now, but I'm sure you'll soon find this natural. The basic structure of the if statement is as follows:

if(condition)
{
    //result
}

Seems pretty simple, right?
Let's say I have some integer x, and I want to print a line of text only if the value of x is currently greater than 5. We would have:

if(x>5)
{
    printf("This will get printed!!");
}

Easy!
Likewise, if we wanted to check if it was strictly equal to 5:

if(x==5)
{
    printf("It is equal to 5!");
}

The basic idea behind this boolean logic is that if the statement contained within the parentheses is true, then the condition returns a value of 1. If false, a value of 0 is returned.
This is very important, as new programmers often make a silly mistake, I will show you:

//This is a very common programming mistake
if(x=5)
{
    printf("What's wrong here?");
}

Can you see the difference? Rather than using the comparison operator, I used the assignment operator! Here, we are actually assigning a value to x, not comparing one! Although it is just a one character difference, it completely changes the meaning of the statement. Instead of having the condition return true or false (1 or 0), the condition is returning the assigned value of x, 5! The if statement also interprets this as a "true" value. In other words, no matter what the value of x was before the if statement ran, it will now be 5 when it does run, and hence, it will always return true, defeating the purpose of writing the statement altogether!

The beauty of if statements is that they don't simply end if your condition is false. There's also the option of passing the flow of control to an "else" which runs if the first condition is not true. For example:

if(8<5)
{
    //this will always be false
}
else
{
    //run some code!
}

This way, you can use the else as a "default" execution.
But wait, there's more! You can combine ifs and elses to create a string of conditions

if(condition)
{
}
else if(another condition)
{
}
else if(yet another condition)
{
}
...this can go on for a while
else
{
}

Notice how each consecutive else if will be run if the previous condition is not true. And that in this case, if none of them are true, then the else block will run. Note that it is not necessary to terminate with an else block. Hope this has been useful! Next time, I'll talk more about logic and how to create more complex conditions.

Sunday 20 March 2011

Scopes

When programming, it is important to know which variables you can use where. I'll explain.
Let's say you declare a global integer x, and you define a function myFunct(). In the definition of myFunct(). You would obviously be able to use this integer x. But does it go the other way around? That is, can you use variable declared inside a function outside the function?
Simple answer. No.

When you call a function, the "flow" of your program halts so that it can go run your function and then return with the results. Let's say we have a function add2()

int add2(int x)
{
    int y = 2;
    return x+y;
}

Yes, I know there's no reason to declare this variable y, but it's for the sake of the example. In this case, The variable called y only "lives" until the end of the function. After that, this variable no longer exists, and hence it can no longer be referred to (Things like this can be overcome using the "static" keyword. I'll mention this sometime). In fact, this variable x no longer exists either. x is just a temporary holder for a value that is used while the function is running!
Functions are just one example of where knowledge of a scope could be useful. Let's say you were running a for loop.

int i;
for(i=0;i<5;i++)
{
    //do stuff
}

Clearly, this is just an average for loop. Runs whatever code between its curly brackets 5 times. Now check this out:

for(int i=0;i<5;i++)
{
    //do other stuff
}

Can you see the difference?
The difference is that in the first case, the variable i would be usable even after the for loop was executed. In the second case, i "dies" as soon as the loop terminates. Personally, I like to use the second method just because it allows me to use the same identifier for all my loops (assuming they're not nested) without fear of that identifier already being declared and used for another loop. I also just find it neater ;)

Friday 18 March 2011

Simple Functions

Functions are the basis for writing programs in C.  They allow you to efficiently run repeated lines of code. For example, take a look at this mathematical function:

f(x) = x2

This function takes any real number x and squares it. Another way of writing this function could be:

f(x) = x*x

In C. Functions have 3 "Parts":
1) The function prototype which tells the program what to expect when the function is defined.
2) The function definition which tells the program how to execute the function.
3) The function call, which actually runs the function.

For this example, let's look at this square example. To declare a function, you must specify its return type (more on this is a sec) and then give the function an identifier and specify its parameter type. The parameters are the values that get passed into and used by the function (in f(x), our parameter is x). Here's a function prototype for our square function.

int square(int);

Later in your program, usually after your main method (unless you decide to declare it before, in which case a function prototype is not necessary), you will include your function definition. Here's the definition for square.

int square(int x)
{
    return x*x;
}

A really simple function. The program will take whatever value gets passed into the function. That value is now represented by x. The function now calculates the square of x and returns the value back to wherever the function was called. Here's an example showing how to call this function

#include<stdio.h>


int square(int);


int main()
{
    y=2;
    printf("The square of %d is % d.\n",y,square(y));
    return 0;
}


int square(int x)
{
    return x*x;
}


Compile this and you'll see that the output of the program is:
The square of 2 is 4.

Notice how to call the function, you simply write the function name and then the values you want to pass into the function (These are called the arguments).

Wednesday 16 March 2011

Declaring and initialising variables

Today, I'll show you how to declare and initialize variables and talk about the possible types.

C has a few "core" data types called primitives these data types typically hold some sort of numerical value. The following are primitives:

int (integers)
long (integers)
float (real numbers)
double (real numbers)
char (characters)

The most used primitives are int, float, and char. Long and double are simply ints and floats respectively which allow a wider range of values.

Declaring a variable is easy. You first type the type of variable you want to declare, and then you give it an identifier (a name) which must start with a letter and may contain numbers and underscore characters as well. For example:

int x;

would declare an integer with the name x. You can now assign a value to x using the assignment operator =.

x=5;

This would store a value of 5 in x. It is also possible to combine declaration and assignment in one line.

int x=5;

is also acceptable. floats are declared in the same way.

float y=2.7;

however, attempting something like:

int z=3.14;

would cause an error, because 3.14 is not an integer.
Declaring characters is done in the same way, however, assignment is slightly different.

char c='f';

This would declare a character and hold the character 'f' in it.

Every character has its own unique code to identify it. Given the 256 standard characters, these codes are called ASCII codes and each ASCII character has its own. In C, when you store a character in a variable, you are actually storing the value of its ASCII code. When you want to specify that you're talking about a character, you must put it between apostrophes.

Next time, I'll talk about modifying the contents of variables and displaying their contents using printf();

Tuesday 15 March 2011

Back to basics

So I've been getting some comments and messages about my posts being too advanced, So I've decided that it would probably make more sense to start C from the beginning. In this post, I'll show you how to write a basic "Hello World!" program and explain the ins and outs of it. Some things will just be touched on right now and more fully explained in later posts.

#include<stdio.h>


int main()
{
    printf("Hello World!");
    return 0;
}

To fully understand how the program works, I'll explain its components.


#include<stdio.h>

#include is a macro. This particular macro goes and finds a file on your computer, in this case stdio.h. This file is what we call a header file. Basically, this file contains a bunch of function definitions (again, more on this later). The #include macro tells the program to go look in the folder on your computer containing your standard C library header files and to find stdio.h. When you hit the "build" button, the compiler will take your source code and compile it into machine code, but even before that, something called the preprocessor (we'll mention this again much later) will physically copy all the text from stdio.h into your program. Now when your program gets compiled, the compiler sees all this code in your program, so it actually becomes a part of your program.

int main()
{


}

Functions are very important in any programming language. Every program in C must have a main function. Everything contained within the curly brackets will be code executed during runtime.

printf("Hello World");

This is a call the function which prints to the screen. This printf function is defined within the stdio.h file. Having included that file, we are able to call this function. Here, we can see that the text we are printing to the screen is "Hello World".

return 0;

To indicate to the operating system that the program has terminated successfully (ie. No runtime errors occurred/The program did not crash/etc) we return 0 at the end of out main. This signifies the end of our program.

I hope this has been useful to those not yet fully comfortable with C.

Sunday 13 March 2011

Recursion

Probably one of the more controversial topics in terms of usefulness. Today, I'll show you how to write some simple recursive functions and in an upcoming post, I'll show you guys how it can be useful outside of mathematical applications.
So first off, what exactly is a recursive function. A recursive function is a function that keeps calling itself, until it reaches a base case. Once the base case is reached, then the function calls will return all the values to the previous function calls until the original call to the function returns its value.
This sounds like a lot, but I'll explain it using examples. First, let's look at the factorial function.
For example, we know that:

4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
etc.

Written as a recurrence relation, the factorial function looks like:

an = n*an-1
a1 = 1

So let's say we wanted to find the 3rd element in this relation, a3. It will be equal to 3 times a2

a3 = 3*a2

and a2 is equal to 2 times a1

a2 = 2*a1

and a1 is 1

a1 = 1    (This is the base case)

So like this we can write

a2 = 2*1
a3 = 3*(2*1) = 6

And we have our answer!
This is how a recursive function works. See how the function would keep "calling" itself until it reached a base case that was not a recursive call?
Now let's see how we can apply this as a function in C.

int factorial(int n)
{
    if(n==1 || n==0)
        return 1;
    else
        return n*factorial(n-1);
}

Notice how each time a recursive call is made, we first check to see if n is our base case, and if not, we do recursive calls. Of course, this code is buggy in that if we passed a negative number into the function, it would loop forever, but simple modifications can fix that.
So now that you've seen how it's done, you may be wondering how it works.
Every time a recursive call is made, the scope of the previous call is held in memory and "awaits" an answer from the call that it made. With the example earlier where we calculated 3!, the first call, makes a another call which then makes another call. The 3rd call will return the base case which then gets multiplied by 2 and returned to the first call. The original call then multiplies 2 by 3 and then returns the value to wherever the original call was made.

Later, I'll show you how to apply recursive functions to practical situations where math is not directly involved.

Friday 11 March 2011

Calculus

Being a computer science major, I need to take lots of different kinds of math courses. One of these classes is calculus. Now I'd like to say that I like calculus as much as I like discrete mathematics or linear algebra, but that's not the case. Gone are the days when we would take derivatives of polynomials and that was it. Now it's like "Oh what's that, you've just finally understood u-substitution? Great because now we're going to start integration by parts. Also, remember the Cartesian coordinate system? Yeah so we're going to plot things based on r and theta now, not x and y." FML. I love math, I just don't like this particular area of it. Saying this, I do have to ensure everyone that I do understand its importance and usefulness. Calculus is a very useful tool for optimization problems, especially for civil and mechanical engineers. I'm just not all that into it.
More discrete math please!

Thursday 10 March 2011

Variable length argument lists

I wrote my midterm for computer programming today and a good portion of it focused on variable length argument lists. What is that you may ask? Well take a look at a random function:

int square(int x)
{
    return x*x;
}
This function takes one argument - an integer which is multiplied by itself to give its square.
Now let's look at C's function printf() which is used to print text to the command prompt. We could simply tell it to print a string:

printf("Hello");

or we could use to print a value stored in a variable:

int x=6;
printf("The value of x is %d",x);

we can do as many variables as we want!

int x=3;
float y=6.3;
printf("%d %f",x,y);

Now if you're like me when I started out with C, you're probably thinking "How does the function know when to stop printing, and what types of variables to expect?" Well we're not going to write out the implementation of printf() as that takes numerous cross-referencing libraries, but we are going to do a function with a variable length argument list that finds the average of a unspecified number of numbers.
The required functions are stored in stdarg.h so we first need to include that:

#include <stdarg.h>

Now that we have the proper tools, let's look at how a variable argument list is created. There are 4 main "tools" we will be using:

1) va_list identifier
This declares a variable of type va_list. This is sort of like a pointer to your arguments.

2) va_start(identifier, first argument)
This tells the list where we're starting. The obvious location is the first argument. Note that you must have at least one defined parameter when dealing with variable length argument lists (more on this later)

3) va_arg(indentifiertype)
This is what we will be using to acquire the values form our arguments. Note that the identifier used here is the same one used to declare our va_list

4) va_end(identifier)
This is just to clean up nicely. It tells our program "Ok, we're done with this list of arguments, let's clean up now"

So now that we've defined the necessary tools, let's get to the implementation of the function:

float average(int n,...)
{
    float sum=0;
    va_list arg;
    va_start(arg,n);
    
    for(int i=0;i<n;i++)
    {
        sum+=va_arg(arg,float);
    }
    va_end(arg);
    return sum/n;
}

So let's go through it bit by bit:
When declaring a function with a variable length argument list, you MUST have at least one definitive argument. In this case, we are taking that argument to be the number of numbers we would like to average. As we all know, to average numbers, you take the sum of the numbers (float sum), and then divide it by the amount of numbers you have (int n).
So we start off by declaring our va_list and initializing the va_start(). Note that after suing va_start, the pointer now points at the 2nd argument (if available).
Next is our for loop which will loop n times to sum up all the integers. After doing that, we call va_end() to clean up. And then return sum/n, which will be our average!

A sample call of this function could be:

float x = average(3,6,4,5);

In which case, the value of x would be 5 since (4+5+6)/3 = (15/3) = 5

I hope this has been useful! I just thought I would share this piece of information with everyone!

Wednesday 9 March 2011

Using using malloc() to dynamically declare variables

If you're like me and used to using arrays to store primitive data types (or other data types for that matter), then you'll realize that there are some situations in which you do not initially know how big of an array you will need to store the data your user may enter. To get around this, we will dynamically declare data which will allow your program to effectively store an infinite number of variables (or until your computer runs out of memory).
To do this, we will be using a function called malloc()  (memory allocation)
The syntax for malloc is as follows:

malloc((type*)sizeof(type));

This may seem like a lot at first, but I'll break it down a bit starting from the right:
sizeof(type) will return the number of bytes which malloc will allocate to your program. For example, sizeof(char) will return 1 byte. Instead of using sizeof(), you could always just enter a number, but that's really bad practice, so I don't recommend it.
When you run malloc, it goes and asks the operating system "Can I get this much memory for my program?" and in general, it will return unspecified memory. This means that we have to cast it using (type*) because we know what this memory is being used for, but malloc() does not!
So now that I've explained the basics of malloc(), I'll give some examples:


int *p;
p = malloc((int*)sizeof(int));


See, we are starting off with a pointer to an integer and we are setting it equal to the memory allocated by malloc(). Useful isn't it?
This is great, but there are even more applications. Take a look at this code:


int *p;
p = malloc(int*)10*sizeof(int));


Have you caught on? Basically, malloc() went and asked the operating system "Can I get 10*sizeof(int) bytes to use?". We have gotten to enough memory for 10 integers which are all right beside each other in memory. What does this mean? We have dynamically created an array of integers. The multiplier can also be a variable. For example:


int x=42;
int *p;

p = malloc(int*)x*sizeof(int));

There we go! An array of 42 integers. This can also be applied so that the user is asked to enter the size of the array and then an array of that size will be created.

I hope this first lesson has been informative! Keep on coding and be creative!