ARRAYS

Hello, i am having an issue with my code, i have the 1.5.6 software and an uno r3, when i run this code it should print out a 2d array that is completely zeros, but for some reason i get 17 rows of 0’s, than a row of 457, and it goes back and forth like that, once you are like 10 lines form the end it makes completely random numbers.
After that i try to make the part of the array [0][1 ] equal to 9, it makes the whole row 9, any suggestions on ho to fix these 2 problems?

#include <SPI.h>

#include <Servo.h>


const int ultra3 = 4;
const int ultra = 7;
const int ultra2 = 6;
const int in = 5;
const int in2 = 3;
const int in3 = 8;

Servo myservo;

const int MAX_PAGENAME_LEN = 8;

char buffer[MAX_PAGENAME_LEN + 1];



unsigned int depth = 0;
unsigned int width = 0;
unsigned int height = 0;

int array[0][0];


void setup()
{
  randomSeed(analogRead(0));
  pinMode(in, INPUT);
  pinMode(in2, INPUT);
  pinMode(ultra, OUTPUT);
  pinMode(ultra2, OUTPUT);
  Serial.begin(9600);

  myservo.attach(9);
  delay(1000);
  createroom();
  Serial.println(width);
  Serial.println(height);

  zero(width, height); //zeros out array

}

void loop()
{
  zero(width, height);
  zero(width, height);

  Serial.println("starting");
  


  for (int p = 0; p < height; p++) {
    for (int i = 0; i < width; i++) {
      
      Serial.print(array[i][p]);
      Serial.print("|");
    }
    
    Serial.println();

  }





  array[0][1] = 9;
 
  Serial.println("start mid");
  for (int p = 0; p < height; p++) {
    for (int i = 0; i < width; i++) {
     
      Serial.print(array[i][p]);
      Serial.print("|");
    }
    Serial.println();
  }
 

 
}
void zero(int j, int k) {

  array[width][height];

  for (int i = 0; i < height; i++) {
    int u = 0;
    for (int p = 0; p < width; p++) {
      array[i][p] = 0;

    }


  }
}
void createroom() { //test
  int j = random(1, 50); //pointing left
  int i = random(1, 50); //pointing straight
  int u = random(1, 50); //pointing up
  int q = random(1, 50); //pointing down

  int l = random(1, 50); //pointing straight
  int h = random(1, 50); //pointing right
  depth = u + h;
  height = i + l;
  width = j + h;

}
void createRoom() { //creates the data neccesary for to form the room

  int j = irRead(in, ultra); //pointing left
  int i = irRead(in2, ultra2); //pointing straight
  int u = irRead(in3, ultra3); //pointing up
  int q = irRead(in3, ultra3); //pointing down
  myservo.write(0);
  int l = irRead(in, ultra); //pointing straight
  int h = irRead(in2, ultra2); //pointing right
  depth = u + h;
  height = i + l;
  width = j + h;
  myservo.write(90);


}



int irRead(int readPin, int triggerPin)
{
  int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
  int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
  int i;
  for (i = 0; i <= cycles; i++)
  {
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead
  }
  return digitalRead(readPin);
}
int array[0][0];

A 0 by 0 array. How useless is that?

You are writing WAY out of bounds of that array, and then reading out of bounds. Rubbish is not surprising.

i know, i was working to fix that. here is what i have now and it is even more buggy:

#include <SPI.h>

#include <Servo.h>


Servo myservo;

const int MAX_PAGENAME_LEN = 8;

char buffer[MAX_PAGENAME_LEN + 1];







void setup()
{
  randomSeed(analogRead(0));

  Serial.begin(9600);

  myservo.attach(9);
  delay(1000);
  
  

 
}

void loop()
{
  const int x=createroomRight();
  const int y=createroomStraight();
  int array[x][y];

  for (int i = 0; i < y; i++) {
    int u = 0;
    for (int p = 0; p < x; p++) {
      array[i][p] = 0;

    }


  }

  Serial.println("starting");
  


  for (int p = 0; p < y; p++) {
    for (int i = 0; i < x; i++) {
     
      Serial.print(array[i][p]);
      Serial.print("|");
    }
    
    Serial.println();

  }





  array[0][1] = 9;
  array[1][3] = 8;
  Serial.println("start mid");
  for (int p = 0; p < y; p++) {
    for (int i = 0; i < x; i++) {
     
      Serial.print(array[i][p]);
      Serial.print("|");
    }
    
    Serial.println();
  }

}

int createroomRight() { //test
  int j = random(1, 50); //pointing left
  int i = random(1, 50); //pointing straight
  int u = random(1, 50); //pointing up
  int q = random(1, 50); //pointing down

  int l = random(1, 50); //pointing straight
  int h = random(1, 50); //pointing right
   int depth = u + h;
  int height = i + l;
 int width = j + h;
return width;
}

int createroomStraight() { //test
  int j = random(1, 50); //pointing left
  int i = random(1, 50); //pointing straight
  int u = random(1, 50); //pointing up
  int q = random(1, 50); //pointing down

  int l = random(1, 50); //pointing straight
  int h = random(1, 50); //pointing right
   int depth = u + h;
  int height = i + l;
 int width = j + h;
return height;
}

now after the above code the serial monitor spits out random symbols and doesn’t even print anything right at all.

    int u = 0;

What's this for?

What are you really trying to do? Dynamically declaring arrays like you are doing really isn't a good idea.

the int u is unnecessary i forgot to delete it, based of the code i have now, can you tell me how to make it work better because I'm a beginner at this.

PaulS:
Dynamically declaring arrays like you are doing really isn’t a good idea.

Why not? Unless persistent data is needed, its a perfectly valid way of allocating an array to the stack.

@OP

You loop variables are backwards and could possibly be writing out of bounds.

  const int x=createroomRight();
  const int y=createroomStraight();
  int array[x][y];

  for (int i = 0; i < y; i++) {
    int u = 0;
    for (int p = 0; p < x; p++) {
      array[i][p] = 0;

    }
  }

The array is declared

[x][y]

, however you address it like [y][x]

ok, it is starting to work now thank you for your help.