-5

I'm trying to create a program with C++ that calculates values for a 2D array and then prints them out in the form of a table. Each nvalue ranges from 1 to 6 decimal places, so I need to give them equal space in the table. I've tried looking around but I've had trouble understanding it. Can I use printf to give equal space to each value in a 2D array? Would I use a for loop to print each value using printf?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

1 Answers1

0

For example you have a two dimensional array like :

int a[5][4];

So if you want to store data and then print, you could try doing it this way:

int i,j=0;
cout<< "\nEnter your array values : \n";
for (i=0;i<5;i++){
  for(j=0;j<4;j++){
      cin>>a[i][j];
 }    
}

To print :

for (i=0;i<5;i++){
  for(j=0;j<4;j++){
      cout<<a[i][j];
 }    
cout<< "\n";
}

Edit: yikes, just re-read your question, you wish to print them in a table?: yes, you can use printf for equal spaces, just give the new line between the for loops, so that it appears in a table format.

YouHaveaBigEgo
  • 220
  • 6
  • 13