TAILIEUCHUNG - Lecture Introduction to computer and programming - Lecture No 20

The contents of this chapter include all of the following: 2 Dimensional array, Initializing 2 Dimensional array, Memory map of 2-D array, Pointer and 2-D array, Array of pointer, Passing 2-D Array to a Function. | CSC103: Introduction to Computer and Programming Lecture No 20 Previous lecture Pointer arithmetic – a program Sorting techniques – a program Passing an Entire Array to a Function Array and pointer Today’s lecture outline 2 Dimensional array Initializing 2 Dimensional array Memory map of 2-D array Pointer and 2-D array Array of pointer Passing 2-D Array to a Function Two Dimensional Arrays It is also possible for arrays to have two or more dimensions For example you want to input roll no and mark of 4 students. Since we know 1-D array, so for this problem we can have two 1-D array One for roll number Second for marks One 2-D array can be used to store these values Example program 1 0 0 1 2 3 row column Roll Number Marks stud[0][0] stud[0][1] stud[1][0] stud[1][1] stud[2][0] stud[2][1] stud[3][0] stud[3][1] 10 85 12 65 13 89 14 92 Go to program Initializing a 2-Dimensional Array Example program Write a program that adds two 4 x 4 matrices. 3 5 6 1 5 3 2 9 1 0 -3 4 7 2 3 -9 4 2 -7 0 5 7 3 1 6 3 -1 0 8 9 -2 3 + 7 7 -1 1 10 10 5 10 7 3 -4 4 15 11 1 -6 = Go to program Memory map of 2D array Remember student array int s[4][2]; But actually memory allocated is continuous 1 0 0 1 2 3 10 85 12 65 13 89 14 92 10 85 12 65 13 89 14 92 s[0][0] s[0][1] s[1][0] s[1][1] s[2][0] s[2][1] s[3][0] s[3][1] Each row of a two-dimensional array can be thought of as a one-dimensional a[4][2] This is important to know if you want to access 2-D array elements using pointer Pointers and 2-Dimensional Arrays 1 0 0 1 2 3 10 85 12 65 13 89 14 92 Array 1 Array 2 Array 3 Array 4 Cont. 1234 56 1212 33 1434 80 1312 78 s[0][0] s[0][1] s[1][0] s[1][1] s[2][0] s[2][1] s[3][0] s[3][1] 65508 65512 65516 65520 65524 65528 65532 65536 s[0] s[1] s[2] s[3] 65508 65516 65524 65532 Cont Now we know each 1-D array Next thing is to access elements of a 1-D array Suppose we want to refer to the element s[2][1] using pointers s[2] 65524 that is that address of third 1-D array 65524 + 1 65528 .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.