CSI 123: Structured Programming Language
Array
Dr. Kamruddin Nur
Associate Professor & Chairman
kamruddin.nur@gmail.com
November, 2017
CSE
Department of
STAMFORD UNIVERSITY BANGLADESH
"Education for Tomorrow's World"
Contents
1 Array
2 Declaration and Initialization
3 Array Index
4 Accessing Array Elements and Printing
5 Insertion
6 Insertion and Printing
7 Deleting / Removing of an element
8 Multi-dimensional Array
9 Printing 2D Array
10 Printing 3D Array
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 1 / 24
Textbook and Reference Books
Textbook:
• Let Us C, by Yashavant Kanetkar, 13th Edition, Published by BPB Publications.
Reference books:
• Teach Yourself C, by Herbert Schildt, 4th Edition, Published by Osborne.
• Sams Teach Yourself C, by Bradley L. Jones & Peter Aitken, 6th Edition.
• The C Programming Language, by Brian W. Kernighan & Dennis M. Ritchie, 2nd
Edition, Published by Prentice Hall.
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 2 / 24
1 Array
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 3 / 24
Array
– An array is a collection of data storage locations, each storing the same
type of data and having the same name.
int number [5];
float marks [10];
double salary [10];
char alphabet [5];
– Each storage location in an array is called an array element.
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 4 / 24
Array (Cont’d)
– An array is a collection of variables of same data types.
– All elements of array are stored in the contiguous memory locations.
– The size of array must be a constant integral value.
– Individual elements in an array can be accessed by the name of the array and
an integer enclosed in square bracket called subscript/index variable like
number [10].
– The first element in an array is at index 0, whereas the last element is at index
[ArraySize - 1].
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 5 / 24
2 Declaration and Initialization
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 6 / 24
Array Declaration and Initialization
Array Declaration:
int number [5];
float marks [5];
double salary [5];
Array Initialization:
marks [0] = 5;
marks [1] = 2;
marks [2] = 9;
marks [3] = 1;
marks [4] = 1;
Array Declaration and Initialization:
int number [5] = {1 ,2 ,3 ,4 ,500};
float score [10] = {10 ,8 ,7 ,9 ,4.5 ,6 ,7 ,9 ,8.5 ,10};
double salary [5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000.00};
double salary [] = {10000.00 , 15000.00 , 20500.500 , 5050.50 , 30000.00};
char alphabet [5] = {’A’, ’B’, ’C’, ’D’, ’E’};
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 7 / 24
3 Array Index
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 8 / 24
Array Index
- Array index is the location of an element in an array
- You can access elements of an array by indices
- For example, Let’s get the following salary array elements using index numbers -
1 double salary [5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000.00};
2 double salary1 = salary [0];
3 double salary2 = salary [1];
4 double salary3 = salary [2];
5 double salary4 = salary [3];
6 double salary5 = salary [4];
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 9 / 24
4 Accessing Array Elements and Printing
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 10 / 24
Accessing elements using index and printing with loops
1 #include <stdio.h>
2 void main (){
3 char alphabet [5]={ ’A’,’B’,’C’,’D’,’E’};
4
5 /* Let ’s print all alphabets one by one */
6 int i;
7 for(i=0; i<5;i++){
8 printf("%c ", alphabet[i]);
9 }
10 }
1 #include <stdio.h>
2 void main (){
3 double salary [] = {10000.00 , 15000.00 , 20500.50 ,
5050.50 , 30000.00};
4
5 /* Let ’s print all salary one by one */
6 int i;
7 for(i=0; i<5;i++){
8 printf("Salary [%d]: %.2 lf n", i, salary[i]);
9 }
10 }
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 11 / 24
Accessing elements using index and printing with loops
1 #include <stdio.h>
2 void main (){
3 char alphabet [5]={ ’A’,’B’,’C’,’D’,’E’};
4
5 /* Let ’s print all alphabets one by one */
6 int i;
7 for(i=0; i<5;i++){
8 printf("%c ", alphabet[i]);
9 }
10 }
1 #include <stdio.h>
2 void main (){
3 double salary [] = {10000.00 , 15000.00 , 20500.50 ,
5050.50 , 30000.00};
4
5 /* Let ’s print all salary one by one */
6 int i;
7 for(i=0; i<5;i++){
8 printf("Salary [%d]: %.2 lf n", i, salary[i]);
9 }
10 }
Program Output:
A B C D E
Program Output:
Salary [0]: 10000.00
Salary [1]: 15000.00
Salary [2]: 20500.50
Salary [3]: 5050.50
Salary [4]: 30000.00
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 11 / 24
5 Insertion
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 12 / 24
Insertion: Insert elements in an array
1 #include <stdio.h>
2 void main (){
3 int size = 5;
4 float marks[size ];
5
6 /* Inserting marks into marks array */
7 int i;
8 for(i = 0; i <= size; i++){
9 printf("Enter a mark: ");
10 scanf("%f", &marks[i]);
11 }
12
13 }
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 13 / 24
Insertion: Insert elements in an array
1 #include <stdio.h>
2 void main (){
3 int size = 5;
4 float marks[size ];
5
6 /* Inserting marks into marks array */
7 int i;
8 for(i = 0; i <= size; i++){
9 printf("Enter a mark: ");
10 scanf("%f", &marks[i]);
11 }
12
13 }
Program Output:
Enter a mark: 50.5
Enter a mark: 40
Enter a mark: 30.5
Enter a mark: 75.5
Enter a mark: 80
Enter a mark: 90
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 13 / 24
6 Insertion and Printing
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 14 / 24
Insertion and Printing
1 #include <stdio.h>
2 void main (){
3 int size = 5;
4 float marks[size ];
5
6 /* Inserting marks into marks array */
7 int i;
8 for(i = 0; i <= size; i++){
9 printf("Enter a marks: ");
10 scanf("%f", &marks[i]);
11 }
12
13 /* Let ’s print all marks in the marks array */
14
15 printf("nPrinting all marks:");
16
17 int j;
18 for(j = 0; j <= size; j++){
19 printf("n%.1f ", marks[j]);
20 }
21
22 }
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 15 / 24
Insertion and Printing
1 #include <stdio.h>
2 void main (){
3 int size = 5;
4 float marks[size ];
5
6 /* Inserting marks into marks array */
7 int i;
8 for(i = 0; i <= size; i++){
9 printf("Enter a marks: ");
10 scanf("%f", &marks[i]);
11 }
12
13 /* Let ’s print all marks in the marks array */
14
15 printf("nPrinting all marks:");
16
17 int j;
18 for(j = 0; j <= size; j++){
19 printf("n%.1f ", marks[j]);
20 }
21
22 }
Program Output:
Enter a marks: 80
Enter a marks: 85.5
Enter a marks: 75
Enter a marks: 63.5
Enter a marks: 90
Enter a marks: 11.0
Printing all marks:
80.0
85.5
75.0
63.5
90.0
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 15 / 24
7 Deleting / Removing of an element
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 16 / 24
Deleting / Removing an element
1 #include <stdio.h>
2 void main (){
3 int number_array [5] = {11, 12, 13, 14, 15};
4
5 // let ’s print all elements in the number_array
6 printf("Array before deletion: n");
7 int i;
8 for (i = 0; i < 5; i++) {
9 printf("[%d] %d n", i, number_array [i]);
10 }
11
12 printf("let’s delete element at index 3 n");
13 int index;
14 for (index = 3 ; index < 5 - 1 ; index ++){
15 number_array [index] = number_array [index
+1];
16 }
17
18 printf("Array after deletion: n");
19 for (i = 0; i < 5 - 1; i++) {
20 printf("[%d] %d n", i, number_array [i]);
21 }
22 }
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 17 / 24
Deleting / Removing an element
1 #include <stdio.h>
2 void main (){
3 int number_array [5] = {11, 12, 13, 14, 15};
4
5 // let ’s print all elements in the number_array
6 printf("Array before deletion: n");
7 int i;
8 for (i = 0; i < 5; i++) {
9 printf("[%d] %d n", i, number_array [i]);
10 }
11
12 printf("let’s delete element at index 3 n");
13 int index;
14 for (index = 3 ; index < 5 - 1 ; index ++){
15 number_array [index] = number_array [index
+1];
16 }
17
18 printf("Array after deletion: n");
19 for (i = 0; i < 5 - 1; i++) {
20 printf("[%d] %d n", i, number_array [i]);
21 }
22 }
Program Output:
Array before deletion:
[0] 11
[1] 12
[2] 13
[3] 14
[4] 15
let ’s delete element at
index 3
Array after deletion:
[0] 11
[1] 12
[2] 13
[3] 15
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 17 / 24
8 Multi-dimensional Array
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 18 / 24
Multi-dimensional Array
Declaration:
int table [2][3];
int three_d_array [3][3][3];
Declaration and Initialization:
int table1 [2][3] = {{1 ,2 ,3} ,{4 ,5 ,6}};
int grid [][3] = {{1 ,2 ,3} ,{4 ,5 ,6}};
int table2 [2][3] = {1,2,3,4,5,6};
int three_d_array [3][3][3] = {
{{10 , 20, 30} ,{40 , 50, 60} ,{70 , 80, 90}} ,
{{11 , 21, 31} ,{41 , 51, 61} ,{71 , 81, 91}} ,
{{33 , 34, 35} ,{36 , 37, 38} ,{39 , 40, 41}}
};
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 19 / 24
9 Printing 2D Array
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 20 / 24
Printing 2D Array
1 #include <stdio.h>
2 void main (){
3 int row = 3;
4 int col = 4;
5 int table [3][4] =
6 {
7 {1, 2, 3, 4},
8 {10, 11, 12, 13},
9 {20, 21, 22, 23}
10 };
11 // printing 2d array
12 int i,j;
13 for(i = 0; i < row; ++i)
14 {
15 for(j = 0; j < col; ++j)
16 {
17 printf("%d ", table[i][j]);
18 }
19 printf("n");
20 }
21
22 }
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 21 / 24
Printing 2D Array
1 #include <stdio.h>
2 void main (){
3 int row = 3;
4 int col = 4;
5 int table [3][4] =
6 {
7 {1, 2, 3, 4},
8 {10, 11, 12, 13},
9 {20, 21, 22, 23}
10 };
11 // printing 2d array
12 int i,j;
13 for(i = 0; i < row; ++i)
14 {
15 for(j = 0; j < col; ++j)
16 {
17 printf("%d ", table[i][j]);
18 }
19 printf("n");
20 }
21
22 }
Program Output:
1 2 3 4
10 11 12 13
20 21 22 23
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 21 / 24
10 Printing 3D Array
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 22 / 24
Printing 3D Array
1 #include <stdio.h>
2 void main (){
3 int x = 3; int y = 3; int z = 3;
4 int three_d_array [3][3][3] = {
5 {{10 , 20, 30} ,{40 , 50, 60} ,{70 , 80, 90}} ,
6 {{11 , 21, 31} ,{41 , 51, 61} ,{71 , 81, 91}} ,
7 {{33 , 34, 35} ,{36 , 37, 38} ,{39 , 40, 41}}
8 };
9
10 // printing 3d array
11 int i,j,k;
12 for(i=0;i<x;i++){
13 for(j=0;j<y;j++){
14 for(k=0;k<z;k++){
15 printf("%dt",three_d_array [i][j][k]);
16 }
17 printf("n");
18 }
19 printf("n");
20 }
21 }
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 23 / 24
Printing 3D Array
1 #include <stdio.h>
2 void main (){
3 int x = 3; int y = 3; int z = 3;
4 int three_d_array [3][3][3] = {
5 {{10 , 20, 30} ,{40 , 50, 60} ,{70 , 80, 90}} ,
6 {{11 , 21, 31} ,{41 , 51, 61} ,{71 , 81, 91}} ,
7 {{33 , 34, 35} ,{36 , 37, 38} ,{39 , 40, 41}}
8 };
9
10 // printing 3d array
11 int i,j,k;
12 for(i=0;i<x;i++){
13 for(j=0;j<y;j++){
14 for(k=0;k<z;k++){
15 printf("%dt",three_d_array [i][j][k]);
16 }
17 printf("n");
18 }
19 printf("n");
20 }
21 }
Program Output:
10 20 30
40 50 60
70 80 90
11 21 31
41 51 61
71 81 91
33 34 35
36 37 38
39 40 41
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 23 / 24
Thanks
Thanks for your time and attention!
kamruddin.nur@gmail.com
sites.google.com/view/kamruddinnur
CSE
Department of
STAMFORD UNIVERSITY BANGLADESH
"Education for Tomorrow's World"
CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 24 / 24

