Results 1 to 5 of 5

Thread: C++ Output 2D array?

  1. #1
    Join Date
    Mar 2009
    Posts
    60

    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?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: C++ Output 2D array?

    hi,
    Because your endl at the wrong place.

  3. #3
    Join Date
    Mar 2009
    Posts
    60

    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. #4
    Join Date
    May 2008
    Posts
    2,389

    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.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    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 
      )

Similar Threads

  1. Array of array in PHP
    By cyber-noob in forum Software Development
    Replies: 4
    Last Post: 26-02-2010, 05:13 PM
  2. Output the contents of a PHP Array
    By Jesus-Ernesto in forum Software Development
    Replies: 3
    Last Post: 27-01-2010, 09:52 PM
  3. Assigning an array to an array
    By MACE in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 05:19 PM
  4. Echo array output in php?
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 07-07-2009, 08:28 AM
  5. XML to ARRAY
    By Vandam in forum Software Development
    Replies: 3
    Last Post: 29-06-2009, 06:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,425,144.44065 seconds with 17 queries