Shlrm.org Blog

Linux, Java, Ruby, and Politics

(re)Learning ANSI C: Problem 2

| Comments

I’m noticing that these problems aren’t as much about learning the language as I’d have thought. They’re fairly simple tasks.

Anyways, here’s the second problem’s solution:

Second Problem’s Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * File:   main.c
 * Author: david.kowis
 *
 * Created on August 4, 2009, 7:48 AM
 */

#include <stdio.h>
#include <stdlib.h>

/*
 *
 */
int main(int argc, char** argv) {

    int last = 1, secondLast = 0, current = 0;
    int max = 0, min = 0;
    int x;

    int numbers[10];

    //Fibbonaci sequence
    printf("0, 1, ");
    for (x = 1; x < 100; x++) {
        current = last + secondLast;
        printf("%u, ", current);
        secondLast = last;
        last = current;
    }
    printf("\n");

    srand(time(NULL));
    //initialize random list
    printf("Random numbers to find max and min of\n");
    for (x = 0; x < 10; x++) {
        numbers[x] = rand() % 1000;
        printf("%u, ", numbers[x]);
    }
    printf("\n");

    max = numbers[0];
    min = numbers[0];
    printf("Finding the max and min...\n");

    for (x = 0; x < 10; x++) {
        if (min > numbers[x]) {
            min = numbers[x];
        }
        if (max < numbers[x]) {
            max = numbers[x];
        }
    }

    printf("Maximum: %u  Minimum %u\n\n", max, min);

    return (EXIT_SUCCESS);
}

Comments