Arrays in C

  • 1.
    CSI 123: StructuredProgramming Language Array Dr. Kamruddin Nur Associate Professor & Chairman kamruddin.nur@gmail.com November, 2017 CSE Department of STAMFORD UNIVERSITY BANGLADESH "Education for Tomorrow's World"
  • 2.
    Contents 1 Array 2 Declarationand Initialization 3 Array Index 4 Accessing Array Elements and Printing 5 Insertion 6 Insertion and Printing 7 Deleting / Removing of an element 8 Multi-dimensional Array 9 Printing 2D Array 10 Printing 3D Array CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 1 / 24
  • 3.
    Textbook and ReferenceBooks Textbook: • Let Us C, by Yashavant Kanetkar, 13th Edition, Published by BPB Publications. Reference books: • Teach Yourself C, by Herbert Schildt, 4th Edition, Published by Osborne. • Sams Teach Yourself C, by Bradley L. Jones & Peter Aitken, 6th Edition. • The C Programming Language, by Brian W. Kernighan & Dennis M. Ritchie, 2nd Edition, Published by Prentice Hall. CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 2 / 24
  • 4.
    1 Array CSI 123:Structured Prog. Language Dr. Kamruddin Nur November, 2017 3 / 24
  • 5.
    Array – An arrayis a collection of data storage locations, each storing the same type of data and having the same name. int number [5]; float marks [10]; double salary [10]; char alphabet [5]; – Each storage location in an array is called an array element. CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 4 / 24
  • 6.
    Array (Cont’d) – Anarray is a collection of variables of same data types. – All elements of array are stored in the contiguous memory locations. – The size of array must be a constant integral value. – Individual elements in an array can be accessed by the name of the array and an integer enclosed in square bracket called subscript/index variable like number [10]. – The first element in an array is at index 0, whereas the last element is at index [ArraySize - 1]. CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 5 / 24
  • 7.
    2 Declaration andInitialization CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 6 / 24
  • 8.
    Array Declaration andInitialization Array Declaration: int number [5]; float marks [5]; double salary [5]; Array Initialization: marks [0] = 5; marks [1] = 2; marks [2] = 9; marks [3] = 1; marks [4] = 1; Array Declaration and Initialization: int number [5] = {1 ,2 ,3 ,4 ,500}; float score [10] = {10 ,8 ,7 ,9 ,4.5 ,6 ,7 ,9 ,8.5 ,10}; double salary [5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000.00}; double salary [] = {10000.00 , 15000.00 , 20500.500 , 5050.50 , 30000.00}; char alphabet [5] = {’A’, ’B’, ’C’, ’D’, ’E’}; CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 7 / 24
  • 9.
    3 Array Index CSI123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 8 / 24
  • 10.
    Array Index - Arrayindex is the location of an element in an array - You can access elements of an array by indices - For example, Let’s get the following salary array elements using index numbers - 1 double salary [5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000.00}; 2 double salary1 = salary [0]; 3 double salary2 = salary [1]; 4 double salary3 = salary [2]; 5 double salary4 = salary [3]; 6 double salary5 = salary [4]; CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 9 / 24
  • 11.
    4 Accessing ArrayElements and Printing CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 10 / 24
  • 12.
    Accessing elements usingindex and printing with loops 1 #include <stdio.h> 2 void main (){ 3 char alphabet [5]={ ’A’,’B’,’C’,’D’,’E’}; 4 5 /* Let ’s print all alphabets one by one */ 6 int i; 7 for(i=0; i<5;i++){ 8 printf("%c ", alphabet[i]); 9 } 10 } 1 #include <stdio.h> 2 void main (){ 3 double salary [] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000.00}; 4 5 /* Let ’s print all salary one by one */ 6 int i; 7 for(i=0; i<5;i++){ 8 printf("Salary [%d]: %.2 lf n", i, salary[i]); 9 } 10 } CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 11 / 24
  • 13.
    Accessing elements usingindex and printing with loops 1 #include <stdio.h> 2 void main (){ 3 char alphabet [5]={ ’A’,’B’,’C’,’D’,’E’}; 4 5 /* Let ’s print all alphabets one by one */ 6 int i; 7 for(i=0; i<5;i++){ 8 printf("%c ", alphabet[i]); 9 } 10 } 1 #include <stdio.h> 2 void main (){ 3 double salary [] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000.00}; 4 5 /* Let ’s print all salary one by one */ 6 int i; 7 for(i=0; i<5;i++){ 8 printf("Salary [%d]: %.2 lf n", i, salary[i]); 9 } 10 } Program Output: A B C D E Program Output: Salary [0]: 10000.00 Salary [1]: 15000.00 Salary [2]: 20500.50 Salary [3]: 5050.50 Salary [4]: 30000.00 CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 11 / 24
  • 14.
    5 Insertion CSI 123:Structured Prog. Language Dr. Kamruddin Nur November, 2017 12 / 24
  • 15.
    Insertion: Insert elementsin an array 1 #include <stdio.h> 2 void main (){ 3 int size = 5; 4 float marks[size ]; 5 6 /* Inserting marks into marks array */ 7 int i; 8 for(i = 0; i <= size; i++){ 9 printf("Enter a mark: "); 10 scanf("%f", &marks[i]); 11 } 12 13 } CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 13 / 24
  • 16.
    Insertion: Insert elementsin an array 1 #include <stdio.h> 2 void main (){ 3 int size = 5; 4 float marks[size ]; 5 6 /* Inserting marks into marks array */ 7 int i; 8 for(i = 0; i <= size; i++){ 9 printf("Enter a mark: "); 10 scanf("%f", &marks[i]); 11 } 12 13 } Program Output: Enter a mark: 50.5 Enter a mark: 40 Enter a mark: 30.5 Enter a mark: 75.5 Enter a mark: 80 Enter a mark: 90 CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 13 / 24
  • 17.
    6 Insertion andPrinting CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 14 / 24
  • 18.
    Insertion and Printing 1#include <stdio.h> 2 void main (){ 3 int size = 5; 4 float marks[size ]; 5 6 /* Inserting marks into marks array */ 7 int i; 8 for(i = 0; i <= size; i++){ 9 printf("Enter a marks: "); 10 scanf("%f", &marks[i]); 11 } 12 13 /* Let ’s print all marks in the marks array */ 14 15 printf("nPrinting all marks:"); 16 17 int j; 18 for(j = 0; j <= size; j++){ 19 printf("n%.1f ", marks[j]); 20 } 21 22 } CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 15 / 24
  • 19.
    Insertion and Printing 1#include <stdio.h> 2 void main (){ 3 int size = 5; 4 float marks[size ]; 5 6 /* Inserting marks into marks array */ 7 int i; 8 for(i = 0; i <= size; i++){ 9 printf("Enter a marks: "); 10 scanf("%f", &marks[i]); 11 } 12 13 /* Let ’s print all marks in the marks array */ 14 15 printf("nPrinting all marks:"); 16 17 int j; 18 for(j = 0; j <= size; j++){ 19 printf("n%.1f ", marks[j]); 20 } 21 22 } Program Output: Enter a marks: 80 Enter a marks: 85.5 Enter a marks: 75 Enter a marks: 63.5 Enter a marks: 90 Enter a marks: 11.0 Printing all marks: 80.0 85.5 75.0 63.5 90.0 CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 15 / 24
  • 20.
    7 Deleting /Removing of an element CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 16 / 24
  • 21.
    Deleting / Removingan element 1 #include <stdio.h> 2 void main (){ 3 int number_array [5] = {11, 12, 13, 14, 15}; 4 5 // let ’s print all elements in the number_array 6 printf("Array before deletion: n"); 7 int i; 8 for (i = 0; i < 5; i++) { 9 printf("[%d] %d n", i, number_array [i]); 10 } 11 12 printf("let’s delete element at index 3 n"); 13 int index; 14 for (index = 3 ; index < 5 - 1 ; index ++){ 15 number_array [index] = number_array [index +1]; 16 } 17 18 printf("Array after deletion: n"); 19 for (i = 0; i < 5 - 1; i++) { 20 printf("[%d] %d n", i, number_array [i]); 21 } 22 } CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 17 / 24
  • 22.
    Deleting / Removingan element 1 #include <stdio.h> 2 void main (){ 3 int number_array [5] = {11, 12, 13, 14, 15}; 4 5 // let ’s print all elements in the number_array 6 printf("Array before deletion: n"); 7 int i; 8 for (i = 0; i < 5; i++) { 9 printf("[%d] %d n", i, number_array [i]); 10 } 11 12 printf("let’s delete element at index 3 n"); 13 int index; 14 for (index = 3 ; index < 5 - 1 ; index ++){ 15 number_array [index] = number_array [index +1]; 16 } 17 18 printf("Array after deletion: n"); 19 for (i = 0; i < 5 - 1; i++) { 20 printf("[%d] %d n", i, number_array [i]); 21 } 22 } Program Output: Array before deletion: [0] 11 [1] 12 [2] 13 [3] 14 [4] 15 let ’s delete element at index 3 Array after deletion: [0] 11 [1] 12 [2] 13 [3] 15 CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 17 / 24
  • 23.
    8 Multi-dimensional Array CSI123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 18 / 24
  • 24.
    Multi-dimensional Array Declaration: int table[2][3]; int three_d_array [3][3][3]; Declaration and Initialization: int table1 [2][3] = {{1 ,2 ,3} ,{4 ,5 ,6}}; int grid [][3] = {{1 ,2 ,3} ,{4 ,5 ,6}}; int table2 [2][3] = {1,2,3,4,5,6}; int three_d_array [3][3][3] = { {{10 , 20, 30} ,{40 , 50, 60} ,{70 , 80, 90}} , {{11 , 21, 31} ,{41 , 51, 61} ,{71 , 81, 91}} , {{33 , 34, 35} ,{36 , 37, 38} ,{39 , 40, 41}} }; CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 19 / 24
  • 25.
    9 Printing 2DArray CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 20 / 24
  • 26.
    Printing 2D Array 1#include <stdio.h> 2 void main (){ 3 int row = 3; 4 int col = 4; 5 int table [3][4] = 6 { 7 {1, 2, 3, 4}, 8 {10, 11, 12, 13}, 9 {20, 21, 22, 23} 10 }; 11 // printing 2d array 12 int i,j; 13 for(i = 0; i < row; ++i) 14 { 15 for(j = 0; j < col; ++j) 16 { 17 printf("%d ", table[i][j]); 18 } 19 printf("n"); 20 } 21 22 } CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 21 / 24
  • 27.
    Printing 2D Array 1#include <stdio.h> 2 void main (){ 3 int row = 3; 4 int col = 4; 5 int table [3][4] = 6 { 7 {1, 2, 3, 4}, 8 {10, 11, 12, 13}, 9 {20, 21, 22, 23} 10 }; 11 // printing 2d array 12 int i,j; 13 for(i = 0; i < row; ++i) 14 { 15 for(j = 0; j < col; ++j) 16 { 17 printf("%d ", table[i][j]); 18 } 19 printf("n"); 20 } 21 22 } Program Output: 1 2 3 4 10 11 12 13 20 21 22 23 CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 21 / 24
  • 28.
    10 Printing 3DArray CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 22 / 24
  • 29.
    Printing 3D Array 1#include <stdio.h> 2 void main (){ 3 int x = 3; int y = 3; int z = 3; 4 int three_d_array [3][3][3] = { 5 {{10 , 20, 30} ,{40 , 50, 60} ,{70 , 80, 90}} , 6 {{11 , 21, 31} ,{41 , 51, 61} ,{71 , 81, 91}} , 7 {{33 , 34, 35} ,{36 , 37, 38} ,{39 , 40, 41}} 8 }; 9 10 // printing 3d array 11 int i,j,k; 12 for(i=0;i<x;i++){ 13 for(j=0;j<y;j++){ 14 for(k=0;k<z;k++){ 15 printf("%dt",three_d_array [i][j][k]); 16 } 17 printf("n"); 18 } 19 printf("n"); 20 } 21 } CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 23 / 24
  • 30.
    Printing 3D Array 1#include <stdio.h> 2 void main (){ 3 int x = 3; int y = 3; int z = 3; 4 int three_d_array [3][3][3] = { 5 {{10 , 20, 30} ,{40 , 50, 60} ,{70 , 80, 90}} , 6 {{11 , 21, 31} ,{41 , 51, 61} ,{71 , 81, 91}} , 7 {{33 , 34, 35} ,{36 , 37, 38} ,{39 , 40, 41}} 8 }; 9 10 // printing 3d array 11 int i,j,k; 12 for(i=0;i<x;i++){ 13 for(j=0;j<y;j++){ 14 for(k=0;k<z;k++){ 15 printf("%dt",three_d_array [i][j][k]); 16 } 17 printf("n"); 18 } 19 printf("n"); 20 } 21 } Program Output: 10 20 30 40 50 60 70 80 90 11 21 31 41 51 61 71 81 91 33 34 35 36 37 38 39 40 41 CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 23 / 24
  • 31.
    Thanks Thanks for yourtime and attention! kamruddin.nur@gmail.com sites.google.com/view/kamruddinnur CSE Department of STAMFORD UNIVERSITY BANGLADESH "Education for Tomorrow's World" CSI 123: Structured Prog. Language Dr. Kamruddin Nur November, 2017 24 / 24