斐波那契序列

时间:2011-02-10 16:43:11

标签: c fibonacci

#define MAX_SEQUENCE 10 // Max values to store in shared memory
#define MIN_SEQUENCE 2 // Min value the user can enter

//shared memory:
// 1) holds an array of numbers
// 2) holds how many numbers are in the array
typedef struct {
    int fib_seq[MAX_SEQUENCE];
    int sequence_size;
} shared_data;

//MAIN function
int main(int argc, char *argv[]) {

    pid_t pid; //process ID
    int segment_id; //Shared Memory ID
    shared_data *mem; //Shared Memory Pointer

    //check to validate atleast two arguments
    if(argc != 2) {
        printf("USAGE ERROR: [0-9]\n");
        exit(0);
    }

    //validate the input is not larger then the MAX
    if(atoi(argv[1]) > MAX_SEQUENCE) {
        printf("Max Input Size: %d\n", MAX_SEQUENCE);
        exit(0);
    }

    //validate the input is not smaller then the MIN
    if(atoi(argv[1]) < MIN_SEQUENCE) {
        printf("Min Input Size: %d\n", MIN_SEQUENCE);
        exit(0);
    }

    // 1) create a new shared memory location 'IPC_PRIVATE'
    // 2) the size of our shared memory structure 'sizeof(shared_data)'
    // 3) Set Modes S_IRUSR and S_IWUSR so the owner can read and write to the shared memory 'S_IRUSR|S_IWUSR'
    segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR|S_IWUSR);

    //attach the shared memory and get the pointer to the beginning location in memory
    mem = (shared_data *) shmat(segment_id,NULL,0);

    //set the size of the sequence to the argument that was passed in via command line
    mem->sequence_size = atoi(argv[1]);

    // fork a child process
    pid = fork();

    if(pid < 0) { /* error occured */
        fprintf(stderr, "Fork Failed\n");
        return 1;
    }
    else if(pid == 0) { /* child process */
        int counter = 0;
        printf("Child Fibonacci Sequence: ");

        while(counter < mem->sequence_size) {
            if(counter == 0){
                //FIB of zero is always zero
                mem->fib_seq[counter] = 0;
            }
            else if(counter == 1){
                //FIB of one is always one
                mem->fib_seq[counter] = 1;
            }
            else {
                //The Fibonacci Sequence formula 'R = fib(n-1) + fib(n-2)'
                //The first two numbers in the sequence are always 0 and 1.
                //To get a value in the sequence you will want to take the previous
                //two numbers and add them together. For example:
                // b + a = c
                // [fib(d-1) = c] + [fib(d-2) = b] = R
                // fib(0) = 0
                // fib(1) = 1
                // fib(2): 1 + 0 = 1
                // fib(3): 1 + 1 = 2
                // fib(4): 2 + 1 = 3
                // fib(5): 3 + 2 = 5
                // The next Fibonacci number in the sequence will be '8'
                mem->fib_seq[counter] = mem->fib_seq[counter - 1] + mem->fib_seq[counter - 2];
            }
            printf("%d ", mem->fib_seq[(counter)]);
            counter++;
        }
    }
    else { /* parent process */

        /* parent will wait for the child process to complete */
        wait(NULL);

        //Print out shared memory
        int count = 0;
        printf("\nParent Fibonacci Sequence: ");
        while(count < mem->sequence_size){
            printf("%d ", mem->fib_seq[count]);
            count++;
        }

        //detach shared memory
        shmdt(mem);
        //remove shared memory segment
        shmctl(segment_id,IPC_RMID,NULL);
        printf("\nComplete\n");
    }

    return 0;
}

好的,我有这个程序,我已经工作了一段时间了,问题是数字序列是1,我似乎无法找到它的位置。它不会为fib(0)打印0。所以,当我做Fib(2)它给我0 1而不是0 1 1,任何人都有任何建议吗?

4 个答案:

答案 0 :(得分:1)

经典的欧比万错误(一个接一个)。你需要这样做:

mem->sequence_size = atoi(argv[1]) + 1;

(编辑,之前的帖子导致越界数组访问)

答案 1 :(得分:1)

您的代码似乎有用......

如果你想让Fib(2)打印出三个数字,你可能想看看这一行:

while(counter < mem->sequence_size) {

如果你这样做,你需要注意你需要11块内存来计算Fib(10)。你现在只给自己10块记忆。

如果这没有意义,请将MIN_SEQUENCE设置为0,并在计算Fib(0)时问问自己代码的作用。

答案 2 :(得分:1)

根据您的代码,序列大小设置为您传递的参数

mem->sequence_size = atoi(argv[1]);

在while循环中,您正确检查:

while(counter < mem->sequence_size)

所以Fib 2应该只从斐波纳契序列打印2个元素0 1。如果您需要0 1 1的输出,那么您是不是将该程序作为fib 3运行?

答案 3 :(得分:0)

您正在打印counter个项目。即,当counter == 2时,您打印两个数字,当counter == 0时,您打印的是零数字。