提问者:小点点

我想加两个不同的矩阵,然后告诉你哪一个是最大的


嗨,我正在写一个程序,生成两个矩阵,然后告诉哪一个是大的。但我的问题是我不知道怎么加。程序说i和j不是在output3函数中声明的,而是在其他函数中声明的。我想我可能在格式化方面有问题,但我是新来的,所以我不太知道如何正确地编写代码。我该怎么解决这个?多谢了。

#include<cstdio>
#include<cstdlib>
#include <ctime>
#define maxM 4
#define maxN 4
#define maxX 4
#define maxY 4
int tabel1[maxM][maxN];
int tabel2[maxX][maxY];
int sum[maxM][maxN];
int n, m, x, y;
void input1(){
n=4;
m=4;
for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            tabel1[i][j] = rand() % 100;
        }
    }
}

void input2(){
    x = 4;
    y = 4;
    for ( int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            tabel2[i][j] = rand() % 100;
        }
    }
}
void output1(){
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++){
            printf("%8d", tabel1[i][j]);
        }
        for (int j = 0; j < n; j++){
        printf("\n");
    }
    }
}
void output2(){
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++){
            printf("%8d", tabel2[i][j]);
        }
        for (int j = 0; j < n; j++){
        printf("\n");
    }
    }
}
void output3(){
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            sum[i][j] = tabel1[i][j] + tabel2[i][j];
        }
        printf("%8d", sum[i][j]);
    }
}
main(){
    srand((unsigned)time(0));
    printf("First:\n");
    input1();
    output1();
    printf("Second:\n");
    input2();
    output2();
    print("Added up:\n");
    output3();
}

共1个答案

匿名用户

我猜你对C++有点陌生,因为你用了很多C的特性。我改进了您的代码,使它更像C++,并修正了错误。

#include<iostream> //iostream is the c++ equivalent for getting characters from input and putting output.
//#include<cstdlib>
#include <ctime>
#define maxM 4 //next time, maybe don't use macros in c++. I would recommend using "const int MaxM = 4;" or something like that.
#define maxN 4
#define maxX 4
#define maxY 4
int tabel1[maxM][maxN];
int tabel2[maxX][maxY];
int sum[maxM][maxN];
int n, m, x, y;

using namespace std; 

void input1() {
    n = 4;
    m = 4;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            tabel1[i][j] = rand() % 100;
        }
    }
}

void input2() {
    x = 4;
    y = 4;
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            tabel2[i][j] = rand() % 100;
        }
    }
}
void output1() {
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++) {
            cout<< tabel1[i][j]; // "<<" means put into cout, and cout is Console OUTput 
        }
        for (int j = 0; j < n; j++) {
            cout << endl; //or \n
        }
    }
}
void output2() {
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++) {
            cout<<tabel2[i][j]; //again, use cout, not printf()
        }
        for (int j = 0; j < n; j++) {
            cout << endl; //or /n
        }
    }
}
void output3() {
    int temp;
    for (int i = 0; i < m; i++) { //you forgot to define i and j, remember to put int before them
        for (int j = 0; j < n; j++) {
            sum[i][j] = tabel1[i][j] + tabel2[i][j];
            temp = j;
        }
        // original code here: cout<<sum[i][j]; j was outside the loop, so it was out of scope
        cout << sum[i][temp]; //this is new code
    }
}
int main() { //you forgot to put int main, and only had main. C++ does not allow this.
    srand((unsigned)time(0));
    cout << "First:\n";
    input1();
    output1();
    cout << "Second:\n";
    input2();
    output2();
    cout<<"Added up:\n";
    output3();
    return 0;
}

在两个for循环中,您只将i=0;...,因此编译器不知道ij是什么类型。另外,在另一个循环中,sum[i][j]j的for循环之外,因此编译器不知道j是什么。我通过创建变量temp来解决这个问题,将j的值存储在temp中,并执行sum[I][temp]。另外,在C++中,我们使用cout而不是printf()(在iostreams上读取)。最后,在main()中,您忘记将int作为main()的返回类型(这在C++中是必需的)。

此外,在C++中,我将避免使用宏,而尝试执行类似const int的操作。如果你必须使用宏,至少让它们都是大写的。

你的代码现在起作用了!