0% found this document useful (0 votes)
85 views2 pages

Exercises: 1. Fibonacci Numbers

The document contains 3 exercises on programming with objects in C: 1. Write a function to print the Fibonacci sequence up to a given number N. 2. Write functions to analyze an array of integers by counting positives, negatives, zeros and determining if the sequence is increasing, decreasing or neither. 3. Write a program to visualize and print different patterns like a full square, empty square and triangle of a given size N using asterisks.

Uploaded by

bgment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views2 pages

Exercises: 1. Fibonacci Numbers

The document contains 3 exercises on programming with objects in C: 1. Write a function to print the Fibonacci sequence up to a given number N. 2. Write functions to analyze an array of integers by counting positives, negatives, zeros and determining if the sequence is increasing, decreasing or neither. 3. Write a program to visualize and print different patterns like a full square, empty square and triangle of a given size N using asterisks.

Uploaded by

bgment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programmazione ad oggetti

Exercises
1. Fibonacci numbers
Write a function that prints the first N numbers of Fibonacci sequence (where each number is the sum f the two
previous numbers: 1,1,2,3,5,8,13,21...), with N defined at the beginning of main().
2. Numbers
Given an array of N integer numbers let calculate different results:
1. Counting how many positive, negative numbers are in the array, and how many 0 values;
2. Print if sequence is increasing, decreasing or neither increasing nor decreasing.
Note: a sequence of numbers is increasing iff for each couple of consecutive numbers X[i] and X[i+1] the following
condition holds: X[i] < X[i+1] .
3. Figures
Write a program that given a number 'n' defined in method main() :
1. - visualizes a full square of asterisks large 'n' chars (see case 1 with n = 5)
2. - visualizes an empty square (see case 2 with n = 5)
3. - visualzies a triangle (see case 3 with n = 5)
Case 1

Case 2

Case 3

*****
*****
*****
*****
*****

*****
*
*
*
*
*
*
*****

*
**
***
****
*****

file:///E|/4.Teaching/AdvancedProgramming/work/2009/Week1-code/Ex1.htm[2013/9/11 20:06:25]

#include <stdio.h>
#include <stdlib.h>
/* fibonacci with iteration */
void fibonacci(int n) {
int p1=0, p2=1, p=1, i;
for (i=1; i<=n; i++) {
printf("%d ", p);
p = p1+p2;
p1 = p2; p2=p;
}
}
/* fibonacci with recursion */
int cont= 0, N = 10;
void fibonacci_rec(int a, int b) {
if (cont<N) {
printf("%d ", b);
cont++;
fibonacci_rec(b, a+b);
}
}
/* recursive function which returns
the n-th element of Fibonacci sequence */
long fib(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
// stopping cases
// recursive case
return fib(n - 1) + fib(n - 2);
}
int main(int argc, char *argv[])
{
/* fibonacci with iteration */
printf("fibonacci with iteration:\n");
fibonacci(15);
printf("\n\n");
/* fibonacci with recursion */
if (N>0) printf("1 ");
printf("fibonacci with recursion:\n");
fibonacci_rec(1, 1);
printf("\n");
printf("\n %d\n", fib(2));
system("PAUSE");
return 0;
}

You might also like