简体   繁体   中英

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. 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. 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"; to 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:

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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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