简体   繁体   English

这段代码有什么问题?。 几分钟前我刚刚学习了编程,我正在努力做到这一点

[英]What's wrong with this code?. I just learned programming a few minutes ago and I'm trying to make this

I just learned programming a few minutes ago and I'm trying to make this几分钟前我刚刚学习了编程,我正在努力做到这一点

#include <iostream>
int main(){
    int n, fibo_n, fibo_n1=1, fibo_n2=0
    cout<<"Enter the max term of the Fibonacci Sequence: "

    for (int i =1; i<n; i++){
        fibo_n=fibo_n1+fibo_n2
        fibo_n2=fibo_n1
        fibo_n1=fibo_n
        cout<<fibo_n<<" "
    }
    cout<<endl
    cin.get()
    return 0
}

There are two problems with your code one is you are printing addresses of fib0, fib1 instead of their value and the second is you are using only one format specifier while printing two values.您的代码有两个问题,一是打印 fib0、fib1 的地址而不是它们的值,二是在打印两个值时仅使用一个格式说明符。

Here is the modified code.这是修改后的代码。

#include <stdio.h>

void Fibonaci(int N);

void main(){
    int N;
    long hasil;
    printf("Enter the number of elements : ");
    scanf("%d", &N);
    Fibonaci(N);
 }
void Fibonaci(int N){
    int fib0=0, fib1=1, fib;
    printf("%d\n%d\n", fib0, fib1); //modified
    while(fib0<=N/2){
        fib=fib0+fib1;
        fib0=fib1;
        fib1=fib;
        printf("\n%d", fib1); //modified
    }
}
#include <stdio.h>

void Fibonaci(int N);

void main(){
    int N;
    long hasil;
    printf("Enter the number of elements : ");
    scanf("%d", &N);
    Fibonaci(N);
}
void Fibonaci(int N){
    int fib0=0, fib1=1, fib;
    printf("%d, %d\n", fib0, fib1);
    while(fib0<=N/2){
        fib=fib0+fib1;
        fib0=fib1;
        fib1=fib;
        printf("\n%d", fib1);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试在 Vscode 上执行此代码,但每当我运行此代码时,它不会获取字符的值,而只是显示一个随机数 - I'm trying to execute this code on Vscode but whenever I'm running this it's not taking the value of character but just showing a random number 从C#切换到C ++。 我的代码出了什么问题? 我需要标题我正在尝试做什么? 一个文件问题中的类定义 - Switching from C# to C++. What's wrong with my code? do I NEED headers for what I'm trying to do? Class definitions within one file issue 存在内存访问异常,但我不确定我的代码有什么问题 - There's a memory access exception but I'm not sure what's wrong in my code 我在大学学习的C ++代码的说明 - Explanation of this C++ code I learned in college 代码输出随机符号,我不确定出了什么问题 - Code outputs random symbols, i'm unsure what is wrong 我正在尝试建立虚拟商店(我刚刚开始学习c ++) - I'm trying to make a virtual store (I just started learning c++) 我刚刚了解了 C++ 中的动态 memory 分配 - I just learned about dynamic memory allocation in C++ 我正在尝试在 c++ 中创建一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么? - I am trying to make a logging framework in c++ but information is not being passed to the logger's subcomponents, what am i doing wrong? 我正在尝试将s1内的所有字符更改为“ x”。 但是,当我运行代码时,编译器仅打印了11次“ hello world” - I'm trying to change all character within s1 to 'x'. When I run the code, however, the compiler just printed out 'hello world' 11 times 我正在尝试用getline填充向量数组,这是怎么回事? - I am trying to fill vector array with getline, what's wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM