Program 1: How to generate fibonacci number

#include #include long fibonacci( unsigned int ); void determine( int ); int main(void) { unsigned int n; printf("\nEnter a small positive integer.\n"); scanf("%2d", &n); determine( n ); return EXIT_SUCCESS; } void determine( int n ) { if ( n > 30) printf("\nThat integer is probably too large.\n"); else if (n == 0) printf("\nThe zero'th Fibonacci number is not defined.\n"); else printf("\nThe %ud'th Fibonacci number is %ld.\n", n , fibonacci(n) ); } long fibonacci( unsigned int n ) { if ( n <= 2 ) return 1; else return fibonacci(n-1) + fibonacci(n-2); } When you enter a number 23, you will find that it displays 28657.

Fibonacci number is dounf out by an Italian mathematican in 12 century. The number is determined as follows

1 1 2 3 5 8 13 …. It is to add two previous numbers togther to produce the next number. For example, 8 = 5 + 3. 13 = 8 + 5. The initial number is set to 1 and 1.

Program 2 This is another method. F[1] and f[2] are initially set to one.

#include #include long fibonacci( unsigned int ); int main(void) { unsigned int n; printf("\nEnter a small positive integer less than 30.\n"); scanf("%2d", &n); printf("\nThat integer is %d.\n", fibonacci(n)); return 0; } long fibonacci( unsigned int n ) { int f[30]; f[1] = f[2] = 1; for (unsigned int i = 3; i <= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; } The output is:

Program 3: How to generate Lucas number The lucas number starts with 1, 3, 4, 7, 11, 18… The initial values are 1 and 3. Following is the program to generate this number.

#include #include long lucas( unsigned int ); int main(void) { unsigned int n; printf("\nEnter a small positive integer less than 30.\n"); scanf("%2d", &n); printf("\nThat integer is %d.\n", lucas(n)); return 0; }

long lucas( unsigned int n ) { int f[30]; f[1] = 1; f[2] = 3; for (unsigned int i = 3; i <= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; }

The output for the same number is dfferent.

Up to here, can you write a program that determines the difference between the fibonacci number and lucas number given the number. For example, the difference for the first number is 1 – 1 = 0, while the third number is 4 – 2 = 2.

Program 4: how to control the input number Given the following number and note that scanf("%d", &n); has been changed to %d. Now enter a number such as 12, you will get the result of 322.

#include #include long lucas( unsigned int ); int main(void) { unsigned int n; printf("\nEnter a positive integer.\n"); scanf("%d", &n); printf("\nThat integer is %d.\n", lucas(n)); return 0; }

long lucas( unsigned int n ) { int f[30]; f[1] = 1; f[2] = 3; for (unsigned int i = 3; i <= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; }

Now enter the value of 123, you will find that.

If you changed scanf("%2d", &n); with %2d, you will get the result Now you can experience how it affects the data entry.

Program 5: return an address instead of a value

#include #include long *lucas( unsigned int ); int main(void) { unsigned int n; printf("\nEnter a positive integer.\n"); scanf("%2d", &n); printf("\nThat integer is %d.\n", *(long *)lucas(n)); return 0; }

long *lucas( unsigned int n ) { long result; long f[30]; f[1] = 1; f[2] = 3; for (unsigned int i = 3; i <= n; i++) f[i] = f[i-1] + f[i-2]; result = f[n]; return &result; }

The result is:

You can see, it will return an address in instead of a value. This is not encouraged but if you handle it properly, it still works.

Program 6: to show how to get the current memory size.

#include #include long lucas( unsigned int ); int main(void) { unsigned int n; printf("\nEnter a positive integer.\n"); scanf("%2d", &n); printf("\nThat integer is %d.\n", lucas(n)); return 0; } long lucas( unsigned int n ) { long *array = (long *)malloc(n); long result; long f[30]; f[1] = 1; *(long *)array = 1L; f[2] = 3; *(long *)(array + 1) = 1L; for (unsigned int i = 3; i <= n; i++) { f[i] = f[i-1] + f[i-2]; *(long *) (array + i) = f[i]; } result = f[n]; for (i = 0; i

The output should be 1, 3, 4, 7, 11 instead of 1, 2, 3, 4, 5:

Can you find out the reasons why it fails? (hint: two points)

Program 6; gets()

This program will read the data entered from the keyboard to be assigned to a and then prints it out using printf.

#include #include void main() { char a[13] = "How are you?"; char b[5] = "Good"; gets(a); //adds a string terminating character to the end of characters it reads printf("The string you enter is %s\n",a); }

If you enter “This is city”, it will print it out correctly.

However, if you enter a longer value say 1234567890 abcdefghijk, the program will crash and will display an error. It is because, you have over-written the memory location. Can you point out what is the allocated size for this variable, a in the program? You should use the debugger to debug the program and try to understand why it crashes. Remember, each string is terminated by a null character.

Calloc and Realloc

The information is extracted from http://www.cm.cf.ac.uk/Dave/C/node11.html#SECTION001120000000000000000 For teaching purpose.

There are two additional memory allocation functions, Calloc() and Realloc(). Their prototypes are given below:

void *calloc(size_t num_elements, size_t element_size};

void *realloc( void *ptr, size_t new_size);

Malloc does not initialise memory (to zero) in any way. If you wish to initialise memory then use calloc. Calloc there is slightly more computationally expensive but, occasionally, more convenient than malloc. Also note the different syntax between calloc and malloc in that calloc takes the number of desired elements, num_elements, and element_size, element_size, as two individual arguments.

Thus to assign 100 integer elements that are all initially zero you would do:

int *ip; ip = (int *) calloc(100, sizeof(int));

Realloc is a function which attempts to change the size of a previous allocated block of memory. The new size can be larger or smaller. If the block is made larger then the old contents remain unchanged and memory is added to the end of the block. If the size is made smaller then the remaining contents are unchanged.

If the original block size cannot be resized then realloc will attempt to assign a new block of memory and will copy the old block contents. Note a new pointer (of different value) will consequently be returned. You must use this new value. If new memory cannot be reallocated then realloc returns NULL.

Thus to change the size of memory allocated to the *ip pointer above to an array block of 50 integers instead of 100, simply do:

ip = (int *) calloc( ip, 50);

Program to show how to use malloc(), realloc()

#include #include #include #include #include void main() { unsigned int n, m; char a[5] = "This"; char *b; printf("Enter the number of strings\n"); scanf("%d", &n); b = (char *)malloc(n +4);

for (unsigned int i = 0; i < 4; i++) *(char *)(b + i) = a[i]; getchar(); //clear the buffer printf("Enter string\n"); for (i = 4; i

// use realloc() to reallocate the size of buffer, the format is (void *)realloc(void pointer, size)

printf("Expand the buffer\n");

printf("Enter the number of strings\n"); scanf("%d", &m); b = (char *)realloc(b, n*sizeof(m));

for (i = 4 + n; i <4 + n + m; i++) { *(char *)(b + i) = getchar(); //getchar() is to get a character from the keyboard } *(char *)(b + i) = 0x00; printf("the string is %s\n", b);

}

The output is: