Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的图示器要打印函数的多个实例?C++_C++_Function_Graph_Equations - Fatal编程技术网

为什么我的图示器要打印函数的多个实例?C++

为什么我的图示器要打印函数的多个实例?C++,c++,function,graph,equations,C++,Function,Graph,Equations,我有一个程序,可以在我定义的长度和宽度的板上绘制简单的参数方程。它可以很好地编译函数,但可以在图形的不同位置打印函数的多个实例。如果有人能帮我弄清楚为什么我会得到这个输出,我将不胜感激。我在代码中加入了注释,以帮助理解正在发生的事情。 我没有足够的声誉来发布输出的图片,但如果您编译并执行它,您将看到我所说的内容 #include <iostream> #include <cstdlib> #include <unistd.h> #include <tim

我有一个程序,可以在我定义的长度和宽度的板上绘制简单的参数方程。它可以很好地编译函数,但可以在图形的不同位置打印函数的多个实例。如果有人能帮我弄清楚为什么我会得到这个输出,我将不胜感激。我在代码中加入了注释,以帮助理解正在发生的事情。 我没有足够的声誉来发布输出的图片,但如果您编译并执行它,您将看到我所说的内容

#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
using namespace std;

#define N 25
#define M 60

/*
  This function prints the board each time it is called and places an *
  in the place corresponding to the value of the function.
*/
void print_board(char p[M][N]) {
    int i, j;
    for (i=0; i<=N; i++) {
        for (j=0; j<=M; j++)
                 if (i==0)      cout << '=';
            else if (j==0)      cout << '|';
            else if (i==N)      cout << '=';
            else if (j==M)      cout << '|';
            else if (p[i][j]== '*') cout << '*';
            else            cout << ' ';
        cout << endl;
    }
}
/*
  These functions accepts an integer for time and computes a value for x and y
  for the parametirc equations given and returns each.
*/
int fx(int t) {

    int x = t;

    return x;
}

int fy(int t) {

    //int y = 5 * sin(0.2 * t) + 15;
    int y = (pow(t,2)/60) - t + 25;

    return y;
}

/*
  This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[M][N], int t) {
    int i, j;

    //copies the old board
    int q[M][N];
    for (i=0; i<=N; i++) {
        for (j=0; j<=M; j++) {
            q[i][j] = p[i][j];
        }
    }

    //creates the new board
    int x, y;
    for (i=0; i<=N; i++) {
        for (j=0; j<=M; j++) {
            x = fx(t);
            y = fy(t);

            if (i == y && j == x) {
                p[i][j] = '*';  //stores an * for the values of x and y
            }
        }
    }

}

int main() {

    char p[M][N];

    print_board(p);

    int t = 0;
    while(t <= M) {
        cout << string(80, '\n');

        next_board(p , t);
        print_board(p);
        usleep(20000);

        t++;
    }

    return 0;
}

请帮助并感谢所有尝试的人

在您的程序中

char p[M][N]
换成

char p[N][M]
你应该得到你期望的结果,你的程序中的混合轴

如果你愿意,这是全部工作代码

#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
#include <string>
using namespace std;

#define N 25
#define M 60

/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[N][M]) {
    int i, j;
    for (i = 0; i <= N; i++) {
        for (j = 0; j <= M; j++)
            if (i == 0)      cout << '=';
            else if (j == 0)      cout << '|';
            else if (i == N)      cout << '=';
            else if (j == M)      cout << '|';
            else if (p[i][j] == '*') cout << '*';
            else            cout << ' ';
            cout << endl;
    }
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {

    int x = t;

    return x;
}

int fy(int t) {

    //int y = 5 * sin(0.2 * t) + 15;
    int y = (pow(t, 2) / 60) - t + 25;

    return y;
}

/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[N][M], int t) {
    int i, j;

    //copies the old board
    int q[M][N];
    for (i = 0; i <= N; i++) {
        for (j = 0; j <= M; j++) {
            q[i][j] = p[i][j];
        }
    }

    //creates the new board
    int x, y;

    for (i = 0; i <= N; i++) {

        for (j = 0; j <= M; j++) {
            x = fx(t);
            y = fy(t);

            if (i == y && j == x) {
                p[i][j] = '*';  //stores an * for the values of x and y
            }
        }
    }

}

int main() {

    char p[N][M];

    print_board(p);

    int t = 0;
    while (t <= M) {
        cout << string(80, '\n');

        next_board(p, t);
        print_board(p);
        usleep(20000);

        t++;
    }

    return 0;
}