#1
| |||
| |||
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] |
#2
| |||
| |||
Re: C++ Output 2D array? hi, Because your endl at the wrong place. |
#3
| |||
| |||
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. |
#4
| |||
| |||
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 ) |
#5
| |||
| |||
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 ) |
![]() |
|
Tags: array, column, row |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Array of array in PHP | cyber-noob | Software Development | 4 | 26-02-2010 05:13 PM |
Output the contents of a PHP Array | Jesus-Ernesto | Software Development | 3 | 27-01-2010 09:52 PM |
Assigning an array to an array | MACE | Software Development | 3 | 18-11-2009 05:19 PM |
Echo array output in php? | Chrisch | Software Development | 3 | 07-07-2009 08:28 AM |
XML to ARRAY | Vandam | Software Development | 3 | 29-06-2009 06:14 PM |