2 Dimensional Arrays in C++

I started learning arrays in C++ and came over a little side note in the book talking about 2D arrays in breif.

I tested it out and i was amazed that it could give the programmer the ability to store data or information and lay it out infront of him in a spread sheet format.

In a normal array to access elements i would simply do this:

int matrix[2] = { 1, 15};

But in 2D arrays :The only way it tells me to actually acces elements is by using a loop:

int fly[2][2];
int i = 0;
int n=0;
for(i=0; i<2; i++){
    for (n=0; n<2; n++){
        fly[i][n] =0;
    }
}
for(i=0; i<2; i++){
    for (n =0; n<2; n++){
        cout << fly[i][n]<< endl;
    }
}

I have tried accessing elements the old way:

int fly[2][2] = { 0};

but i noticed that this changes all the elements to 0

So..

  1. Can anyone explain when i try accessing this 2D array like this all the elements change.

  2. Is there another way to access 2D array elements without using a loop.

Thank you all.

1

In a normal array to access elements i would simply do this:

int matrix[2] = { 1, 15};

You’re declaring an array with that code. You’re also assigning ‘1’ to the int at index 0 and ’15’ to the int at index 1.

I have tried accessing elements the old way:

int fly[2][2] = { 0};

but i noticed that this changes all the elements to 0

That code doesn’t assign ‘0’ to the int at coordinates 2, 2… it creates a 2×2 array and tries to initialize it with the array that you’ve provided. Since the array is smaller than the size of the array that you’re assigning to, the rest of the elements are simply initialized to 0. If you’d used ‘{15}’ instead, you’d get a ’15’ in the first position, and ‘0’ everywhere else. Try this instead:

int fly[2][2] = {{1, 2}, {3, 4}};

or even just:

int fly[2][2] = {1, 2, 3, 4};

In those cases, enough values are provided for every element of fly. If you this, though:

int fly[2][2] = {1, 2, 3};

you’ll find that fly[1][1] is 0 because you didn’t provide enough data.

Can anyone explain when i try accessing this 2D array like this all the elements change.

You can’t really say that they “changed”. You’re only just creating the array, so other than the initialization you don’t know what values are in the array. At any point after you create the array, you can access the array using the two subscripts:

int fly[2][2] = {{1, 2}, {3, 4}};
printf("%d %d %d %dn", fly[0][0], fly[0][1], fly[1][0], fly[1][1]);   // prints '1 2 3 4'

2

This is an implementation question, so it should therefore be posted on StackOverflow.com , not Programmers.StackExchange.com.

int fly[2][2] = { 0}; is declaring an array (hence why it has int at the front) and assigning it (to {0}). It’s been years since I’ve done any C++, but I’m somewhat surprised it actually compiles (I’ll take your word for it), seeing as how you are assigning a one-dimensional array to a two-dimensional array.

Accessing the array (and outputting it to the screen) would be done by cout << fly[2][2].

int fly[2][2]={0} ; allocates and initialises an new array – it is not an accessor to an existing array. Study up in C++ Initialises and partial intialisation.

Question 2 : As far as acessing the array elements – you could use

 cout << fly[0][0]<< endl;
 cout << fly[0][1]<< endl;
 cout << fly[1][0]<< endl;
 cout << fly[1][1]<< endl;

This does not use a loop, the quesiton clear but probably not the question you want answered. – what part of using a loop do you have a problem with?

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật