Odd numbers, where there should be 0's

Jun 3, 2010 at 1:55pm
I have placed an integer in a matrix and have asked it to read out the values in each part of the matrix. The number is correct where I've placed the integer but the other section give me very long numbers where there ought to be 0's. Please could someone tell me why this occurs? Thank you




#include <iostream>
#include <cmath>
#include <cstdlib>

//#include <algorithm> //provides function count
//#include "NewBadgerClassesHeader.h"

using namespace std;

int main()
{
int size=2;
int AdMs =2;
int x=2;
int y=2;
int A[x][y];//creates array


for (x=0;x<size;x++){
for (y=0;y<size;y++){

A[0][1]=AdMs;//places 2 susceptible adult males into right of the top row of 2x2grid

cout<<A[x][y]<<endl;//2 is in the right place but am getting odd numbers for the remainder.
}
}




system ("pause");

return 0;
}

Jun 3, 2010 at 2:19pm
Every element except the one at [0][1] is uninitialized.

(and you'll get lots of comments about using the gcc variable length array extension to the language; it's
not a big deal, and it won't break your program, just know that your code is not portable to other compilers
as written. To fix, change int x and int y to const int x and const int y. Then you'll have to change the
variable name in your for loops).
Jun 3, 2010 at 2:40pm
Thank you
Topic archived. No new replies allowed.