-
C++ Output 2D array?
hi,
I need an array with 400 values of x and y coordinate. (Values 1-400, just up counting)
Code:
# include <iostream>
using namespace std;
int main () (
int rows = 20;
int columns = 20;
int i;
int j;
int matrix [rows] [columns]; / / 2-D array of produce (x + y Coord)
for (i = 1; i <rows; i + +) (/ / lines
court <<i << "" <<endl;
for (j = 0, j <column; j + +) (/ / columns
matrix [i] [j] = i * columns + j;
court <<i * columns + j << "";
)
)
cout << "\ n" << "Position:" <<matrix [1] [1] <<endl; / / output array value at position [row] [column]
the problem is first line actually connected to each end of the line?
-
Re: C++ Output 2D array?
hi,
Because your endl at the wrong place.
-
Re: C++ Output 2D array?
I think this is not the mistake.
I am completely out endl arises following chain:
1 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 2 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 3 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 4 80 81
The error is expected in the 1st Loop.
-
Re: C++ Output 2D array?
Try
Code:
for (i = 0; i <rows; i + +) (/ / lines
for (j = 0, j <column; j + +) (/ / columns
matrix [i] [j] = i * columns + j +1;
court <<matrix [i] [j] << "";
)
cout <<endl; / / here is the end of line
)
However, without the header row and column.
-
Re: C++ Output 2D array?
If you also want to have, the more loops
Code:
int matrix [rows +1] [column +1], / / you need logically extra row / column for the heads.
/ / Headers for rows and columns
for (i = 0; i <rows; i + +) (
matrix [i] [0] = i;
)
for (i = 0; i <columns; i + +) (
matrix [0] [i] = i;
)
/ / empty boxes and fill everything out.
for (i = 0; i <rows; i + +) (
for (j = 0, j <column; j + +) (
if (i! = 0 & & j! = 0) / / header should not be overwritten ...
matrix [i] [j] = (i-1) * columns + j / / puts the figures are 1.1 to begin with 1
cout <<matrix [i] [j] << "";
)
cout <<endl; / / here is the end of line
)
Page generated in 1,751,019,506.20461 seconds with 10 queries