Chapter 4 Array
Chapter 4 Array
PROGRAMMING
DAE 20103
CHAPTER 4
ARRAYS
INTRODUCTION
An array is a collection of data elements that are of
the same type (e.g., a collection of integers,
collection of characters, collection of doubles).
INTRODUCTION
1-dimensional array
2-dimensional array
Oct 14
Oct 15
Oct 16
Array Declaration
Must declare both the name of the array
and the number of cells associated with it
The declaration:
Data_type array_name [expression]
Expression : size of array
Array name : name of array that are an identifier
Array Declaration
Array Initialization
Three types of array initialization:
i. Array of 10 un-initialized integers
int A[10];
ii. Initialize array
int months[12]={31,28,31,30,31,30,31,31,30,31,30,31}
iii. If the size of declared array is not given and not
initialized to any value, the size of array will be
automatically set to the length of the list.
Exp:
float a[] = {1.1,2.1,3.1,4.1,5.1};
Array Initialization
Example
int A[10];
--
A[3] = 1;
int x = A[3];
--
--
0
A
--
--
1
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Array Initialization
Array Initialization
Arrays can be initialized, but they cannot
be assigned!
Exp:
char A[3] = {a,b,c,d};
char B[3] = {f,g,h,i};
A = B ERROR
char C[3] = A; ERROR
10
Average
12
13
Allocate
Row:0-4
Allocate
Column:0-3
14
X[0][0]
X[0][1]
int x[10][5]
X[0][2]
X[0][3]
X[1][0]
X[1][1]
X[1][2]
X[1][3]
X[2][0]
X[2][1]
10
11
X[2][3]
12
float a[3][20]
/*allocate 60 spaces (row:0-2, and
column:0-19*/
Example:
15
Coding Example
/*This program will read each element in array b*/
#include<stdio.h>
main()
{
float b[3]
[4]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10,11.1,12.12};
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("[%d][%d]=%.1f\n",i,j,b[i][j]);
}
}
Output
return (0);
[0][0]=1.1
[0][1]=2.2
[0][2]=3.3 and so on.
16
STRING ARRAY
String is a grouping of characters
C implements the string data structure
using arrays of char type
Example:
Char college[11]=KUITTHO BP;
String Initialization
A string
K U
T H O
char college[11]={K,U,I,T,T,H,O,,B,P,\0};
B P \0
Coding Example
#include<stdio.h>
void main()
{
int i=0;
char time[7]="Work";
while (time[i]!='\0')
{
printf(%c, time[i]);
i++;
}
getchar();
}
Output
Work
18
EXERCISE
19
Exercises 1
1.
2.
20
Exercise 2
int A[10], i = 7, j = 2, k = 4;
A[0] = 1;
A[i] = 5;
A[j] = A[i] + 3;
A[j+1] = A[i] + A[0];
A[A[j]] = 12;
12
21
Whats wrong?
Exercise 3
Q(1)
int A[2][3] = {{2,8,1}, {4,3,9}};
int B[4] = {3, 6, 4, 2, 10, 9};
Q(2)
int A[6];
for (int i=1; i<=5; i++)
{
printf("Enter number: );
scanf(%d,&A);
22
Exercise 4
What will the output after executing the following
code?
int data[5] = {2, 8, 3, 1, 7};
int temp;
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
if(data[j] > data[i]){
temp = data[j];
data[j] = data[i];
data[i] = temp;
printf(%d\n, data[i]);
}
}
}
23
Exercise 5
What is the content of the array A upon
completion of the for loop?
int A[9] = {5, 3, 21, 33, 21, 9, 10, 80, 12};
int count, temp;
for (count = 1; count <= 8; count++) {
if (A[count-1] > A[count]) {
temp = A[count-1];
A[count-1] = A[count];
A[count] = temp;
}
}
24