Professional Documents
Culture Documents
Computer Programming
Arrays and Strings
Prepared by
Johra Muhammad Moosa
Assistant Professor
CSE, BUET
Modified by
Madhusudan Basak
Assistant Professor
CSE, BUET
Array
• List of variables of same type
• Accessed through a common name
• General form
– type var_name[size];
– int myarray[20];
– int m[10], a[5];
• Accessed by indexing
– Known as subscript
– Can be any valid expression
– Begin at 0
– myarray[1] : 2nd element
• Array elements are stored in contiguous memory location
Array (Initialization)
• int n[6]={48, 53, 26,71, 9, 12};
48 53 26 71 9 12
Output:
C
S
E
1
0
9
String Read using scanf()
• %s is used in scanf
• Reads characters untill ENTER pressed
• ENTER key is not stored, replaced with null character
• No bound checking
• Can not read multi word string
– "Department Name: EEE"
• scanf("%s", s);
– Just the array name is used
• Initialization: • Initialization:
int sqr[3][3] ={ • Specify all but the leftmost dimension
1,2,3, int sqr[][3] ={
4,5,6, 1,2,3,
7,8,9 4,5,6,
}; 7,8,9
Col no. 0 Col no. 1 Col no. 2 };
Row no. 0 1 2 3
Row no. 1 4 5 6
Row no. 2 7 8 9
Multidimensional array
• Initialization:
int sqr[3][3] ={
{1,2,3},
{4,5,6},
{7,8,9}
};
int sqr[3][3] ={1,2,3,4,5,6,7,8,9};
int sqr[][3] ={1,2,3,4,5,6,7,8,9};
Multidimensional array
• Initialization:
int sqr[3][] ={1,2,3,4,5,6,7,8,9};
int sqr[][] ={1,2,3,4,5,6,7,8,9};
• This would never work
Multidimensional array
• Arrangement of 2-D array in memory
• Memory doesn't contain rows and columns
• Elements are stored in one continuous chain
1 2 3 4 5 6 7 8 9
5000 5004 5008 5012 5016 5020 5024 5028 5032
Multidimensional array
int th[4][2][7]
Multidimensional array
Multidimensional array
int arr[4][3][2]={ {
{2,4},
{7,8},
{4,9}
},{
{7,6},
{5,1},
{3,4}
},{
{2,3},
{7,2},
{9,4}
},{
{9,3},
{6,8},
{9,0}
}
}
Multidimensional array
• An one dimensional array of two elements
• Three such arrays placed to create a two dimensional array of three rows
• Four such two dimensional arrays are placed to yield a three dimensional
array containing three two dimensional arrays.
0th 2-D Array 1st 2-D Array 2nd 2-D Array 3rd 2-D Array
2 4 7 8 4 9 7 6 5 1 3 4 2 3 7 2 9 4 9 3 6 8 9 0
String Tables
• Arrays of strings
• char names[10][40]
– 10 names (strings) each can hold 40 characters at most including null
• gets(names[2]);
• printf(names[1]);
• char student_profile[10][4][80];//name, address, father's name, mother's name
– Specify two leftmost indices
– student_profile[3][4]
String Tables
char names[][10]={"Anik", "Himel", "Fatema", "Parag", "Azad” }
100 A n i k \0
1
101 H i m e l \0
1
102 F a t e m a \0
1
103 P a r a g \0
1
104 A z a d \0 1050 (last location)
1
String Tables
• Initialization:
char words[][2][40]={
"dog", "Hund",
"no", "nein",
"to", "zu",
"I", "Ich"
};
• Curly braces are needed
• Example: 2 (5.5)
Multidimensional array
• 100-character one dimensional requires 100 bytes of memory
• 100×100 character two dimensional array requires 10,000 bytes of memory
• 100 ×100 ×100 character array requires 10,00,000 bytes
Home Works
• Cumulative skills check: 2
• Matrix addition, multiplication, sort
Reference
• TEACH YOURSELF C by Herbert Schildt (3rd Edition)
– Chapter 5 (Full)
Thank You