Category: Programming Tips


Prime numbers

Hi there, today we’re gonna talk about “Prime Numbers”.

First, What is a prime number?

An informal definition is any integer greater o equal to 2 that is divisible just by 2 numbers, one and the number itself.

View full article »

Class #2 (11 – Dec – 2010)

UPDATE: Sorry i forgot to attach the pptx :D , but now is here.

Well in this class we will talk about RECURSION.

The recursion is soo important cause you will use it in a lot of algorithms from searching and sorting to traveling a tree and much more, this is the way you can make your programs a little more smarter than usual.

ACM TRAINING COURSE 2

Art of Programming Contest – eBook

This is a great book to start on your way to be a competitive programmer, in this book you will find tips to solve some of the common problems that you will find at http://uva.onlinejudge.org/. Also is a great study guide for the ACM contest.

this is the content of this book:

Chapter 1 Fundamental

Concepts

Chapter 2 Game Plan For a Contest

Chapter 3 Programming In C: a Tutorial

Chapter 4 Essential Data Structures for Contest

Chapter 5 Input/Output Techniques

View full article »

Class #1 (20-Nov-2010)

The topics of this class are:

  1. Data Types
  2. Inputs
  3. Outputs

Here you can find the must used I/O functions and how they work (parameters they receive, return values, flags, etc… ), you will see that depending on the problem your facing, you need to read the input in a different way and the same for the output. If you know the difference between the functions you can choose witch one to use for that case.

You can see the presentation here: ACM TRAINING COURSE #1

Getting Started

http://uva.onlinejudge.org/ is where you can see problems, solve them and submit them.

This is the main page

The first thing you most do is create your own account. Click in the “Register” link as in the image above.

Then you most fill out the fields.

Example

View full article »

Sorting and searching

Sorting:

In many problems you’ll need to sort, an array of any type. Fortunately for you the the C standard library has a built-in function named qsort. As you can guess, this function is an implementation of the algorithm quick sort. The function take 4 parameters.

  1. The array
  2. Number of items to sort
  3. The size of each item in bytes
  4. The comparation function in the follow format int function(constant void *a, constant void *b)

The function doesn’t return anything.

This is the prototype:

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

Here an little example of how to use the function qsort:

View full article »

Receiving the input

This is the most critical part of a program, if you cant receive the input rigth the you are dead.

There are a lot of ways the can send you the input, every problem has his answer, now im going to solve some of them:

  • When the number of parameters and the type is always the same then you just have to grab it with scanf,cin,getchar,etc…
  1. scanf(“%d %c %f “,&A, &B, &c, &D);
  2. cin >> A >> B >> C;
  • When the number of parameters is unkown but they are from the same type always you can do something like this
  1. while(scanf(“%d”, &A[x++]) != EOF);
  2. while((c = getchar()) != EOF);
  3. for(x = 0, scanf(“%d”, &N) ; x < N ; scanf(“%d”, A[x++]));
  • When you need to receive NUMBERS and CHARACTERS
  1. scanf(“%d\n”, &N);  /* If you dont remove or get the ‘\n’ from the stream then if you use gets() or scanf(“%c”, &c) or something similar it would take the ‘\n’ as the input, you need to be very careful with this all your algorithm can be rigth but if you dont catch this then it would be ALL WRONG.

Freopen with Debugger (GDB)

Most of the time when you are programming and then output is not the one that you expected and you are trying to find your mistake, one helpful way to do this is to use freopen in your main function to use a file like keyboard input and this makes the debugging work easier, then you just have to watch your variables and try to find the error.

**Example: freopen(“input.in”, “r”, stdin);

**Tip: You can also use freopen(“outpu.out”, “w”, stdout) to send the output to a file.

Follow

Get every new post delivered to your Inbox.