3 Replies - 585 Views - Last Post: 19 December 2009 - 07:59 AM Rate Topic: -----

#1 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

strange multidimensional arrays

Posted 19 December 2009 - 07:05 AM

i am utterly confounded by this phenomenon.
i have searched and experimented with the code,
and i don't know how to explain the problem.


the code is tiny and very simple.


#include <iostream>

using namespace std;



int field[2][2];



void draw()
{

for(int b = 0; b < 3; b++)
   {
 cout <<"\n";
 for(int a = 0; a < 3; a++)
	{cout << field[a][b];}
   }


}




int main()
{
field[1][0] = 1;

draw();

cin.ignore();

} 



if i have understood multidimensional arrays right
the screen should look like this:

010
000
000

but alas! every time i run the program it prints this:

010
000
100

if i write field[0][2] = 1; instead the same thing happens.

some parts of the array are connected with eachother
in some way.


is there something i have missed completely?
is my computer damaged in some way?

or are strange cosmical forces halting my work?

Is This A Good Question/Topic? 0
  • +

Replies To: strange multidimensional arrays

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: strange multidimensional arrays

Posted 19 December 2009 - 07:08 AM

An array sized [2] is indexed from 0 to 1 - At the moment you're overflowing by one. This applies to all arrays, whether they're 'multi-dimensional' or not

This post has been edited by Bench: 19 December 2009 - 07:09 AM

Was This Post Helpful? 1
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: strange multidimensional arrays

Posted 19 December 2009 - 07:13 AM

EDIT - Bench beat me with a better constructed answer.

I withdraw to sleep.

This post has been edited by janotte: 19 December 2009 - 07:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

Re: strange multidimensional arrays

Posted 19 December 2009 - 07:59 AM

View PostBench, on 19 Dec, 2009 - 06:08 AM, said:

An array sized [2] is indexed from 0 to 1 - At the moment you're overflowing by one. This applies to all arrays, whether they're 'multi-dimensional' or not




strange. i don't understand how i could miss that. i consider myself a somewhat proficient programmer after all.

sorry to have wasted your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1