简体   繁体   English

在 c++ 中将数组打印为表格

[英]printing an array as a table in c++

So, I am trying to print an array Fi[rows][columns] in a table format of 11 rows and 11 columns.因此,我试图以 11 行和 11 列的表格格式打印数组Fi[rows][columns] The code I am using is:我正在使用的代码是:

for (int i = 0; i < rows ; i++){
    for (int j=0; j< columns; j++)
        std::cout << Fi[i][j]<<"\t";
    std::cout << "\n";
}

And my problem is, that the element printed on Fi[0][10] is not the one that it's supposed to be.我的问题是,打印在 Fi[0][10] 上的元素不是它应该是的元素。 Actually, if I simply print out实际上,如果我只是打印出来

std::cout<< Fi[0][10];

I get the correct value.我得到正确的值。 Can someone help me to figure out what I am doing wrong?有人可以帮我弄清楚我做错了什么吗?

Are you looking for something like this?你在寻找这样的东西吗? I changed the part std::cout << "\n";我改变了部分std::cout << "\n"; to std::cout << std::endl;std::cout << std::endl;

#include <array>
int main() {
    int rows = 4;
    int columns = 5;

    int my_array[rows][columns] = {{0,1,2,3,4},{5,6,7,8,9},{10,11,12,13,13},{14,15,16,17,18}};

    for (int i = 0; i < rows ; i++){
        for (int j=0; j< columns; j++)
            std::cout << my_array[i][j]<<"\t";
        std::cout << std::endl;
    }

    return 0;
}

With the output:使用 output:

0   1   2   3   4   
5   6   7   8   9   
10  11  12  13  13  
14  15  16  17  18  
using namespace std;
#include<iostream>
int main(){
int num[3][3],i=0,sum=0;

for(i=0;i<3;i++){
    for(int j=0;j<3;j++){
        cin>>num[i][j];
        sum+=num[i][j];

}

}

for(i=0;i<3;i++){
    for(int j=0;j<3;j++){
        cout<<num[i][j]<<"\t";
    }
    cout<<endl;
    }

cout<<sum<<endl;
return 0;
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM