for循环中的数组有问题

时间:2015-03-29 23:53:36

标签: c++ arrays for-loop

我希望我的程序生成Fibonacci序列,并由用户进行一些调整。

输入他们想要生成的数字后,用户将输入他们想要输入的修饰符的数量。

我知道这意味着需要创建一个维度n的数组,其中n是用户想要输入的修饰符的数量。 (即10-20之间的数字乘以2,21-30之间的数字将乘以3,等等。)

接下来,用户将输入他们想要调整的间隔。 (即斐波那契数字10-20,21-30,31-40之间等)

当我运行间隔检查循环时出现问题。这对我来说不起作用:

for (intervalarray[n][0] < n < intervalarray[n][1]) { /* ... */ }

所以我假设我需要一个指针到数组。但我没有找到有关此问题的任何有用的教程。

我是C ++的新手,所以请耐心等待,如果你不能直接回答我的问题,请指点我学习的方向,以帮助我找到解决方案。

#include <iostream>
#include <stdlib.h>

using namespace std;

int newnumb, thisnumb, lastnumb, runtotal, yourup;
int i, n;
float yourbet;

int s, t;

int getnumbers() {
    cout << "how many numbers would you like to generate?";
    cin >> n;
}

int main() {
    /* Ask the user how many numbers of the Fib Sequence to generate,
       store the answer in the variable 'n' */

    getnumbers();

    cout << "How many modifiers are there? \n";
    cin >> s;

    float justtesting[s][3];

    /* This loop is going to use a variable j to span the array by rows,
       (i.e. j < the number of rows), storing the values into the array of dim(s) */

    cout << "When does the first modified interval begin?" << endl;
    cin >> justtesting[0][0];

    cout << "When does that modified interval end?" << endl;
    cin >> justtesting[0][1];

    for (int j = 1; j < s; j++) {
        cout << "When does the next modified interval begin?" << endl;
        cin >> justtesting[j][0];

        cout << "When does that modified interval end?" << endl;
        cin >> justtesting[j][1];
    }

    /* This loop is going to take the modifier divisor values and store them in
       the third dimensional slot of each row of the array */

    cout << endl;
    cout << "What is the first divisor?" << endl;
    cin >> justtesting[0][2];

    for (int l = 1; l < s; l++) {
        cout << "What is the next divisor?" << endl;
        cin >> justtesting[l][2];
    }

    cout << endl;

    /* Prints out user input values for the intervals to be modified */

    for (int row = 0; row < s; row++) {
        for (int column = 0; column < 2; column++) {
            cout << justtesting[row][column] << " ";
        }
        cout << endl;
    }

    cout << "  " << endl;

    /* Prints out the divisors that were stored in the 3rd dimension of
       the user generated array */

    for (int row = 0; row < s; row++) {
        cout << justtesting[row][2] << " ";
    }

    // Loop for fib sequence

    thisnumb = 1;
    lastnumb = 0;
    runtotal = 0;
    yourup   = 0;

    for (i = 0; i < n ; i++) {
        newnumb = thisnumb + lastnumb;
        yourup = 2 * lastnumb - runtotal;

        thisnumb = lastnumb;
        lastnumb = newnumb;
        runtotal = (thisnumb + lastnumb) - 1;

        cout << "The count: " << i ;
        cout << " Fibornacci Number: " << thisnumb;

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找如何实施所谓的“粗糙阵列”#34;&#34;或&#34;动态数组&#34;对于未知(对程序)的整数,您将要相互比较数字。如果您让用户输入它们。

您似乎需要了解如何使用newdelete关键字。